Ran update-copyright.py
[hooke.git] / hooke / util / convert.py
1 # Copyright (C) 2010-2012 W. Trevor King <wking@tremily.us>
2 #
3 # This file is part of Hooke.
4 #
5 # Hooke is free software: you can redistribute it and/or modify it under the
6 # terms of the GNU Lesser General Public License as published by the Free
7 # Software Foundation, either version 3 of the License, or (at your option) any
8 # later version.
9 #
10 # Hooke is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
13 # details.
14 #
15 # You should have received a copy of the GNU Lesser General Public License
16 # along with Hooke.  If not, see <http://www.gnu.org/licenses/>.
17
18 """Type conversion utilities.
19 """
20
21
22 ANALOGS = {
23     'file': 'string',
24     'path': 'string',
25     'point': 'int',
26     }
27 """Types that may be treated as other types.
28
29 These types may have optional special handling on the UI end
30 (e.g. file picker dialogs), but it is not required.
31 """
32
33 RAW_TYPES = [
34     'curve',
35     'dict',
36     'driver',
37     'function',
38     'object',
39     'playlist',
40     ]
41 """List of types that should not be converted.
42 """
43
44 def to_string(value, type, count=1):
45     """Convert `value` from `type` to a unicode string.
46     """
47     type = ANALOGS.get(type, type)
48     if type in RAW_TYPES:
49         return value
50     if count != 1:
51         values = [to_string(v, type) for v in value]
52         return '[%s]' % ', '.join(values)
53     return unicode(value)
54
55 def from_string(value, type, count=1):
56     """Convert `value` from a string to `type`.
57
58     Examples
59     --------
60     >>> from_string('abcde', type='string')
61     u'abcde'
62     >>> from_string('None', type='string')
63     >>> from_string(None, type='string')
64     >>> from_string('true', type='bool')
65     True
66     >>> from_string('false', type='bool')
67     False
68     >>> from_string(None, type='bool')
69     False
70     >>> from_string('123', type='int')
71     123
72     >>> from_string('123', type='float')
73     123.0
74     """
75     type = ANALOGS.get(type, type)
76     if type in RAW_TYPES:
77         return value
78     fn = globals()['_string_to_%s' % type]
79     if count != 1:
80         assert value.startswith('[') and value.endswith(']'), value
81         value = value[1:-1]  # strip off brackets
82         values = [from_string(v, type) for v in value.split(', ')]
83         assert count == -1 or len(values) == count, (
84             'array with %d != %d values: %s'
85             % (len(values), count, values))
86         return values
87     return fn(value)
88
89 def _string_to_string(value):
90     if value in [None, 'None'] or len(value) == 0:
91         return None
92     return unicode(value)
93
94 def _string_to_bool(value):
95     return hasattr(value, 'lower') and value.lower() == 'true'
96
97 def _string_to_int(value):
98     if value in [None, 'None']:
99         return None
100     return int(value)
101
102 def _string_to_float(value):
103     if value in [None, 'None']:
104         return None
105     return float(value)