source: nagslang/yamlish.py@ 131:568476db7115

Last change on this file since 131:568476db7115 was 131:568476db7115, checked in by Stefano Rivera <stefano@…>, 7 years ago

Dump inline lists and dicts too

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