More cleanups to Setting/Argument type handling, mostly for count != 1.
[hooke.git] / hooke / util / convert.py
index 92e6b64bbe2d2a3e911784a30ba0b24ac7974f56..44955a574791a9fd04f6d717b2daae45a6960cdc 100644 (file)
@@ -14,6 +14,7 @@ CONVERT_FROM_STRING = {
 
 ANALOGS = {
     'file': 'string',
+    'path': 'string',
     'point': 'int',
     }
 """Types that may be treated as other types.
@@ -33,21 +34,32 @@ RAW_TYPES = [
 """List of types that should not be converted.
 """
 
-def to_string(value, type):
+def to_string(value, type, count=1):
     """Convert `value` from `type` to a unicode string.
     """
     type = ANALOGS.get(type, type)
     if type in RAW_TYPES:
         return value
+    if count != 1:
+        values = [to_string(v, type) for v in value]
+        return '[%s]' % ', '.join(values)
     return unicode(value)
 
-def from_string(value, type):
+def from_string(value, type, count=1):
     """Convert `value` from a string to `type`.
     """
     type = ANALOGS.get(type, type)
     if type in RAW_TYPES:
         return value
     fn = globals()['_string_to_%s' % type]
+    if count != 1:
+        assert value.startswith('[') and value.endswith(']'), value
+        value = value[1:-1]  # strip off brackets
+        values = [from_string(v, type) for v in value.split(', ')]
+        assert count == -1 or len(values) == count, (
+            'array with %d != %d values: %s'
+            % (len(values), count, values))
+        return values
     return fn(value)
 
 def _string_to_string(value):