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