X-Git-Url: http://users.mur.at/ms/git/gitweb/?a=blobdiff_plain;f=aces_1.0.0%2Fpython%2Faces_ocio%2Futilities.py;h=947bf936396436e65fa95854267605afbd9cd673;hb=008ab628a49b703cd1141a39b7963947adce9e99;hp=6ad832b6906b333d3d55e394f27d044b239e83ad;hpb=616d30ea450f7d8b669649b1bb57a54d51db0a9a;p=OpenColorIO-Configs.git diff --git a/aces_1.0.0/python/aces_ocio/utilities.py b/aces_1.0.0/python/aces_ocio/utilities.py index 6ad832b..947bf93 100644 --- a/aces_1.0.0/python/aces_ocio/utilities.py +++ b/aces_1.0.0/python/aces_ocio/utilities.py @@ -5,11 +5,14 @@ Defines various package utilities objects. """ +from __future__ import division + +import itertools import os import re from collections import OrderedDict -import PyOpenColorIO as OCIO +import PyOpenColorIO as ocio __author__ = 'ACES Developers' __copyright__ = 'Copyright (C) 2014 - 2015 - ACES Developers' @@ -24,7 +27,9 @@ __all__ = ['ColorSpace', 'files_walker', 'replace', 'sanitize', - 'compact'] + 'compact', + 'colorspace_prefixed_name', + 'unpack_default'] class ColorSpace(object): @@ -34,32 +39,45 @@ class ColorSpace(object): def __init__(self, name, - aliases=[], + aliases=None, description=None, - bit_depth=OCIO.Constants.BIT_DEPTH_F32, - equality_group=None, + bit_depth=ocio.Constants.BIT_DEPTH_F32, + equality_group='', family=None, is_data=False, - to_reference_transforms=[], - from_reference_transforms=[], - allocation_type=OCIO.Constants.ALLOCATION_UNIFORM, - allocation_vars=[0.0, 1.0]): + to_reference_transforms=None, + from_reference_transforms=None, + allocation_type=ocio.Constants.ALLOCATION_UNIFORM, + allocation_vars=None, + aces_transform_id=None): """ - Object description. + Constructor for ColorSpace container class Parameters ---------- - parameter : type - Parameter description. + name : str or unicode + Name of the colorspace + All other arguments are optional Returns ------- - type - Return value description. + None """ + if aliases is None: + aliases = [] + + if to_reference_transforms is None: + to_reference_transforms = [] + + if from_reference_transforms is None: + from_reference_transforms = [] + + if allocation_vars is None: + allocation_vars = [0, 1] + self.name = name - self.aliases = [] + self.aliases = aliases self.bit_depth = bit_depth self.description = description self.equality_group = equality_group @@ -69,6 +87,7 @@ class ColorSpace(object): self.from_reference_transforms = from_reference_transforms self.allocation_type = allocation_type self.allocation_vars = allocation_vars + self.aces_transform_id = aces_transform_id def mat44_from_mat33(mat33): @@ -77,34 +96,40 @@ def mat44_from_mat33(mat33): Parameters ---------- - parameter : type - Parameter description. + mat33 : array of float + A 3x3 matrix Returns ------- - type - Return value description. + array of float + A 4x4 matrix """ - return [mat33[0], mat33[1], mat33[2], 0.0, - mat33[3], mat33[4], mat33[5], 0.0, - mat33[6], mat33[7], mat33[8], 0.0, - 0, 0, 0, 1.0] + return [mat33[0], mat33[1], mat33[2], 0, + mat33[3], mat33[4], mat33[5], 0, + mat33[6], mat33[7], mat33[8], 0, + 0, 0, 0, 1] def filter_words(words, filters_in=None, filters_out=None, flags=0): """ - Object description. + A function to filter strings in an array Parameters ---------- - parameter : type - Parameter description. + words : array of str or unicode + Array of strings + filters_in : array of str or unicode, optional + Words to match + filters_out : array of str or unicode, optional + Words to NOT match + flags : int, optional + Flags for re.search Returns ------- - type - Return value description. + array of str or unicode + An array of matched or unmatched strings """ filtered_words = [] @@ -132,22 +157,28 @@ def filter_words(words, filters_in=None, filters_out=None, flags=0): def files_walker(directory, filters_in=None, filters_out=None, flags=0): """ - Object description. + A function to walk a directory hierarchy, only returning items that do or + do not match the specified filters Parameters ---------- - parameter : type - Parameter description. + directory : str or unicode + The starting point for directory walking + filters_in : array of str or unicode, optional + File or directory names to match + filters_out : array of str or unicode, optional + File or directory names to NOT match + flags : int, optional + Flags for re.search Returns ------- - type - Return value description. + iterable + The next matching file or directory name """ - for parent_directory, directories, files in os.walk(directory, - topdown=False, - followlinks=True): + for parent_directory, directories, files in os.walk( + directory, topdown=False, followlinks=True): for file in files: path = os.path.join(parent_directory, file) if os.path.isfile(path): @@ -175,11 +206,11 @@ def replace(string, data): Examples -------- - >>> patterns = {"John" : "Luke", - ... "Jane" : "Anakin", - ... "Doe" : "Skywalker", - ... "Z6PO" : "R2D2"} - >>> data = "Users are: John Doe, Jane Doe, Z6PO." + >>> patterns = {'John' : 'Luke', + ... 'Jane' : 'Anakin', + ... 'Doe' : 'Skywalker', + ... 'Z6PO' : 'R2D2'} + >>> data = 'Users are: John Doe, Jane Doe, Z6PO.' >>> replace(data,patterns ) u'Users are: Luke Skywalker, Anakin Skywalker, R2D2.' """ @@ -191,17 +222,17 @@ def replace(string, data): def sanitize(path): """ - Object description. + Replaces occurrences of ' ', '(', or ')' in the string with an underscore. Parameters ---------- - parameter : type - Parameter description. + path : str or unicode + Path string to manipulate. Returns ------- - type - Return value description. + unicode + Manipulated string. """ return replace(path, {' ': '_', ')': '_', '(': '_'}) @@ -231,3 +262,45 @@ def compact(string): ('___', '_'), ('__', '_'), ('_', '')))) + + +def colorspace_prefixed_name(colorspace): + """ + Returns given *OCIO* colorspace prefixed name with its family name. + + Parameters + ---------- + colorspace : Colorspace + Colorspace to prefix. + + Returns + ------- + str or unicode + Family prefixed *OCIO* colorspace name. + """ + prefix = colorspace.family.replace('/', ' - ') + + return '%s - %s' % (prefix, colorspace.name) + + +def unpack_default(iterable, length=3, default=None): + """ + Unpacks given iterable maintaining given length and filling missing + entries with given default. + + Parameters + ---------- + iterable : object + Iterable. + length : int + Iterable length. + default : object + Filling default object. + + Returns + ------- + iterable + """ + + return itertools.islice( + itertools.chain(iter(iterable), itertools.repeat(default)), length)