Mercurial > piecrust2
diff 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 |
line wrap: on
line diff
--- a/piecrust/configuration.py Sat Aug 30 18:30:50 2014 -0700 +++ b/piecrust/configuration.py Sun Aug 31 23:48:18 2014 -0700 @@ -127,6 +127,20 @@ class OrderedDictYAMLLoader(yaml.SafeLoader): """ A YAML loader that loads mappings into ordered dictionaries. """ + def __init__(self, *args, **kwargs): + super(OrderedDictYAMLLoader, self).__init__(*args, **kwargs) + + self.add_constructor(u'tag:yaml.org,2002:map', + type(self).construct_yaml_map) + self.add_constructor(u'tag:yaml.org,2002:omap', + type(self).construct_yaml_map) + + def construct_yaml_map(self, node): + data = collections.OrderedDict() + yield data + value = self.construct_mapping(node) + data.update(value) + def construct_mapping(self, node, deep=False): if not isinstance(node, yaml.MappingNode): raise ConstructorError(None, None,