e5982219316b9a36ea4687016a67546a8b28c6ee
[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='',
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                  aces_transform_id=None):
50         """
51         Object description.
52
53         Parameters
54         ----------
55         parameter : type
56             Parameter description.
57
58         Returns
59         -------
60         type
61              Return value description.
62         """
63
64         self.name = name
65         self.aliases = aliases
66         self.bit_depth = bit_depth
67         self.description = description
68         self.equality_group = equality_group
69         self.family = family
70         self.is_data = is_data
71         self.to_reference_transforms = to_reference_transforms
72         self.from_reference_transforms = from_reference_transforms
73         self.allocation_type = allocation_type
74         self.allocation_vars = allocation_vars
75         self.aces_transform_id = aces_transform_id
76
77 def mat44_from_mat33(mat33):
78     """
79     Creates a 4x4 matrix from given 3x3 matrix.
80
81     Parameters
82     ----------
83     parameter : type
84         Parameter description.
85
86     Returns
87     -------
88     type
89          Return value description.
90     """
91
92     return [mat33[0], mat33[1], mat33[2], 0,
93             mat33[3], mat33[4], mat33[5], 0,
94             mat33[6], mat33[7], mat33[8], 0,
95             0, 0, 0, 1]
96
97
98 def filter_words(words, filters_in=None, filters_out=None, flags=0):
99     """
100     Object description.
101
102     Parameters
103     ----------
104     parameter : type
105         Parameter description.
106
107     Returns
108     -------
109     type
110          Return value description.
111     """
112
113     filtered_words = []
114     for word in words:
115         if filters_in:
116             filter_matched = False
117             for filter in filters_in:
118                 if re.search(filter, word, flags):
119                     filter_matched = True
120                     break
121             if not filter_matched:
122                 continue
123
124         if filters_out:
125             filter_matched = False
126             for filter in filters_out:
127                 if re.search(filter, word, flags):
128                     filter_matched = True
129                     break
130             if filter_matched:
131                 continue
132         filtered_words.append(word)
133     return filtered_words
134
135
136 def files_walker(directory, filters_in=None, filters_out=None, flags=0):
137     """
138     Object description.
139
140     Parameters
141     ----------
142     parameter : type
143         Parameter description.
144
145     Returns
146     -------
147     type
148          Return value description.
149     """
150
151     for parent_directory, directories, files in os.walk(
152             directory, topdown=False, followlinks=True):
153         for file in files:
154             path = os.path.join(parent_directory, file)
155             if os.path.isfile(path):
156                 if not filter_words((path,), filters_in, filters_out, flags):
157                     continue
158
159                 yield path
160
161
162 def replace(string, data):
163     """
164     Replaces the data occurrences in the string.
165
166     Parameters
167     ----------
168     string : str or unicode
169         String to manipulate.
170     data : dict
171         Replacement occurrences.
172
173     Returns
174     -------
175     unicode
176         Manipulated string.
177
178     Examples
179     --------
180     >>> patterns = {"John" : "Luke",
181     ...             "Jane" : "Anakin",
182     ...             "Doe" : "Skywalker",
183     ...             "Z6PO" : "R2D2"}
184     >>> data = "Users are: John Doe, Jane Doe, Z6PO."
185     >>> replace(data,patterns )
186     u'Users are: Luke Skywalker, Anakin Skywalker, R2D2.'
187     """
188
189     for old, new in data.iteritems():
190         string = string.replace(old, new)
191     return string
192
193
194 def sanitize(path):
195     """
196     Object description.
197
198     Parameters
199     ----------
200     parameter : type
201         Parameter description.
202
203     Returns
204     -------
205     type
206          Return value description.
207     """
208
209     return replace(path, {' ': '_', ')': '_', '(': '_'})
210
211
212 def compact(string):
213     """
214     Removes blanks, underscores, dashes and parentheses.
215
216     Parameters
217     ----------
218     string : str or unicode
219         String to compact.
220
221     Returns
222     -------
223     str or unicode
224          A compact version of that string.
225     """
226
227     return replace(string.lower(),
228                    OrderedDict(((' ', '_'),
229                                 ('(', '_'),
230                                 (')', '_'),
231                                 ('.', '_'),
232                                 ('-', '_'),
233                                 ('___', '_'),
234                                 ('__', '_'),
235                                 ('_', ''))))