From 616d30ea450f7d8b669649b1bb57a54d51db0a9a Mon Sep 17 00:00:00 2001 From: Thomas Mansencal Date: Wed, 28 Jan 2015 14:34:47 +0100 Subject: [PATCH] Update sanitisation code. --- .../python/aces_ocio/create_aces_colorspaces.py | 20 +++--- .../python/aces_ocio/create_arri_colorspaces.py | 4 +- aces_1.0.0/python/aces_ocio/utilities.py | 70 ++++++++++++++------ 3 files changed, 63 insertions(+), 31 deletions(-) diff --git a/aces_1.0.0/python/aces_ocio/create_aces_colorspaces.py b/aces_1.0.0/python/aces_ocio/create_aces_colorspaces.py index 9c8f866..8c0eeb4 100644 --- a/aces_1.0.0/python/aces_ocio/create_aces_colorspaces.py +++ b/aces_1.0.0/python/aces_ocio/create_aces_colorspaces.py @@ -21,7 +21,7 @@ from aces_ocio.generate_lut import ( from aces_ocio.utilities import ( ColorSpace, mat44_from_mat33, - sanitize_path, + sanitize, compact) @@ -106,7 +106,7 @@ def create_ACEScc(aces_CTL_directory, 'ACEScsc.ACES_to_ACEScg.a1.0.0.ctl')] lut = '%s_to_ACES.spi1d' % name - lut = sanitize_path(lut) + lut = sanitize(lut) generate_1d_LUT_from_CTL( os.path.join(lut_directory, lut), @@ -178,7 +178,7 @@ def create_ACESproxy(aces_CTL_directory, 'ACEScsc.ACES_to_ACEScg.a1.0.0.ctl')] lut = '%s_to_aces.spi1d' % name - lut = sanitize_path(lut) + lut = sanitize(lut) generate_1d_LUT_from_CTL( os.path.join(lut_directory, lut), @@ -446,7 +446,7 @@ def create_generic_log(aces_CTL_directory, 'ACESlib.OCIO_shaper_log2_to_lin_param.a1.0.0.ctl')] lut = '%s_to_aces.spi1d' % name - lut = sanitize_path(lut) + lut = sanitize(lut) generate_1d_LUT_from_CTL( os.path.join(lut_directory, lut), @@ -520,7 +520,7 @@ def create_ACES_LMT(lmt_name, if not os.path.exists(os.path.join(lut_directory, shaper_lut)): ctls = [shaper_to_ACES_CTL % aces_CTL_directory] - shaper_lut = sanitize_path(shaper_lut) + shaper_lut = sanitize(shaper_lut) generate_1d_LUT_from_CTL( os.path.join(lut_directory, shaper_lut), @@ -548,7 +548,7 @@ def create_ACES_LMT(lmt_name, lmt_values['transformCTL'])] lut = '%s.%s.spi3d' % (shaper_name, lmt_name) - lut = sanitize_path(lut) + lut = sanitize(lut) generate_3d_LUT_from_CTL( os.path.join(lut_directory, lut), @@ -580,7 +580,7 @@ def create_ACES_LMT(lmt_name, shaper_from_ACES_CTL % aces_CTL_directory] lut = 'Inverse.%s.%s.spi3d' % (odt_name, shaper_name) - lut = sanitize_path(lut) + lut = sanitize(lut) generate_3d_LUT_from_CTL( os.path.join(lut_directory, lut), @@ -743,7 +743,7 @@ def create_ACES_RRT_plus_ODT(odt_name, if not os.path.exists(os.path.join(lut_directory, shaper_lut)): ctls = [shaper_to_ACES_CTL % aces_CTL_directory] - shaper_lut = sanitize_path(shaper_lut) + shaper_lut = sanitize(shaper_lut) generate_1d_LUT_from_CTL( os.path.join(lut_directory, shaper_lut), @@ -788,7 +788,7 @@ def create_ACES_RRT_plus_ODT(odt_name, odt_values['transformCTL'])] lut = '%s.RRT.a1.0.0.%s.spi3d' % (shaper_name, odt_name) - lut = sanitize_path(lut) + lut = sanitize(lut) generate_3d_LUT_from_CTL( os.path.join(lut_directory, lut), @@ -837,7 +837,7 @@ def create_ACES_RRT_plus_ODT(odt_name, shaper_from_ACES_CTL % aces_CTL_directory] lut = 'InvRRT.a1.0.0.%s.%s.spi3d' % (odt_name, shaper_name) - lut = sanitize_path(lut) + lut = sanitize(lut) generate_3d_LUT_from_CTL( os.path.join(lut_directory, lut), diff --git a/aces_1.0.0/python/aces_ocio/create_arri_colorspaces.py b/aces_1.0.0/python/aces_ocio/create_arri_colorspaces.py index e0be872..76af824 100644 --- a/aces_1.0.0/python/aces_ocio/create_arri_colorspaces.py +++ b/aces_1.0.0/python/aces_ocio/create_arri_colorspaces.py @@ -10,7 +10,7 @@ import math import os import aces_ocio.generate_lut as genlut -from aces_ocio.utilities import ColorSpace, mat44_from_mat33, sanitize_path +from aces_ocio.utilities import ColorSpace, mat44_from_mat33, sanitize __author__ = 'ACES Developers' @@ -128,7 +128,7 @@ def create_log_c(gamut, lut = '%s_to_linear.spi1d' % ( '%s_%s' % (transfer_function, exposure_index)) - lut = sanitize_path(lut) + lut = sanitize(lut) genlut.write_SPI_1d( os.path.join(lut_directory, lut), 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(((' ', '_'), + ('(', '_'), + (')', '_'), + ('.', '_'), + ('-', '_'), + ('___', '_'), + ('__', '_'), + ('_', '')))) -- 1.7.10.4