summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxi <xi@18f92427-320e-0410-9341-c67f048884a3>2006-05-04 10:46:11 +0000
committerxi <xi@18f92427-320e-0410-9341-c67f048884a3>2006-05-04 10:46:11 +0000
commit01134b7d3c475a453ca26ebe320baee5b521decb (patch)
tree0b46a44d982c0c53127219f73a1a6afd7194adea
parentf696b00c560a41d4c267606c2647eef6a77a5aee (diff)
downloadpyyaml-01134b7d3c475a453ca26ebe320baee5b521decb.tar.gz
Add a way to override default style chosen by Representer: fix #9
git-svn-id: http://svn.pyyaml.org/pyyaml/trunk@152 18f92427-320e-0410-9341-c67f048884a3
-rw-r--r--lib/yaml/__init__.py5
-rw-r--r--lib/yaml/dumper.py12
-rw-r--r--lib/yaml/representer.py10
3 files changed, 22 insertions, 5 deletions
diff --git a/lib/yaml/__init__.py b/lib/yaml/__init__.py
index 22df18b..10f6f35 100644
--- a/lib/yaml/__init__.py
+++ b/lib/yaml/__init__.py
@@ -134,6 +134,7 @@ def serialize(node, stream=None, Dumper=Dumper, **kwds):
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,
@@ -150,7 +151,9 @@ def dump_all(documents, stream=None, Dumper=Dumper,
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)
diff --git a/lib/yaml/dumper.py b/lib/yaml/dumper.py
index 84c9ddd..355c1e2 100644
--- a/lib/yaml/dumper.py
+++ b/lib/yaml/dumper.py
@@ -9,6 +9,7 @@ from resolver import *
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,
@@ -19,12 +20,14 @@ class BaseDumper(Emitter, Serializer, BaseRepresenter, BaseResolver):
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,
@@ -35,12 +38,14 @@ class SafeDumper(Emitter, Serializer, SafeRepresenter, Resolver):
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,
@@ -51,6 +56,7 @@ class Dumper(Emitter, Serializer, Representer, Resolver):
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)
diff --git a/lib/yaml/representer.py b/lib/yaml/representer.py
index 685580e..cb37169 100644
--- a/lib/yaml/representer.py
+++ b/lib/yaml/representer.py
@@ -26,7 +26,9 @@ class BaseRepresenter:
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):
@@ -96,6 +98,8 @@ class BaseRepresenter:
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):
@@ -107,6 +111,8 @@ class BaseRepresenter:
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)
@@ -134,6 +140,8 @@ class BaseRepresenter:
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)