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=6ad832b6906b333d3d55e394f27d044b239e83ad;hp=27118436eaa21b1b3fe9428d0a4f0ac4a105b7d6;hb=616d30ea450f7d8b669649b1bb57a54d51db0a9a;hpb=0c0c9313006b5436ae6e09f68d66196b8a48743b diff --git a/aces_1.0.0/python/aces_ocio/utilities.py b/aces_1.0.0/python/aces_ocio/utilities.py index 2711843..6ad832b 100644 --- a/aces_1.0.0/python/aces_ocio/utilities.py +++ b/aces_1.0.0/python/aces_ocio/utilities.py @@ -7,6 +7,7 @@ Defines various package utilities objects. import os import re +from collections import OrderedDict import PyOpenColorIO as OCIO @@ -21,7 +22,9 @@ __all__ = ['ColorSpace', 'mat44_from_mat33', 'filter_words', 'files_walker', - 'sanitize_path'] + 'replace', + 'sanitize', + 'compact'] class ColorSpace(object): @@ -154,7 +157,39 @@ def files_walker(directory, filters_in=None, filters_out=None, flags=0): yield path -def sanitize_path(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. @@ -169,33 +204,30 @@ def sanitize_path(path): Return value description. """ - return path.replace(' ', '_').replace(')', '_').replace('(', '_') + return replace(path, {' ': '_', ')': '_', '(': '_'}) def compact(string): """ - Removes blanks, underscores, dashes and parentheses + Removes blanks, underscores, dashes and parentheses. Parameters ---------- - parameter : type - A string. + string : str or unicode + String to compact. Returns ------- - type + str or unicode A compact version of that string. """ - compact = string - compact = compact.lower() - compact = compact.replace(' ', '_') - compact = compact.replace('(', '_') - compact = compact.replace(')', '_') - compact = compact.replace('.', '_') - compact = compact.replace('-', '_') - compact = compact.replace('___', '_') - compact = compact.replace('__', '_') - compact = compact.replace('_', '') - - return compact + return replace(string.lower(), + OrderedDict(((' ', '_'), + ('(', '_'), + (')', '_'), + ('.', '_'), + ('-', '_'), + ('___', '_'), + ('__', '_'), + ('_', ''))))