Replace .config rather than reconstructing plugins, drivers, and UIs.
[hooke.git] / hooke / util / convert.py
index 44955a574791a9fd04f6d717b2daae45a6960cdc..f64bb46dc373ac1e2419a47d6b2cc27711caa442 100644 (file)
@@ -3,14 +3,6 @@
 """Type conversion utilities.
 """
 
-CONVERT_FROM_STRING = {
-    'string': lambda x: x,
-    'bool': lambda x: x == 'True',
-    'int': lambda x: int(x),
-    'float': lambda x: float(x),
-    }
-"""Functions converting strings to values, keyed by type.
-"""
 
 ANALOGS = {
     'file': 'string',
@@ -47,6 +39,23 @@ def to_string(value, type, count=1):
 
 def from_string(value, type, count=1):
     """Convert `value` from a string to `type`.
+
+    Examples
+    --------
+    >>> from_string('abcde', type='string')
+    u'abcde'
+    >>> from_string('None', type='string')
+    >>> from_string(None, type='string')
+    >>> from_string('true', type='bool')
+    True
+    >>> from_string('false', type='bool')
+    False
+    >>> from_string(None, type='bool')
+    False
+    >>> from_string('123', type='int')
+    123
+    >>> from_string('123', type='float')
+    123.0
     """
     type = ANALOGS.get(type, type)
     if type in RAW_TYPES:
@@ -63,13 +72,19 @@ def from_string(value, type, count=1):
     return fn(value)
 
 def _string_to_string(value):
+    if value in [None, 'None'] or len(value) == 0:
+        return None
     return unicode(value)
 
 def _string_to_bool(value):
-    return value == 'True'
+    return hasattr(value, 'lower') and value.lower() == 'true'
 
 def _string_to_int(value):
+    if value in [None, 'None']:
+        return None
     return int(value)
 
 def _string_to_float(value):
+    if value in [None, 'None']:
+        return None
     return float(value)