Add module headers doctoring skeletons.
[OpenColorIO-Configs.git] / aces_1.0.0 / python / aces_ocio / util.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            'mat44FromMat33',
22            'filter_words',
23            'files_walker']
24
25 #
26 # Utility classes and functions
27 #
28
29 class ColorSpace:
30     """
31     A container for data needed to define an OCIO 'Color Space'
32     """
33
34     def __init__(self,
35                  name,
36                  description=None,
37                  bitDepth=OCIO.Constants.BIT_DEPTH_F32,
38                  equalityGroup=None,
39                  family=None,
40                  isData=False,
41                  toReferenceTransforms=[],
42                  fromReferenceTransforms=[],
43                  allocationType=OCIO.Constants.ALLOCATION_UNIFORM,
44                  allocationVars=[0.0, 1.0]):
45         """
46         Object description.
47
48         Parameters
49         ----------
50         parameter : type
51             Parameter description.
52
53         Returns
54         -------
55         type
56              Return value description.
57         """
58
59         self.name = name
60         self.bitDepth = bitDepth
61         self.description = description
62         self.equalityGroup = equalityGroup
63         self.family = family
64         self.isData = isData
65         self.toReferenceTransforms = toReferenceTransforms
66         self.fromReferenceTransforms = fromReferenceTransforms
67         self.allocationType = allocationType
68         self.allocationVars = allocationVars
69
70
71 def mat44FromMat33(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