Kirill Simonov avatar Kirill Simonov committed fd0d5c1

Add a way to override default style chosen by Representer: fix #9

Comments (0)

Files changed (3)

lib/yaml/__init__.py

     return serialize_all([node], stream, Dumper=Dumper, **kwds)
 
 def dump_all(documents, stream=None, Dumper=Dumper,
+        default_style=None, default_flow_style=None,
         canonical=None, indent=None, width=None,
         allow_unicode=None, line_break=None,
         encoding='utf-8', explicit_start=None, explicit_end=None,
             from StringIO import StringIO
         stream = StringIO()
         getvalue = stream.getvalue
-    dumper = Dumper(stream, canonical=canonical, indent=indent, width=width,
+    dumper = Dumper(stream, default_style=default_style,
+            default_flow_style=default_flow_style,
+            canonical=canonical, indent=indent, width=width,
             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

 class BaseDumper(Emitter, Serializer, BaseRepresenter, BaseResolver):
 
     def __init__(self, stream,
+            default_style=None, default_flow_style=None,
             canonical=None, indent=None, width=None,
             allow_unicode=None, line_break=None,
             encoding=None, explicit_start=None, explicit_end=None,
         Serializer.__init__(self, encoding=encoding,
                 explicit_start=explicit_start, explicit_end=explicit_end,
                 version=version, tags=tags)
-        Representer.__init__(self)
+        Representer.__init__(self, default_style=default_style,
+                default_flow_style=default_flow_style)
         Resolver.__init__(self)
 
 class SafeDumper(Emitter, Serializer, SafeRepresenter, Resolver):
 
     def __init__(self, stream,
+            default_style=None, default_flow_style=None,
             canonical=None, indent=None, width=None,
             allow_unicode=None, line_break=None,
             encoding=None, explicit_start=None, explicit_end=None,
         Serializer.__init__(self, encoding=encoding,
                 explicit_start=explicit_start, explicit_end=explicit_end,
                 version=version, tags=tags)
-        SafeRepresenter.__init__(self)
+        SafeRepresenter.__init__(self, default_style=default_style,
+                default_flow_style=default_flow_style)
         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,
             allow_unicode=None, line_break=None,
             encoding=None, explicit_start=None, explicit_end=None,
         Serializer.__init__(self, encoding=encoding,
                 explicit_start=explicit_start, explicit_end=explicit_end,
                 version=version, tags=tags)
-        Representer.__init__(self)
+        Representer.__init__(self, default_style=default_style,
+                default_flow_style=default_flow_style)
         Resolver.__init__(self)
 

lib/yaml/representer.py

     yaml_representers = {}
     yaml_multi_representers = {}
 
-    def __init__(self):
+    def __init__(self, default_style=None, default_flow_style=None):
+        self.default_style = default_style
+        self.default_flow_style = default_flow_style
         self.represented_objects = {}
 
     def represent(self, data):
     add_multi_representer = classmethod(add_multi_representer)
 
     def represent_scalar(self, tag, value, style=None):
+        if style is None:
+            style = self.default_style
         return ScalarNode(tag, value, style=style)
 
     def represent_sequence(self, tag, sequence, flow_style=None):
                 best_style = False
             value.append(self.represent_data(item))
         if flow_style is None:
+            flow_style = self.default_flow_style
+        if flow_style is None:
             flow_style = best_style
         return SequenceNode(tag, value, flow_style=flow_style)
 
                     best_style = False
                 value.append((node_key, node_value))
         if flow_style is None:
+            flow_style = self.default_flow_style
+        if flow_style is None:
             flow_style = best_style
         return MappingNode(tag, value, flow_style=flow_style)
 
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.