Changeset 119:b75de48a618c
- Timestamp:
- 09/02/13 13:27:51 (7 years ago)
- Branch:
- default
- Phase:
- public
- Rebase:
- 36323566343330333330326464383930636237313130623336306361313666623936323563613936
- Location:
- nagslang
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
nagslang/tests/test_yamlish.py
r114 r119 80 80 }) 81 81 82 def test_quoted(self): 83 # a literal true is True, but 'true' is a string 84 self.roundtrip({'foo': 'true'}) 85 86 def test_literals(self): 87 self.roundtrip({'foo': [True, False, None]}) 88 82 89 83 90 class TestFromPyYAML(TestRoundTrip): -
nagslang/yamlish.py
r113 r119 2 2 Serializer and dumper for a simple, YAMLish format (actually a YAML subset). 3 3 The top level object is a dict. 4 lists and dicts can contain lists, dicts, and strings. 4 lists and dicts can contain: 5 * lists, dicts, 6 * single line strings, 7 * True, False, and None 5 8 ''' 6 9 … … 34 37 f = getattr(self, '_dump_%s' % type_.__name__) 35 38 return f(data, indent) 39 if data in (True, False, None): 40 return self._dump_literal(data, indent) 36 41 raise NotImplementedError() 37 42 … … 59 64 60 65 def _dump_basestring(self, data, indent): 66 if data in ('true', 'false', 'null'): 67 data = "'%s'" % data 61 68 return [' ' * indent + data] 69 70 def _dump_literal(self, data, indent): 71 string = { 72 True: 'true', 73 False: 'false', 74 None: 'null', 75 }[data] 76 return [' ' * indent + string] 62 77 63 78 … … 144 159 else: 145 160 assert self._in_list 146 self._container.append( line)161 self._container.append(self._parse_value(line)) 147 162 148 163 if dm: 149 164 key, value = dm.groups() 165 assert self._in_dict 150 166 if value: 151 assert self._in_dict 152 self._container[key] = value 167 self._container[key] = self._parse_value(value) 153 168 else: 154 169 self._parent_key = key 155 170 156 171 return self._stack[0][1] 172 173 def _parse_value(self, value): 174 if value.startswith("'") and value.endswith("'"): 175 return value[1:-1] 176 if value == 'true': 177 return True 178 if value == 'false': 179 return False 180 if value == 'null': 181 return None 182 return value
Note:
See TracChangeset
for help on using the changeset viewer.