X-Git-Url: http://users.mur.at/ms/git/gitweb/?p=OpenColorIO-Configs.git;a=blobdiff_plain;f=aces_1.0.0%2Fpython%2Faces_ocio%2Futilities.py;h=56c9c9d2696f53792797b111828de626d3797b16;hp=00804e3947710f1dea1e43cb236b01bcc5f9c1bd;hb=54a5731f8ae165da84bbfd8f460359a21433708e;hpb=6eac5f132b3888eee16a8306d91b9405346f3ac2 diff --git a/aces_1.0.0/python/aces_ocio/utilities.py b/aces_1.0.0/python/aces_ocio/utilities.py index 00804e3..56c9c9d 100644 --- a/aces_1.0.0/python/aces_ocio/utilities.py +++ b/aces_1.0.0/python/aces_ocio/utilities.py @@ -5,10 +5,13 @@ Defines various package utilities objects. """ +from __future__ import division + 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' @@ -20,11 +23,11 @@ __status__ = 'Production' __all__ = ['ColorSpace', 'mat44_from_mat33', 'filter_words', - 'files_walker'] + 'files_walker', + 'replace', + 'sanitize', + 'compact'] -# -# Utility classes and functions -# class ColorSpace(object): """ @@ -33,15 +36,16 @@ class ColorSpace(object): def __init__(self, name, + aliases=[], 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]): + allocation_type=ocio.Constants.ALLOCATION_UNIFORM, + allocation_vars=[0, 1]): """ Object description. @@ -57,6 +61,7 @@ class ColorSpace(object): """ self.name = name + self.aliases = aliases self.bit_depth = bit_depth self.description = description self.equality_group = equality_group @@ -83,10 +88,10 @@ def mat44_from_mat33(mat33): Return value description. """ - 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): @@ -142,9 +147,8 @@ def files_walker(directory, filters_in=None, filters_out=None, flags=0): Return value description. """ - 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): @@ -152,3 +156,79 @@ def files_walker(directory, filters_in=None, filters_out=None, flags=0): continue yield path + + +def replace(string, data): + """ + Replaces the data occurrences in the string. + + Parameters + ---------- + string : str or unicode + String to manipulate. + data : dict + Replacement occurrences. + + Returns + ------- + unicode + Manipulated string. + + Examples + -------- + >>> 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.' + """ + + for old, new in data.iteritems(): + string = string.replace(old, new) + return string + + +def sanitize(path): + """ + Object description. + + Parameters + ---------- + parameter : type + Parameter description. + + Returns + ------- + type + Return value description. + """ + + return replace(path, {' ': '_', ')': '_', '(': '_'}) + + +def compact(string): + """ + Removes blanks, underscores, dashes and parentheses. + + Parameters + ---------- + string : str or unicode + String to compact. + + Returns + ------- + str or unicode + A compact version of that string. + """ + + return replace(string.lower(), + OrderedDict(((' ', '_'), + ('(', '_'), + (')', '_'), + ('.', '_'), + ('-', '_'), + ('___', '_'), + ('__', '_'), + ('_', ''))))