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