Code formatting.
[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 itertools
11 import os
12 import re
13 from collections import OrderedDict
14
15 import PyOpenColorIO as ocio
16
17 __author__ = 'ACES Developers'
18 __copyright__ = 'Copyright (C) 2014 - 2015 - ACES Developers'
19 __license__ = ''
20 __maintainer__ = 'ACES Developers'
21 __email__ = 'aces@oscars.org'
22 __status__ = 'Production'
23
24 __all__ = ['ColorSpace',
25            'mat44_from_mat33',
26            'filter_words',
27            'files_walker',
28            'replace',
29            'sanitize',
30            'compact',
31            'colorspace_prefixed_name',
32            'unpack_default']
33
34
35 class ColorSpace(object):
36     """
37     A container for data needed to define an *OCIO* *ColorSpace*.
38     """
39
40     def __init__(self,
41                  name,
42                  aliases=[],
43                  description=None,
44                  bit_depth=ocio.Constants.BIT_DEPTH_F32,
45                  equality_group='',
46                  family=None,
47                  is_data=False,
48                  to_reference_transforms=[],
49                  from_reference_transforms=[],
50                  allocation_type=ocio.Constants.ALLOCATION_UNIFORM,
51                  allocation_vars=[0, 1],
52                  aces_transform_id=None):
53         """
54         Object description.
55
56         Parameters
57         ----------
58         parameter : type
59             Parameter description.
60
61         Returns
62         -------
63         type
64              Return value description.
65         """
66
67         self.name = name
68         self.aliases = aliases
69         self.bit_depth = bit_depth
70         self.description = description
71         self.equality_group = equality_group
72         self.family = family
73         self.is_data = is_data
74         self.to_reference_transforms = to_reference_transforms
75         self.from_reference_transforms = from_reference_transforms
76         self.allocation_type = allocation_type
77         self.allocation_vars = allocation_vars
78         self.aces_transform_id = aces_transform_id
79
80
81 def mat44_from_mat33(mat33):
82     """
83     Creates a 4x4 matrix from given 3x3 matrix.
84
85     Parameters
86     ----------
87     parameter : type
88         Parameter description.
89
90     Returns
91     -------
92     type
93          Return value description.
94     """
95
96     return [mat33[0], mat33[1], mat33[2], 0,
97             mat33[3], mat33[4], mat33[5], 0,
98             mat33[6], mat33[7], mat33[8], 0,
99             0, 0, 0, 1]
100
101
102 def filter_words(words, filters_in=None, filters_out=None, flags=0):
103     """
104     Object description.
105
106     Parameters
107     ----------
108     parameter : type
109         Parameter description.
110
111     Returns
112     -------
113     type
114          Return value description.
115     """
116
117     filtered_words = []
118     for word in words:
119         if filters_in:
120             filter_matched = False
121             for filter in filters_in:
122                 if re.search(filter, word, flags):
123                     filter_matched = True
124                     break
125             if not filter_matched:
126                 continue
127
128         if filters_out:
129             filter_matched = False
130             for filter in filters_out:
131                 if re.search(filter, word, flags):
132                     filter_matched = True
133                     break
134             if filter_matched:
135                 continue
136         filtered_words.append(word)
137     return filtered_words
138
139
140 def files_walker(directory, filters_in=None, filters_out=None, flags=0):
141     """
142     Object description.
143
144     Parameters
145     ----------
146     parameter : type
147         Parameter description.
148
149     Returns
150     -------
151     type
152          Return value description.
153     """
154
155     for parent_directory, directories, files in os.walk(
156             directory, topdown=False, followlinks=True):
157         for file in files:
158             path = os.path.join(parent_directory, file)
159             if os.path.isfile(path):
160                 if not filter_words((path,), filters_in, filters_out, flags):
161                     continue
162
163                 yield path
164
165
166 def replace(string, data):
167     """
168     Replaces the data occurrences in the string.
169
170     Parameters
171     ----------
172     string : str or unicode
173         String to manipulate.
174     data : dict
175         Replacement occurrences.
176
177     Returns
178     -------
179     unicode
180         Manipulated string.
181
182     Examples
183     --------
184     >>> patterns = {"John" : "Luke",
185     ...             "Jane" : "Anakin",
186     ...             "Doe" : "Skywalker",
187     ...             "Z6PO" : "R2D2"}
188     >>> data = "Users are: John Doe, Jane Doe, Z6PO."
189     >>> replace(data,patterns )
190     u'Users are: Luke Skywalker, Anakin Skywalker, R2D2.'
191     """
192
193     for old, new in data.iteritems():
194         string = string.replace(old, new)
195     return string
196
197
198 def sanitize(path):
199     """
200     Object description.
201
202     Parameters
203     ----------
204     parameter : type
205         Parameter description.
206
207     Returns
208     -------
209     type
210          Return value description.
211     """
212
213     return replace(path, {' ': '_', ')': '_', '(': '_'})
214
215
216 def compact(string):
217     """
218     Removes blanks, underscores, dashes and parentheses.
219
220     Parameters
221     ----------
222     string : str or unicode
223         String to compact.
224
225     Returns
226     -------
227     str or unicode
228          A compact version of that string.
229     """
230
231     return replace(string.lower(),
232                    OrderedDict(((' ', '_'),
233                                 ('(', '_'),
234                                 (')', '_'),
235                                 ('.', '_'),
236                                 ('-', '_'),
237                                 ('___', '_'),
238                                 ('__', '_'),
239                                 ('_', ''))))
240
241
242 def colorspace_prefixed_name(colorspace):
243     """
244     Returns given *OCIO* colorspace prefixed name with its family name.
245
246     Parameters
247     ----------
248     colorspace : Colorspace
249         Colorspace to prefix.
250
251     Returns
252     -------
253     str or unicode
254          Family prefixed *OCIO* colorspace name.
255     """
256     prefix = colorspace.family.replace('/', ' - ')
257
258     return '%s - %s' % (prefix, colorspace.name)
259
260
261 def unpack_default(iterable, length=3, default=None):
262     """
263     Unpacks given iterable maintaining given length and filling missing
264     entries with given default.
265
266     Parameters
267     ----------
268     iterable : object
269         Iterable.
270     length : int
271         Iterable length.
272     default : object
273         Filling default object.
274
275     Returns
276     -------
277     iterable
278     """
279
280     return itertools.islice(itertools.chain(iter(iterable),
281                                             itertools.repeat(default)),
282                             length)