Implement usage of "with" statement on relevant IO operations.
[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                  description=None,
35                  bit_depth=OCIO.Constants.BIT_DEPTH_F32,
36                  equality_group=None,
37                  family=None,
38                  is_data=False,
39                  to_reference_transforms=[],
40                  from_reference_transforms=[],
41                  allocation_type=OCIO.Constants.ALLOCATION_UNIFORM,
42                  allocation_vars=[0.0, 1.0]):
43         """
44         Object description.
45
46         Parameters
47         ----------
48         parameter : type
49             Parameter description.
50
51         Returns
52         -------
53         type
54              Return value description.
55         """
56
57         self.name = name
58         self.bit_depth = bit_depth
59         self.description = description
60         self.equality_group = equality_group
61         self.family = family
62         self.is_data = is_data
63         self.to_reference_transforms = to_reference_transforms
64         self.from_reference_transforms = from_reference_transforms
65         self.allocation_type = allocation_type
66         self.allocation_vars = allocation_vars
67
68
69 def mat44_from_mat33(mat33):
70     """
71     Creates a 4x4 matrix from given 3x3 matrix.
72
73     Parameters
74     ----------
75     parameter : type
76         Parameter description.
77
78     Returns
79     -------
80     type
81          Return value description.
82     """
83
84     return [mat33[0], mat33[1], mat33[2], 0.0,
85             mat33[3], mat33[4], mat33[5], 0.0,
86             mat33[6], mat33[7], mat33[8], 0.0,
87             0, 0, 0, 1.0]
88
89
90 def filter_words(words, filters_in=None, filters_out=None, flags=0):
91     """
92     Object description.
93
94     Parameters
95     ----------
96     parameter : type
97         Parameter description.
98
99     Returns
100     -------
101     type
102          Return value description.
103     """
104
105     filtered_words = []
106     for word in words:
107         if filters_in:
108             filter_matched = False
109             for filter in filters_in:
110                 if re.search(filter, word, flags):
111                     filter_matched = True
112                     break
113             if not filter_matched:
114                 continue
115
116         if filters_out:
117             filter_matched = False
118             for filter in filters_out:
119                 if re.search(filter, word, flags):
120                     filter_matched = True
121                     break
122             if filter_matched:
123                 continue
124         filtered_words.append(word)
125     return filtered_words
126
127
128 def files_walker(directory, filters_in=None, filters_out=None, flags=0):
129     """
130     Object description.
131
132     Parameters
133     ----------
134     parameter : type
135         Parameter description.
136
137     Returns
138     -------
139     type
140          Return value description.
141     """
142
143     for parent_directory, directories, files in os.walk(directory,
144                                                         topdown=False,
145                                                         followlinks=True):
146         for file in files:
147             path = os.path.join(parent_directory, file)
148             if os.path.isfile(path):
149                 if not filter_words((path,), filters_in, filters_out, flags):
150                     continue
151
152                 yield path
153
154
155 def sanitize_path(path):
156     """
157     Object description.
158
159     Parameters
160     ----------
161     parameter : type
162         Parameter description.
163
164     Returns
165     -------
166     type
167          Return value description.
168     """
169
170     return path.replace(' ', '_').replace(')', '_').replace('(', '_')