source: nagslang/yamlish.py@ 129:423266f0b9e4

Last change on this file since 129:423266f0b9e4 was 129:423266f0b9e4, checked in by Stefano Rivera <stefano@…>, 7 years ago

Support inline lists, dicts, etc. And alternative top level objects

File size: 6.6 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
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 _inline_list_re = re.compile(r"^([^',]+|''|'.+?[^'](?:'')*')(?:, (.*))?$")
91
92 def __init__(self):
93 # Stack of (indent level, container object)
94 self._stack = []
95 # When a dict's value is a nested block, remember the key
96 self._parent_key = None
97
98 @property
99 def _indent(self):
100 return self._stack[-1][0]
101
102 @property
103 def _container(self):
104 return self._stack[-1][1]
105
106 @property
107 def _in_list(self):
108 return isinstance(self._container, list)
109
110 @property
111 def _in_dict(self):
112 return isinstance(self._container, dict)
113
114 def _push(self, container, indent=None):
115 in_list = self._in_list
116 assert in_list or self._parent_key
117
118 if indent is None:
119 indent = self._indent
120 self._stack.append((indent, container()))
121 if in_list:
122 self._stack[-2][1].append(self._container)
123 else:
124 self._stack[-2][1][self._parent_key] = self._container
125 self._parent_key = None
126
127 def parse(self, yaml):
128 if yaml.startswith(('[', '{')):
129 return self._parse_value(yaml)
130
131 if yaml.startswith('-'):
132 self._stack.append((0, []))
133 else:
134 self._stack.append((0, {}))
135
136 for line in yaml.splitlines():
137 spaces, line = self._spaces_re.match(line).groups()
138
139 while len(spaces) < self._indent:
140 self._stack.pop()
141
142 lm = self._list_re.match(line)
143 dm = self._dict_re.match(line)
144 if len(spaces) == self._indent:
145 if lm and self._in_dict:
146 # Starting a list in a dict
147 self._push(list)
148 elif dm and self._in_list:
149 # Left an embedded list
150 self._stack.pop()
151
152 if len(spaces) > self._indent:
153 assert self._parent_key
154 if dm:
155 # Nested dict
156 self._push(dict, len(spaces))
157 elif lm:
158 # Over-indented list in a dict
159 self._push(list, len(spaces))
160
161 indent = self._indent
162 while lm and lm.group(2).startswith('- '):
163 # Nested lists
164 prefix, line = lm.groups()
165 indent += len(prefix)
166 self._push(list, indent)
167 lm = self._list_re.match(line)
168 del indent
169
170 if lm:
171 prefix, line = lm.groups()
172 dm = self._dict_re.match(line)
173 if dm:
174 self._push(dict, self._indent + len(prefix))
175 else:
176 assert self._in_list
177 self._container.append(self._parse_value(line))
178
179 if dm:
180 key, value = dm.groups()
181 assert self._in_dict
182 if value:
183 self._container[key] = self._parse_value(value)
184 else:
185 self._parent_key = key
186
187 return self._stack[0][1]
188
189 def _parse_value(self, value):
190 if value.startswith("'") and value.endswith("'"):
191 return value[1:-1]
192 if value.startswith('[') and value.endswith(']'):
193 value = value[1:-1]
194 output = []
195 while value:
196 m = self._inline_list_re.match(value)
197 assert m
198 output.append(self._parse_value(m.group(1)))
199 value = m.group(2)
200 return output
201 if value.startswith('{') and value.endswith('}'):
202 value = value[1:-1]
203 output = {}
204 while value:
205 key, value = value.split(': ', 1)
206 m = self._inline_list_re.match(value)
207 assert m
208 output[key] = self._parse_value(m.group(1))
209 value = m.group(2)
210 return output
211 if value.startswith('!!'):
212 raise NotImplemented()
213 if value == 'true':
214 return True
215 if value == 'false':
216 return False
217 if value == 'null':
218 return None
219 for type_ in (int, float):
220 try:
221 return type_(value)
222 except ValueError:
223 pass
224 return value
Note: See TracBrowser for help on using the repository browser.