18574: Adds key & value type checking. Improves code layout for readability.
[arvados.git] / sdk / python / arvados / _normalize_stream.py
index f1f6052cddb9aebff6e4c47652795b4a63f1995c..485c757e7fce34dda579185608f39bfe4911bd94 100644 (file)
@@ -8,14 +8,8 @@ from . import config
 import re
 
 def escape(path):
-    replacements = [
-        ('\t', '\\011'),
-        ('\n', '\\012'),
-        (' ', '\\040'),
-    ]
-    path = re.sub('\\\\([0-3][0-7][0-7])', lambda m: '\\134'+m.group(1), path)
-    for a, b in replacements:
-        path = path.replace(a, b)
+    path = re.sub('\\\\', lambda m: '\\134', path)
+    path = re.sub('[:\000-\040]', lambda m: "\\%03o" % ord(m.group(0)), path)
     return path
 
 def normalize_stream(stream_name, stream):
@@ -61,13 +55,13 @@ def normalize_stream(stream_name, stream):
                 if streamoffset == current_span[1]:
                     current_span[1] += segment.segment_size
                 else:
-                    stream_tokens.append("{0}:{1}:{2}".format(current_span[0], current_span[1] - current_span[0], fout))
+                    stream_tokens.append(u"{0}:{1}:{2}".format(current_span[0], current_span[1] - current_span[0], fout))
                     current_span = [streamoffset, streamoffset + segment.segment_size]
 
         if current_span is not None:
-            stream_tokens.append("{0}:{1}:{2}".format(current_span[0], current_span[1] - current_span[0], fout))
+            stream_tokens.append(u"{0}:{1}:{2}".format(current_span[0], current_span[1] - current_span[0], fout))
 
         if not stream[streamfile]:
-            stream_tokens.append("0:0:{0}".format(fout))
+            stream_tokens.append(u"0:0:{0}".format(fout))
 
     return stream_tokens