2 # -*- coding: utf-8 -*-
5 Defines various package utilities objects.
8 from __future__ import division
13 from collections import OrderedDict
15 import PyOpenColorIO as ocio
17 __author__ = 'ACES Developers'
18 __copyright__ = 'Copyright (C) 2014 - 2015 - ACES Developers'
20 __maintainer__ = 'ACES Developers'
21 __email__ = 'aces@oscars.org'
22 __status__ = 'Production'
24 __all__ = ['ColorSpace',
31 'colorspace_prefixed_name',
35 class ColorSpace(object):
37 A container for data needed to define an *OCIO* *ColorSpace*.
44 bit_depth=ocio.Constants.BIT_DEPTH_F32,
48 to_reference_transforms=None,
49 from_reference_transforms=None,
50 allocation_type=ocio.Constants.ALLOCATION_UNIFORM,
52 aces_transform_id=None):
54 Constructor for ColorSpace container class
59 Name of the colorspace
60 All other arguments are optional
70 if to_reference_transforms is None:
71 to_reference_transforms = []
73 if from_reference_transforms is None:
74 from_reference_transforms = []
76 if allocation_vars is None:
77 allocation_vars = [0, 1]
80 self.aliases = aliases
81 self.bit_depth = bit_depth
82 self.description = description
83 self.equality_group = equality_group
85 self.is_data = is_data
86 self.to_reference_transforms = to_reference_transforms
87 self.from_reference_transforms = from_reference_transforms
88 self.allocation_type = allocation_type
89 self.allocation_vars = allocation_vars
90 self.aces_transform_id = aces_transform_id
93 def mat44_from_mat33(mat33):
95 Creates a 4x4 matrix from given 3x3 matrix.
99 mat33 : array of float
108 return [mat33[0], mat33[1], mat33[2], 0,
109 mat33[3], mat33[4], mat33[5], 0,
110 mat33[6], mat33[7], mat33[8], 0,
114 def filter_words(words, filters_in=None, filters_out=None, flags=0):
116 A function to filter strings in an array
120 words : array of str or unicode
122 filters_in : array of str or unicode, optional
124 filters_out : array of str or unicode, optional
126 flags : int, optional
131 array of str or unicode
132 An array of matched or unmatched strings
138 filter_matched = False
139 for filter in filters_in:
140 if re.search(filter, word, flags):
141 filter_matched = True
143 if not filter_matched:
147 filter_matched = False
148 for filter in filters_out:
149 if re.search(filter, word, flags):
150 filter_matched = True
154 filtered_words.append(word)
155 return filtered_words
158 def files_walker(directory, filters_in=None, filters_out=None, flags=0):
160 A function to walk a directory hierarchy, only returning items that do or
161 do not match the specified filters
165 directory : str or unicode
166 The starting point for directory walking
167 filters_in : array of str or unicode, optional
168 File or directory names to match
169 filters_out : array of str or unicode, optional
170 File or directory names to NOT match
171 flags : int, optional
177 The next matching file or directory name
180 for parent_directory, directories, files in os.walk(
181 directory, topdown=False, followlinks=True):
183 path = os.path.join(parent_directory, file)
184 if os.path.isfile(path):
185 if not filter_words((path,), filters_in, filters_out, flags):
191 def replace(string, data):
193 Replaces the data occurrences in the string.
197 string : str or unicode
198 String to manipulate.
200 Replacement occurrences.
209 >>> patterns = {'John' : 'Luke',
210 ... 'Jane' : 'Anakin',
211 ... 'Doe' : 'Skywalker',
213 >>> data = 'Users are: John Doe, Jane Doe, Z6PO.'
214 >>> replace(data,patterns )
215 u'Users are: Luke Skywalker, Anakin Skywalker, R2D2.'
218 for old, new in data.iteritems():
219 string = string.replace(old, new)
225 Replaces occurrences of ' ', '(', or ')' in the string with an underscore.
229 path : str or unicode
230 Path string to manipulate.
238 return replace(path, {' ': '_', ')': '_', '(': '_'})
243 Removes blanks, underscores, dashes and parentheses.
247 string : str or unicode
253 A compact version of that string.
256 return replace(string.lower(),
257 OrderedDict(((' ', '_'),
267 def colorspace_prefixed_name(colorspace):
269 Returns given *OCIO* colorspace prefixed name with its family name.
273 colorspace : Colorspace
274 Colorspace to prefix.
279 Family prefixed *OCIO* colorspace name.
281 prefix = colorspace.family.replace('/', ' - ')
283 return '%s - %s' % (prefix, colorspace.name)
286 def unpack_default(iterable, length=3, default=None):
288 Unpacks given iterable maintaining given length and filling missing
289 entries with given default.
298 Filling default object.
305 return itertools.islice(
306 itertools.chain(iter(iterable), itertools.repeat(default)), length)