b64cc62d2f19ae372d9dfdcc893cdfb226ff3d0d
[OpenColorIO-Configs.git] / aces_1.0.0 / python / aces_ocio / utilities.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 """
5 Defines various package utilities objects.
6 """
7
8 from __future__ import division
9
10 import os
11 import re
12 from collections import OrderedDict
13
14 import PyOpenColorIO as ocio
15
16 __author__ = 'ACES Developers'
17 __copyright__ = 'Copyright (C) 2014 - 2015 - ACES Developers'
18 __license__ = ''
19 __maintainer__ = 'ACES Developers'
20 __email__ = 'aces@oscars.org'
21 __status__ = 'Production'
22
23 __all__ = ['ColorSpace',
24            'mat44_from_mat33',
25            'filter_words',
26            'files_walker',
27            'replace',
28            'sanitize',
29            'compact']
30
31
32 class ColorSpace(object):
33     """
34     A container for data needed to define an *OCIO* *ColorSpace*.
35     """
36
37     def __init__(self,
38                  name,
39                  aliases=[],
40                  description=None,
41                  bit_depth=ocio.Constants.BIT_DEPTH_F32,
42                  equality_group=None,
43                  family=None,
44                  is_data=False,
45                  to_reference_transforms=[],
46                  from_reference_transforms=[],
47                  allocation_type=ocio.Constants.ALLOCATION_UNIFORM,
48                  allocation_vars=[0, 1]):
49         """
50         Object description.
51
52         Parameters
53         ----------
54         parameter : type
55             Parameter description.
56
57         Returns
58         -------
59         type
60              Return value description.
61         """
62
63         self.name = name
64         self.aliases = []
65         self.bit_depth = bit_depth
66         self.description = description
67         self.equality_group = equality_group
68         self.family = family
69         self.is_data = is_data
70         self.to_reference_transforms = to_reference_transforms
71         self.from_reference_transforms = from_reference_transforms
72         self.allocation_type = allocation_type
73         self.allocation_vars = allocation_vars
74
75
76 def mat44_from_mat33(mat33):
77     """
78     Creates a 4x4 matrix from given 3x3 matrix.
79
80     Parameters
81     ----------
82     parameter : type
83         Parameter description.
84
85     Returns
86     -------
87     type
88          Return value description.
89     """
90
91     return [mat33[0], mat33[1], mat33[2], 0,
92             mat33[3], mat33[4], mat33[5], 0,
93             mat33[6], mat33[7], mat33[8], 0,
94             0, 0, 0, 1]
95
96
97 def filter_words(words, filters_in=None, filters_out=None, flags=0):
98     """
99     Object description.
100
101     Parameters
102     ----------
103     parameter : type
104         Parameter description.
105
106     Returns
107     -------
108     type
109          Return value description.
110     """
111
112     filtered_words = []
113     for word in words:
114         if filters_in:
115             filter_matched = False
116             for filter in filters_in:
117                 if re.search(filter, word, flags):
118                     filter_matched = True
119                     break
120             if not filter_matched:
121                 continue
122
123         if filters_out:
124             filter_matched = False
125             for filter in filters_out:
126                 if re.search(filter, word, flags):
127                     filter_matched = True
128                     break
129             if filter_matched:
130                 continue
131         filtered_words.append(word)
132     return filtered_words
133
134
135 def files_walker(directory, filters_in=None, filters_out=None, flags=0):
136     """
137     Object description.
138
139     Parameters
140     ----------
141     parameter : type
142         Parameter description.
143
144     Returns
145     -------
146     type
147          Return value description.
148     """
149
150     for parent_directory, directories, files in os.walk(
151             directory, topdown=False, followlinks=True):
152         for file in files:
153             path = os.path.join(parent_directory, file)
154             if os.path.isfile(path):
155                 if not filter_words((path,), filters_in, filters_out, flags):
156                     continue
157
158                 yield path
159
160
161 def replace(string, data):
162     """
163     Replaces the data occurrences in the string.
164
165     Parameters
166     ----------
167     string : str or unicode
168         String to manipulate.
169     data : dict
170         Replacement occurrences.
171
172     Returns
173     -------
174     unicode
175         Manipulated string.
176
177     Examples
178     --------
179     >>> patterns = {"John" : "Luke",
180     ...             "Jane" : "Anakin",
181     ...             "Doe" : "Skywalker",
182     ...             "Z6PO" : "R2D2"}
183     >>> data = "Users are: John Doe, Jane Doe, Z6PO."
184     >>> replace(data,patterns )
185     u'Users are: Luke Skywalker, Anakin Skywalker, R2D2.'
186     """
187
188     for old, new in data.iteritems():
189         string = string.replace(old, new)
190     return string
191
192
193 def sanitize(path):
194     """
195     Object description.
196
197     Parameters
198     ----------
199     parameter : type
200         Parameter description.
201
202     Returns
203     -------
204     type
205          Return value description.
206     """
207
208     return replace(path, {' ': '_', ')': '_', '(': '_'})
209
210
211 def compact(string):
212     """
213     Removes blanks, underscores, dashes and parentheses.
214
215     Parameters
216     ----------
217     string : str or unicode
218         String to compact.
219
220     Returns
221     -------
222     str or unicode
223          A compact version of that string.
224     """
225
226     return replace(string.lower(),
227                    OrderedDict(((' ', '_'),
228                                 ('(', '_'),
229                                 (')', '_'),
230                                 ('.', '_'),
231                                 ('-', '_'),
232                                 ('___', '_'),
233                                 ('__', '_'),
234                                 ('_', ''))))