Update sanitisation code.
[OpenColorIO-Configs.git] / aces_1.0.0 / python / aces_ocio / utilities.py
index 00804e3..6ad832b 100644 (file)
@@ -7,6 +7,7 @@ Defines various package utilities objects.
 
 import os
 import re
+from collections import OrderedDict
 
 import PyOpenColorIO as OCIO
 
@@ -20,11 +21,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,6 +34,7 @@ class ColorSpace(object):
 
     def __init__(self,
                  name,
+                 aliases=[],
                  description=None,
                  bit_depth=OCIO.Constants.BIT_DEPTH_F32,
                  equality_group=None,
@@ -57,6 +59,7 @@ class ColorSpace(object):
         """
 
         self.name = name
+        self.aliases = []
         self.bit_depth = bit_depth
         self.description = description
         self.equality_group = equality_group
@@ -152,3 +155,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(((' ', '_'),
+                                ('(', '_'),
+                                (')', '_'),
+                                ('.', '_'),
+                                ('-', '_'),
+                                ('___', '_'),
+                                ('__', '_'),
+                                ('_', ''))))