Code formatting.
[OpenColorIO-Configs.git] / aces_1.0.0 / python / aces_ocio / utilities.py
index c60476a..1d426d9 100644 (file)
@@ -5,10 +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'
@@ -21,7 +25,11 @@ __all__ = ['ColorSpace',
            'mat44_from_mat33',
            'filter_words',
            'files_walker',
-           'sanitize_path']
+           'replace',
+           'sanitize',
+           'compact',
+           'colorspace_prefixed_name',
+           'unpack_default']
 
 
 class ColorSpace(object):
@@ -31,15 +39,17 @@ 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],
+                 aces_transform_id=None):
         """
         Object description.
 
@@ -55,6 +65,7 @@ class ColorSpace(object):
         """
 
         self.name = name
+        self.aliases = aliases
         self.bit_depth = bit_depth
         self.description = description
         self.equality_group = equality_group
@@ -64,6 +75,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):
@@ -81,10 +93,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):
@@ -140,9 +152,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,7 +163,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.
 
@@ -167,4 +210,73 @@ def sanitize_path(path):
          Return value description.
     """
 
-    return path.replace(' ', '_').replace(')', '_').replace('(', '_')
\ No newline at end of file
+    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(((' ', '_'),
+                                ('(', '_'),
+                                (')', '_'),
+                                ('.', '_'),
+                                ('-', '_'),
+                                ('___', '_'),
+                                ('__', '_'),
+                                ('_', ''))))
+
+
+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)