54a495e32c97062be2035af216b479da1a8ebbd6
[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 import os
9 import re
10
11 import PyOpenColorIO as OCIO
12
13 __author__ = 'ACES Developers'
14 __copyright__ = 'Copyright (C) 2014 - 2015 - ACES Developers'
15 __license__ = ''
16 __maintainer__ = 'ACES Developers'
17 __email__ = 'aces@oscars.org'
18 __status__ = 'Production'
19
20 __all__ = ['ColorSpace',
21            'mat44_from_mat33',
22            'filter_words',
23            'files_walker',
24            'sanitize_path']
25
26
27 class ColorSpace(object):
28     """
29     A container for data needed to define an *OCIO* *ColorSpace*.
30     """
31
32     def __init__(self,
33                  name,
34                  aliases=[],
35                  description=None,
36                  bit_depth=OCIO.Constants.BIT_DEPTH_F32,
37                  equality_group=None,
38                  family=None,
39                  is_data=False,
40                  to_reference_transforms=[],
41                  from_reference_transforms=[],
42                  allocation_type=OCIO.Constants.ALLOCATION_UNIFORM,
43                  allocation_vars=[0.0, 1.0]):
44         """
45         Object description.
46
47         Parameters
48         ----------
49         parameter : type
50             Parameter description.
51
52         Returns
53         -------
54         type
55              Return value description.
56         """
57
58         self.name = name
59         self.aliases = []
60         self.bit_depth = bit_depth
61         self.description = description
62         self.equality_group = equality_group
63         self.family = family
64         self.is_data = is_data
65         self.to_reference_transforms = to_reference_transforms
66         self.from_reference_transforms = from_reference_transforms
67         self.allocation_type = allocation_type
68         self.allocation_vars = allocation_vars
69
70
71 def mat44_from_mat33(mat33):
72     """
73     Creates a 4x4 matrix from given 3x3 matrix.
74
75     Parameters
76     ----------
77     parameter : type
78         Parameter description.
79
80     Returns
81     -------
82     type
83          Return value description.
84     """
85
86     return [mat33[0], mat33[1], mat33[2], 0.0,
87             mat33[3], mat33[4], mat33[5], 0.0,
88             mat33[6], mat33[7], mat33[8], 0.0,
89             0, 0, 0, 1.0]
90
91
92 def filter_words(words, filters_in=None, filters_out=None, flags=0):
93     """
94     Object description.
95
96     Parameters
97     ----------
98     parameter : type
99         Parameter description.
100
101     Returns
102     -------
103     type
104          Return value description.
105     """
106
107     filtered_words = []
108     for word in words:
109         if filters_in:
110             filter_matched = False
111             for filter in filters_in:
112                 if re.search(filter, word, flags):
113                     filter_matched = True
114                     break
115             if not filter_matched:
116                 continue
117
118         if filters_out:
119             filter_matched = False
120             for filter in filters_out:
121                 if re.search(filter, word, flags):
122                     filter_matched = True
123                     break
124             if filter_matched:
125                 continue
126         filtered_words.append(word)
127     return filtered_words
128
129
130 def files_walker(directory, filters_in=None, filters_out=None, flags=0):
131     """
132     Object description.
133
134     Parameters
135     ----------
136     parameter : type
137         Parameter description.
138
139     Returns
140     -------
141     type
142          Return value description.
143     """
144
145     for parent_directory, directories, files in os.walk(directory,
146                                                         topdown=False,
147                                                         followlinks=True):
148         for file in files:
149             path = os.path.join(parent_directory, file)
150             if os.path.isfile(path):
151                 if not filter_words((path,), filters_in, filters_out, flags):
152                     continue
153
154                 yield path
155
156
157 def sanitize_path(path):
158     """
159     Object description.
160
161     Parameters
162     ----------
163     parameter : type
164         Parameter description.
165
166     Returns
167     -------
168     type
169          Return value description.
170     """
171
172     return path.replace(' ', '_').replace(')', '_').replace('(', '_')
173
174 def compact(string):
175     """
176     Removes blanks, underscores, dashes and parentheses
177
178     Parameters
179     ----------
180     parameter : type
181         A string.
182
183     Returns
184     -------
185     type
186          A compact version of that string.
187     """
188
189     compact = string
190     compact = compact.lower()
191     compact = compact.replace(' ', '_')
192     compact = compact.replace('(', '_')
193     compact = compact.replace(')', '_')
194     compact = compact.replace('.', '_')
195     compact = compact.replace('-', '_')
196     compact = compact.replace('___', '_')
197     compact = compact.replace('__', '_')
198     compact = compact.replace('_', '')
199
200     return compact