source: nagslang/yamlish.py@ 136:0280ee006d95

Last change on this file since 136:0280ee006d95 was 136:0280ee006d95, checked in by Stefano Rivera <stefano@…>, 8 years ago

Support integer dict keys

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