Anonymous avatar Anonymous committed 2473ab2

Add dictorder parameter to yaml.dump() and friends.

Comments (0)

Files changed (9)

lib/yaml/__init__.py

 
 def emit(events, stream=None, Dumper=Dumper,
         canonical=None, indent=None, width=None,
+        dictorder=None,
         allow_unicode=None, line_break=None):
     """
     Emit YAML parsing events into a stream.
         stream = StringIO()
         getvalue = stream.getvalue
     dumper = Dumper(stream, canonical=canonical, indent=indent, width=width,
+            dictorder=dictorder,
             allow_unicode=allow_unicode, line_break=line_break)
     try:
         for event in events:
 
 def serialize_all(nodes, stream=None, Dumper=Dumper,
         canonical=None, indent=None, width=None,
+        dictorder=None,
         allow_unicode=None, line_break=None,
         encoding='utf-8', explicit_start=None, explicit_end=None,
         version=None, tags=None):
         stream = StringIO()
         getvalue = stream.getvalue
     dumper = Dumper(stream, canonical=canonical, indent=indent, width=width,
+            dictorder=dictorder,
             allow_unicode=allow_unicode, line_break=line_break,
             encoding=encoding, version=version, tags=tags,
             explicit_start=explicit_start, explicit_end=explicit_end)
 def dump_all(documents, stream=None, Dumper=Dumper,
         default_style=None, default_flow_style=None,
         canonical=None, indent=None, width=None,
+        dictorder=None,
         allow_unicode=None, line_break=None,
         encoding='utf-8', explicit_start=None, explicit_end=None,
         version=None, tags=None):
     dumper = Dumper(stream, default_style=default_style,
             default_flow_style=default_flow_style,
             canonical=canonical, indent=indent, width=width,
+            dictorder=dictorder,
             allow_unicode=allow_unicode, line_break=line_break,
             encoding=encoding, version=version, tags=tags,
             explicit_start=explicit_start, explicit_end=explicit_end)

lib/yaml/dumper.py

     def __init__(self, stream,
             default_style=None, default_flow_style=None,
             canonical=None, indent=None, width=None,
+            dictorder=None,
             allow_unicode=None, line_break=None,
             encoding=None, explicit_start=None, explicit_end=None,
             version=None, tags=None):
         Emitter.__init__(self, stream, canonical=canonical,
                 indent=indent, width=width,
+                dictorder=dictorder,
                 allow_unicode=allow_unicode, line_break=line_break)
         Serializer.__init__(self, encoding=encoding,
                 explicit_start=explicit_start, explicit_end=explicit_end,
     def __init__(self, stream,
             default_style=None, default_flow_style=None,
             canonical=None, indent=None, width=None,
+            dictorder=None,
             allow_unicode=None, line_break=None,
             encoding=None, explicit_start=None, explicit_end=None,
             version=None, tags=None):
         Emitter.__init__(self, stream, canonical=canonical,
                 indent=indent, width=width,
+                dictorder=dictorder,
                 allow_unicode=allow_unicode, line_break=line_break)
         Serializer.__init__(self, encoding=encoding,
                 explicit_start=explicit_start, explicit_end=explicit_end,
                 version=version, tags=tags)
         SafeRepresenter.__init__(self, default_style=default_style,
-                default_flow_style=default_flow_style)
+                default_flow_style=default_flow_style,
+                dictorder=dictorder)
         Resolver.__init__(self)
 
 class Dumper(Emitter, Serializer, Representer, Resolver):
     def __init__(self, stream,
             default_style=None, default_flow_style=None,
             canonical=None, indent=None, width=None,
+            dictorder=None,
             allow_unicode=None, line_break=None,
             encoding=None, explicit_start=None, explicit_end=None,
             version=None, tags=None):
         Emitter.__init__(self, stream, canonical=canonical,
                 indent=indent, width=width,
+                dictorder=dictorder,
                 allow_unicode=allow_unicode, line_break=line_break)
         Serializer.__init__(self, encoding=encoding,
                 explicit_start=explicit_start, explicit_end=explicit_end,
                 version=version, tags=tags)
         Representer.__init__(self, default_style=default_style,
-                default_flow_style=default_flow_style)
+                default_flow_style=default_flow_style,
+                dictorder=dictorder)
         Resolver.__init__(self)
 

lib/yaml/emitter.py

     }
 
     def __init__(self, stream, canonical=None, indent=None, width=None,
-            allow_unicode=None, line_break=None):
+            dictorder=None, allow_unicode=None, line_break=None):
 
         # The stream should have the methods `write` and possibly `flush`.
         self.stream = stream
         # Formatting details.
         self.canonical = canonical
         self.allow_unicode = allow_unicode
+        self.dictorder = dictorder
         self.best_indent = 2
         if indent and 1 < indent < 10:
             self.best_indent = indent

lib/yaml/representer.py

     yaml_representers = {}
     yaml_multi_representers = {}
 
-    def __init__(self, default_style=None, default_flow_style=None):
+    def __init__(self, default_style=None, default_flow_style=None,
+            dictorder=None):
         self.default_style = default_style
         self.default_flow_style = default_flow_style
+        self.dictorder = dictorder
         self.represented_objects = {}
         self.object_keeper = []
         self.alias_key = None
                 node.flow_style = best_style
         return node
 
-    def represent_mapping(self, tag, mapping, flow_style=None):
+    def represent_mapping(self, tag, mapping, flow_style=None, dictorder=None):
         value = []
         node = MappingNode(tag, value, flow_style=flow_style)
         if self.alias_key is not None:
         if hasattr(mapping, 'items'):
             mapping = mapping.items()
             mapping.sort()
+        # dictorder must be a function that does an
+        # in-place sort of a list of pairs.
+            if dictorder:
+                dictorder(mapping)
         for item_key, item_value in mapping:
             node_key = self.represent_data(item_key)
             node_value = self.represent_data(item_value)
         #return SequenceNode(u'tag:yaml.org,2002:pairs', value)
 
     def represent_dict(self, data):
-        return self.represent_mapping(u'tag:yaml.org,2002:map', data)
+        return self.represent_mapping(u'tag:yaml.org,2002:map', data,
+            dictorder=self.dictorder)
 
     def represent_set(self, data):
         value = {}

lib3/yaml/__init__.py

 def dump_all(documents, stream=None, Dumper=Dumper,
         default_style=None, default_flow_style=None,
         canonical=None, indent=None, width=None,
+        dictorder=None,
         allow_unicode=None, line_break=None,
         encoding=None, explicit_start=None, explicit_end=None,
         version=None, tags=None):
     dumper = Dumper(stream, default_style=default_style,
             default_flow_style=default_flow_style,
             canonical=canonical, indent=indent, width=width,
+            dictorder=dictorder,
             allow_unicode=allow_unicode, line_break=line_break,
             encoding=encoding, version=version, tags=tags,
             explicit_start=explicit_start, explicit_end=explicit_end)

lib3/yaml/dumper.py

     def __init__(self, stream,
             default_style=None, default_flow_style=None,
             canonical=None, indent=None, width=None,
+            dictorder=None,
             allow_unicode=None, line_break=None,
             encoding=None, explicit_start=None, explicit_end=None,
             version=None, tags=None):
         Emitter.__init__(self, stream, canonical=canonical,
                 indent=indent, width=width,
+                dictorder=dictorder,
                 allow_unicode=allow_unicode, line_break=line_break)
         Serializer.__init__(self, encoding=encoding,
                 explicit_start=explicit_start, explicit_end=explicit_end,
     def __init__(self, stream,
             default_style=None, default_flow_style=None,
             canonical=None, indent=None, width=None,
+            dictorder=None,
             allow_unicode=None, line_break=None,
             encoding=None, explicit_start=None, explicit_end=None,
             version=None, tags=None):
         Emitter.__init__(self, stream, canonical=canonical,
                 indent=indent, width=width,
+                dictorder=dictorder,
                 allow_unicode=allow_unicode, line_break=line_break)
         Serializer.__init__(self, encoding=encoding,
                 explicit_start=explicit_start, explicit_end=explicit_end,
     def __init__(self, stream,
             default_style=None, default_flow_style=None,
             canonical=None, indent=None, width=None,
+            dictorder=None,
             allow_unicode=None, line_break=None,
             encoding=None, explicit_start=None, explicit_end=None,
             version=None, tags=None):
         Emitter.__init__(self, stream, canonical=canonical,
                 indent=indent, width=width,
+                dictorder=dictorder,
                 allow_unicode=allow_unicode, line_break=line_break)
         Serializer.__init__(self, encoding=encoding,
                 explicit_start=explicit_start, explicit_end=explicit_end,
                 version=version, tags=tags)
         Representer.__init__(self, default_style=default_style,
-                default_flow_style=default_flow_style)
+                default_flow_style=default_flow_style,
+                dictorder=dictorder)
         Resolver.__init__(self)
 

lib3/yaml/emitter.py

     }
 
     def __init__(self, stream, canonical=None, indent=None, width=None,
-            allow_unicode=None, line_break=None):
+            allow_unicode=None, line_break=None, dictorder=None):
 
         # The stream should have the methods `write` and possibly `flush`.
         self.stream = stream
         # Formatting details.
         self.canonical = canonical
         self.allow_unicode = allow_unicode
+        self.dictorder = dictorder
         self.best_indent = 2
         if indent and 1 < indent < 10:
             self.best_indent = indent

lib3/yaml/representer.py

     yaml_representers = {}
     yaml_multi_representers = {}
 
-    def __init__(self, default_style=None, default_flow_style=None):
+    def __init__(self, default_style=None, default_flow_style=None,
+            dictorder=None):
         self.default_style = default_style
         self.default_flow_style = default_flow_style
+        self.dictorder = dictorder
         self.represented_objects = {}
         self.object_keeper = []
         self.alias_key = None
                 node.flow_style = best_style
         return node
 
-    def represent_mapping(self, tag, mapping, flow_style=None):
+    def represent_mapping(self, tag, mapping, flow_style=None, dictorder=None):
         value = []
         node = MappingNode(tag, value, flow_style=flow_style)
         if self.alias_key is not None:
                 mapping = sorted(mapping)
             except TypeError:
                 pass
+            if dictorder:
+                dictorder(mapping)
         for item_key, item_value in mapping:
             node_key = self.represent_data(item_key)
             node_value = self.represent_data(item_value)
         #return SequenceNode(u'tag:yaml.org,2002:pairs', value)
 
     def represent_dict(self, data):
-        return self.represent_mapping('tag:yaml.org,2002:map', data)
+        return self.represent_mapping('tag:yaml.org,2002:map', data,
+            dictorder=self.dictorder)
 
     def represent_set(self, data):
         value = {}

tests/lib/test_yaml.py

 
 from test_mark import *
+from test_dictorder import *
 from test_reader import *
 from test_canonical import *
 from test_tokens import *
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.