changeset 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 838a9dd0e23c
children ae90caf26224
files piecrust/configuration.py
diffstat 1 files changed, 14 insertions(+), 0 deletions(-) [+]
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,