11 def scan(stream
, Loader
=Loader
):
13 Scan a YAML stream and produce scanning tokens.
15 loader
= Loader(stream
)
16 while loader
.check_token():
17 yield loader
.get_token()
19 def parse(stream
, Loader
=Loader
):
21 Parse a YAML stream and produce parsing events.
23 loader
= Loader(stream
)
24 while loader
.check_event():
25 yield loader
.get_event()
27 def compose(stream
, Loader
=Loader
):
29 Parse the first YAML document in a stream
30 and produce the corresponding representation tree.
32 loader
= Loader(stream
)
33 if loader
.check_node():
34 return loader
.get_node()
36 def compose_all(stream
, Loader
=Loader
):
38 Parse all YAML documents in a stream
39 and produce corresponsing representation trees.
41 loader
= Loader(stream
)
42 while loader
.check_node():
43 yield loader
.get_node()
45 def load_all(stream
, Loader
=Loader
):
47 Parse all YAML documents in a stream
48 and produce corresponding Python objects.
50 loader
= Loader(stream
)
51 while loader
.check_data():
52 yield loader
.get_data()
54 def load(stream
, Loader
=Loader
):
56 Parse the first YAML document in a stream
57 and produce the corresponding Python object.
59 loader
= Loader(stream
)
60 if loader
.check_data():
61 return loader
.get_data()
63 def safe_load_all(stream
):
65 Parse all YAML documents in a stream
66 and produce corresponding Python objects.
67 Resolve only basic YAML tags.
69 return load_all(stream
, SafeLoader
)
71 def safe_load(stream
):
73 Parse the first YAML document in a stream
74 and produce the corresponding Python object.
75 Resolve only basic YAML tags.
77 return load(stream
, SafeLoader
)
79 def emit(events
, stream
=None, Dumper
=Dumper
,
80 canonical
=None, indent
=None, width
=None,
81 allow_unicode
=None, line_break
=None):
83 Emit YAML parsing events into a stream.
84 If stream is None, return the produced string instead.
89 from cStringIO
import StringIO
91 from StringIO
import StringIO
93 getvalue
= stream
.getvalue
94 dumper
= Dumper(stream
, canonical
=canonical
, indent
=indent
, width
=width
,
95 allow_unicode
=allow_unicode
, line_break
=line_break
)
101 def serialize_all(nodes
, stream
=None, Dumper
=Dumper
,
102 canonical
=None, indent
=None, width
=None,
103 allow_unicode
=None, line_break
=None,
104 encoding
='utf-8', explicit_start
=None, explicit_end
=None,
105 version
=None, tags
=None):
107 Serialize a sequence of representation trees into a YAML stream.
108 If stream is None, return the produced string instead.
113 from cStringIO
import StringIO
115 from StringIO
import StringIO
117 getvalue
= stream
.getvalue
118 dumper
= Dumper(stream
, canonical
=canonical
, indent
=indent
, width
=width
,
119 allow_unicode
=allow_unicode
, line_break
=line_break
,
120 encoding
=encoding
, version
=version
, tags
=tags
,
121 explicit_start
=explicit_start
, explicit_end
=explicit_end
)
124 dumper
.serialize(node
)
129 def serialize(node
, stream
=None, Dumper
=Dumper
, **kwds
):
131 Serialize a representation tree into a YAML stream.
132 If stream is None, return the produced string instead.
134 return serialize_all([node
], stream
, Dumper
=Dumper
, **kwds
)
136 def dump_all(documents
, stream
=None, Dumper
=Dumper
,
137 canonical
=None, indent
=None, width
=None,
138 allow_unicode
=None, line_break
=None,
139 encoding
='utf-8', explicit_start
=None, explicit_end
=None,
140 version
=None, tags
=None):
142 Serialize a sequence of Python objects into a YAML stream.
143 If stream is None, return the produced string instead.
148 from cStringIO
import StringIO
150 from StringIO
import StringIO
152 getvalue
= stream
.getvalue
153 dumper
= Dumper(stream
, canonical
=canonical
, indent
=indent
, width
=width
,
154 allow_unicode
=allow_unicode
, line_break
=line_break
,
155 encoding
=encoding
, version
=version
, tags
=tags
,
156 explicit_start
=explicit_start
, explicit_end
=explicit_end
)
158 for data
in documents
:
159 dumper
.represent(data
)
164 def dump(data
, stream
=None, Dumper
=Dumper
, **kwds
):
166 Serialize a Python object into a YAML stream.
167 If stream is None, return the produced string instead.
169 return dump_all([data
], stream
, Dumper
=Dumper
, **kwds
)
171 def safe_dump_all(documents
, stream
=None, **kwds
):
173 Serialize a sequence of Python objects into a YAML stream.
174 Produce only basic YAML tags.
175 If stream is None, return the produced string instead.
177 return dump_all(documents
, stream
, Dumper
=SafeDumper
, **kwds
)
179 def safe_dump(data
, stream
=None, **kwds
):
181 Serialize a Python object into a YAML stream.
182 Produce only basic YAML tags.
183 If stream is None, return the produced string instead.
185 return dump_all([data
], stream
, Dumper
=SafeDumper
, **kwds
)
187 def add_implicit_detector(tag
, regexp
, first
=None,
188 Loader
=Loader
, Dumper
=Dumper
):
190 Add an implicit scalar detector.
191 If an implicit scalar value matches the given regexp,
192 the corresponding tag is assigned to the scalar.
193 first is a sequence of possible initial characters or None.
195 Loader
.add_implicit_resolver(tag
, regexp
, first
)
196 Dumper
.add_implicit_resolver(tag
, regexp
, first
)
198 def add_path_resolver(tag
, path
, kind
=None, Loader
=Loader
, Dumper
=Dumper
):
200 Add a path based resolver for the given tag.
201 A path is a list of keys that forms a path
202 to a node in the representation tree.
203 Keys can be string values, integers, or None.
205 Loader
.add_path_resolver(tag
, path
, kind
)
206 Dumper
.add_path_resolver(tag
, path
, kind
)
208 def add_constructor(tag
, constructor
, Loader
=Loader
):
210 Add a constructor for the given tag.
211 Constructor is a function that accepts a Loader instance
212 and a node object and produces the corresponding Python object.
214 Loader
.add_constructor(tag
, constructor
)
216 def add_multi_constructor(tag_prefix
, multi_constructor
, Loader
=Loader
):
218 Add a multi-constructor for the given tag prefix.
219 Multi-constructor is called for a node if its tag starts with tag_prefix.
220 Multi-constructor accepts a Loader instance, a tag suffix,
221 and a node object and produces the corresponding Python object.
223 Loader
.add_multi_constructor(tag_prefix
, multi_constructor
)
225 def add_representer(data_type
, representer
, Dumper
=Dumper
):
227 Add a representer for the given type.
228 Representer is a function accepting a Dumper instance
229 and an instance of the given data type
230 and producing the corresponding representation node.
232 Dumper
.add_representer(data_type
, representer
)
234 class YAMLObjectMetaclass(type):
236 The metaclass for YAMLObject.
238 def __init__(cls
, name
, bases
, kwds
):
239 super(YAMLObjectMetaclass
, cls
).__init
__(name
, bases
, kwds
)
240 if 'yaml_tag' in kwds
and kwds
['yaml_tag'] is not None:
241 cls
.yaml_loader
.add_constructor(cls
.yaml_tag
, cls
.from_yaml
)
242 cls
.yaml_dumper
.add_representer(cls
, cls
.to_yaml
)
244 class YAMLObject(object):
246 An object that can dump itself to a YAML stream
247 and load itself from a YAML stream.
250 __metaclass__
= YAMLObjectMetaclass
256 yaml_flow_style
= None
258 def from_yaml(cls
, loader
, node
):
260 Convert a representation node to a Python object.
262 return loader
.construct_yaml_object(node
, cls
)
263 from_yaml
= classmethod(from_yaml
)
265 def to_yaml(cls
, dumper
, data
):
267 Convert a Python object to a representation node.
269 return dumper
.represent_yaml_object(cls
.yaml_tag
, data
, cls
,
270 flow_style
=cls
.yaml_flow_style
)
271 to_yaml
= classmethod(to_yaml
)