From e609947e7cc6ae0696dd5b8f82b80ac894e80565 Mon Sep 17 00:00:00 2001 From: Thomas Mansencal Date: Mon, 19 Jan 2015 15:05:17 +0100 Subject: [PATCH] Implement usage of "with" statement on relevant IO operations. --- aces_1.0.0/python/aces_ocio/create_aces_config.py | 13 ++++------- aces_1.0.0/python/aces_ocio/generate_lut.py | 25 ++++++++++---------- aces_1.0.0/python/aces_ocio/process.py | 26 ++++++++------------- aces_1.0.0/python/aces_ocio/utilities.py | 24 +++++++++++++++---- 4 files changed, 46 insertions(+), 42 deletions(-) diff --git a/aces_1.0.0/python/aces_ocio/create_aces_config.py b/aces_1.0.0/python/aces_ocio/create_aces_config.py index 63b1314..44dc565 100755 --- a/aces_1.0.0/python/aces_ocio/create_aces_config.py +++ b/aces_1.0.0/python/aces_ocio/create_aces_config.py @@ -145,9 +145,8 @@ def write_config(config, config_path, sanity_check=True): return # sys.exit() - file_handle = open(config_path, mode='w') - file_handle.write(config.serialize()) - file_handle.close() + with open(config_path, mode='w') as fp: + fp.write(config.serialize()) def generate_OCIO_transform(transforms): @@ -1525,11 +1524,8 @@ def get_transform_info(ctl_transform): Return value description. """ - # TODO: Use *with* statement. - fp = open(ctl_transform, 'rb') - - # Read lines - lines = fp.readlines() + with open(ctl_transform, 'rb') as fp: + lines = fp.readlines() # Grab transform ID and User Name transform_ID = lines[1][3:].split('<')[1].split('>')[1].strip() @@ -1539,7 +1535,6 @@ def get_transform_info(ctl_transform): transform_user_name_prefix = ( lines[2][3:].split('<')[1].split('>')[1].split('-')[0].strip()) # print(transformUserName) - fp.close() return transform_ID, transform_user_name, transform_user_name_prefix diff --git a/aces_1.0.0/python/aces_ocio/generate_lut.py b/aces_1.0.0/python/aces_ocio/generate_lut.py index 3328bb8..ac5a875 100644 --- a/aces_1.0.0/python/aces_ocio/generate_lut.py +++ b/aces_1.0.0/python/aces_ocio/generate_lut.py @@ -99,19 +99,18 @@ def write_SPI_1d(filename, from_min, from_max, data, entries, channels): Return value description. """ - f = file(filename, 'w') - f.write('Version 1\n') - f.write('From %f %f\n' % (from_min, from_max)) - f.write('Length %d\n' % entries) - f.write('Components %d\n' % (min(3, channels))) - f.write('{\n') - for i in range(0, entries): - entry = '' - for j in range(0, min(3, channels)): - entry = '%s %s' % (entry, data[i * channels + j]) - f.write(' %s\n' % entry) - f.write('}\n') - f.close() + with open(filename, 'w') as fp: + fp.write('Version 1\n') + fp.write('From %f %f\n' % (from_min, from_max)) + fp.write('Length %d\n' % entries) + fp.write('Components %d\n' % (min(3, channels))) + fp.write('{\n') + for i in range(0, entries): + entry = '' + for j in range(0, min(3, channels)): + entry = '%s %s' % (entry, data[i * channels + j]) + fp.write(' %s\n' % entry) + fp.write('}\n') def generate_1d_LUT_from_image(ramp_1d_path, diff --git a/aces_1.0.0/python/aces_ocio/process.py b/aces_1.0.0/python/aces_ocio/process.py index ad0b02e..47b968f 100755 --- a/aces_1.0.0/python/aces_ocio/process.py +++ b/aces_1.0.0/python/aces_ocio/process.py @@ -39,11 +39,9 @@ def read_text(text_file): Return value description. """ - if (text_file != ''): - fp = open(text_file, 'rb') - # Create a text/plain message - text = (fp.read()) - fp.close() + if text_file != '': + with open(text_file, 'rb') as fp: + text = (fp.read()) return text @@ -62,11 +60,9 @@ def write_text(text, text_file): Return value description. """ - if (text_file != ''): - fp = open(text_file, 'wb') - # Create a text/plain message - fp.write(text) - fp.close() + if text_file != '': + with open(text_file, 'wb') as fp: + fp.write(text) return text @@ -309,16 +305,14 @@ class Process: if log_filename: try: - # This also doesn't seem like the best structure... + # TODO: Review statements. # 3.1 try: - log_handle = open(log_filename, - mode='wt', - encoding='utf-8') + log_handle = ( + open(log_filename, mode='wt', encoding='utf-8')) # 2.6 except: - log_handle = open(log_filename, - mode='wt') + log_handle = open(log_filename, mode='wt') except: print('Couldn\'t open log : %s' % log_filename) log_handle = None diff --git a/aces_1.0.0/python/aces_ocio/utilities.py b/aces_1.0.0/python/aces_ocio/utilities.py index 00804e3..c60476a 100644 --- a/aces_1.0.0/python/aces_ocio/utilities.py +++ b/aces_1.0.0/python/aces_ocio/utilities.py @@ -20,11 +20,9 @@ __status__ = 'Production' __all__ = ['ColorSpace', 'mat44_from_mat33', 'filter_words', - 'files_walker'] + 'files_walker', + 'sanitize_path'] -# -# Utility classes and functions -# class ColorSpace(object): """ @@ -152,3 +150,21 @@ def files_walker(directory, filters_in=None, filters_out=None, flags=0): continue yield path + + +def sanitize_path(path): + """ + Object description. + + Parameters + ---------- + parameter : type + Parameter description. + + Returns + ------- + type + Return value description. + """ + + return path.replace(' ', '_').replace(')', '_').replace('(', '_') \ No newline at end of file -- 1.7.10.4