comparison piecrust/configuration.py @ 81:d64e4703f5e6

Propertly create `OrderedDict`s when loading YAML.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 31 Aug 2014 23:48:18 -0700
parents d9e494df2a99
children 10fc9c8bf682
comparison
equal deleted inserted replaced
80:838a9dd0e23c 81:d64e4703f5e6
125 125
126 126
127 class OrderedDictYAMLLoader(yaml.SafeLoader): 127 class OrderedDictYAMLLoader(yaml.SafeLoader):
128 """ A YAML loader that loads mappings into ordered dictionaries. 128 """ A YAML loader that loads mappings into ordered dictionaries.
129 """ 129 """
130 def __init__(self, *args, **kwargs):
131 super(OrderedDictYAMLLoader, self).__init__(*args, **kwargs)
132
133 self.add_constructor(u'tag:yaml.org,2002:map',
134 type(self).construct_yaml_map)
135 self.add_constructor(u'tag:yaml.org,2002:omap',
136 type(self).construct_yaml_map)
137
138 def construct_yaml_map(self, node):
139 data = collections.OrderedDict()
140 yield data
141 value = self.construct_mapping(node)
142 data.update(value)
143
130 def construct_mapping(self, node, deep=False): 144 def construct_mapping(self, node, deep=False):
131 if not isinstance(node, yaml.MappingNode): 145 if not isinstance(node, yaml.MappingNode):
132 raise ConstructorError(None, None, 146 raise ConstructorError(None, None,
133 "expected a mapping node, but found %s" % node.id, 147 "expected a mapping node, but found %s" % node.id,
134 node.start_mark) 148 node.start_mark)