source: nagslang/yamlish.py@ 121:7d022648aa4e

Last change on this file since 121:7d022648aa4e was 121:7d022648aa4e, checked in by Stefano Rivera <stefano@…>, 8 years ago

Dump dicts in sorted order

File size: 5.5 KB
Line 
1'''
2Serializer and dumper for a simple, YAMLish format (actually a YAML subset).
3The top level object is a dict.
4lists and dicts can contain:
5 * lists, dicts,
6 * single line strings,
7 * ints, floats,
8 * True, False, and None
9'''
10
11import re
12
13
14def dump(data, file_object):
15 file_object.write(dump_s(data))
16
17
18def dump_s(data):
19 return Dumper().dump(data)
20
21
22def load(file_object):
23 yaml = file_object.read()
24 return load_s(yaml)
25
26
27def load_s(yaml):
28 return Parser().parse(yaml.strip())
29
30
31class Dumper(object):
32 def dump(self, data):
33 return '\n'.join(self._dump(data))
34
35 def _dump(self, data, indent=0):
36 for type_ in (list, dict, basestring, int, float):
37 if isinstance(data, type_):
38 f = getattr(self, '_dump_%s' % type_.__name__)
39 return f(data, indent)
40 if data in (True, False, None):
41 return self._dump_literal(data, indent)
42 raise NotImplementedError()
43
44 def _dump_list(self, data, indent):
45 output = []
46 for item in data:
47 dumped = self._dump(item, indent + 2)
48 dumped[0] = '%s- %s' % (' ' * indent, dumped[0][indent + 2:])
49 output += dumped
50 return output
51
52 def _dump_dict(self, data, indent):
53 output = []
54 for k, v in sorted(data.iteritems()):
55 output.append('%s%s:' % (' ' * indent, k))
56 if isinstance(v, dict):
57 output += self._dump(v, indent + 2)
58 elif isinstance(v, list):
59 output += self._dump(v, indent)
60 else:
61 value = self._dump(v)
62 assert len(value) == 1
63 output[-1] += ' %s' % value[0]
64 return output
65
66 def _dump_basestring(self, data, indent):
67 if data in ('true', 'false', 'null'):
68 data = "'%s'" % data
69 return [' ' * indent + data]
70
71 def _dump_int(self, data, indent):
72 return ['%s%i' % (' ' * indent, data)]
73
74 def _dump_float(self, data, indent):
75 return ['%s%f' % (' ' * indent, data)]
76
77 def _dump_literal(self, data, indent):
78 string = {
79 True: 'true',
80 False: 'false',
81 None: 'null',
82 }[data]
83 return [' ' * indent + string]
84
85
86class Parser(object):
87 _spaces_re = re.compile(r'^(\s*)(.*)')
88 _list_re = re.compile(r'^(-\s+)(.*)')
89 _dict_re = re.compile(r'^([^-:]+):\s?(.*)')
90
91 def __init__(self):
92 # Stack of (indent level, container object)
93 self._stack = [(0, {})]
94 # When a dict's value is a nested block, remember the key
95 self._parent_key = None
96
97 @property
98 def _indent(self):
99 return self._stack[-1][0]
100
101 @property
102 def _container(self):
103 return self._stack[-1][1]
104
105 @property
106 def _in_list(self):
107 return isinstance(self._container, list)
108
109 @property
110 def _in_dict(self):
111 return isinstance(self._container, dict)
112
113 def _push(self, container, indent=None):
114 in_list = self._in_list
115 assert in_list or self._parent_key
116
117 if indent is None:
118 indent = self._indent
119 self._stack.append((indent, container()))
120 if in_list:
121 self._stack[-2][1].append(self._container)
122 else:
123 self._stack[-2][1][self._parent_key] = self._container
124 self._parent_key = None
125
126 def parse(self, yaml):
127 for line in yaml.splitlines():
128 spaces, line = self._spaces_re.match(line).groups()
129
130 while len(spaces) < self._indent:
131 self._stack.pop()
132
133 lm = self._list_re.match(line)
134 dm = self._dict_re.match(line)
135 if len(spaces) == self._indent:
136 if lm and self._in_dict:
137 # Starting a list in a dict
138 self._push(list)
139 elif dm and self._in_list:
140 # Left an embedded list
141 self._stack.pop()
142
143 if len(spaces) > self._indent:
144 assert self._parent_key
145 if dm:
146 # Nested dict
147 self._push(dict, len(spaces))
148 elif lm:
149 # Over-indented list in a dict
150 self._push(list, len(spaces))
151
152 indent = self._indent
153 while lm and lm.group(2).startswith('- '):
154 # Nested lists
155 prefix, line = lm.groups()
156 indent += len(prefix)
157 self._push(list, indent)
158 lm = self._list_re.match(line)
159 del indent
160
161 if lm:
162 prefix, line = lm.groups()
163 dm = self._dict_re.match(line)
164 if dm:
165 self._push(dict, self._indent + len(prefix))
166 else:
167 assert self._in_list
168 self._container.append(self._parse_value(line))
169
170 if dm:
171 key, value = dm.groups()
172 assert self._in_dict
173 if value:
174 self._container[key] = self._parse_value(value)
175 else:
176 self._parent_key = key
177
178 return self._stack[0][1]
179
180 def _parse_value(self, value):
181 if value.startswith("'") and value.endswith("'"):
182 return value[1:-1]
183 if value == 'true':
184 return True
185 if value == 'false':
186 return False
187 if value == 'null':
188 return None
189 for type_ in (int, float):
190 try:
191 return type_(value)
192 except ValueError:
193 pass
194 return value
Note: See TracBrowser for help on using the repository browser.