Implement usage of "with" statement on relevant IO operations.
authorThomas Mansencal <thomas.mansencal@gmail.com>
Mon, 19 Jan 2015 14:05:17 +0000 (15:05 +0100)
committerThomas Mansencal <thomas.mansencal@gmail.com>
Mon, 19 Jan 2015 14:05:17 +0000 (15:05 +0100)
aces_1.0.0/python/aces_ocio/create_aces_config.py
aces_1.0.0/python/aces_ocio/generate_lut.py
aces_1.0.0/python/aces_ocio/process.py
aces_1.0.0/python/aces_ocio/utilities.py

index 63b1314..44dc565 100755 (executable)
@@ -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
 
index 3328bb8..ac5a875 100644 (file)
@@ -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,
index ad0b02e..47b968f 100755 (executable)
@@ -39,11 +39,9 @@ def read_text(text_file):
          Return value description.\r
     """\r
 \r
-    if (text_file != ''):\r
-        fp = open(text_file, 'rb')\r
-        # Create a text/plain message\r
-        text = (fp.read())\r
-        fp.close()\r
+    if text_file != '':\r
+        with open(text_file, 'rb') as fp:\r
+            text = (fp.read())\r
     return text\r
 \r
 \r
@@ -62,11 +60,9 @@ def write_text(text, text_file):
          Return value description.\r
     """\r
 \r
-    if (text_file != ''):\r
-        fp = open(text_file, 'wb')\r
-        # Create a text/plain message\r
-        fp.write(text)\r
-        fp.close()\r
+    if text_file != '':\r
+        with open(text_file, 'wb') as fp:\r
+            fp.write(text)\r
     return text\r
 \r
 \r
@@ -309,16 +305,14 @@ class Process:
 \r
         if log_filename:\r
             try:\r
-                # This also doesn't seem like the best structure...\r
+                # TODO: Review statements.\r
                 # 3.1\r
                 try:\r
-                    log_handle = open(log_filename,\r
-                                      mode='wt',\r
-                                      encoding='utf-8')\r
+                    log_handle = (\r
+                        open(log_filename, mode='wt', encoding='utf-8'))\r
                 # 2.6\r
                 except:\r
-                    log_handle = open(log_filename,\r
-                                      mode='wt')\r
+                    log_handle = open(log_filename, mode='wt')\r
             except:\r
                 print('Couldn\'t open log : %s' % log_filename)\r
                 log_handle = None\r
index 00804e3..c60476a 100644 (file)
@@ -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