15 __with_libyaml__ = True
17 __with_libyaml__ = False
19 def scan(stream, Loader=Loader):
21 Scan a YAML stream and produce scanning tokens.
23 loader = Loader(stream)
25 while loader.check_token():
26 yield loader.get_token()
30 def parse(stream, Loader=Loader):
32 Parse a YAML stream and produce parsing events.
34 loader = Loader(stream)
36 while loader.check_event():
37 yield loader.get_event()
41 def compose(stream, Loader=Loader):
43 Parse the first YAML document in a stream
44 and produce the corresponding representation tree.
46 loader = Loader(stream)
48 return loader.get_single_node()
52 def compose_all(stream, Loader=Loader):
54 Parse all YAML documents in a stream
55 and produce corresponding representation trees.
57 loader = Loader(stream)
59 while loader.check_node():
60 yield loader.get_node()
64 def load(stream, Loader=Loader):
66 Parse the first YAML document in a stream
67 and produce the corresponding Python object.
69 loader = Loader(stream)
71 return loader.get_single_data()
75 def load_all(stream, Loader=Loader):
77 Parse all YAML documents in a stream
78 and produce corresponding Python objects.
80 loader = Loader(stream)
82 while loader.check_data():
83 yield loader.get_data()
87 def safe_load(stream):
89 Parse the first YAML document in a stream
90 and produce the corresponding Python object.
91 Resolve only basic YAML tags.
93 return load(stream, SafeLoader)
95 def safe_load_all(stream):
97 Parse all YAML documents in a stream
98 and produce corresponding Python objects.
99 Resolve only basic YAML tags.
101 return load_all(stream, SafeLoader)
103 def emit(events, stream=None, Dumper=Dumper,
104 canonical=None, indent=None, width=None,
105 allow_unicode=None, line_break=None):
107 Emit YAML parsing events into a stream.
108 If stream is None, return the produced string instead.
112 from StringIO import StringIO
114 getvalue = stream.getvalue
115 dumper = Dumper(stream, canonical=canonical, indent=indent, width=width,
116 allow_unicode=allow_unicode, line_break=line_break)
125 def serialize_all(nodes, stream=None, Dumper=Dumper,
126 canonical=None, indent=None, width=None,
127 allow_unicode=None, line_break=None,
128 encoding='utf-8', explicit_start=None, explicit_end=None,
129 version=None, tags=None):
131 Serialize a sequence of representation trees into a YAML stream.
132 If stream is None, return the produced string instead.
137 from StringIO import StringIO
139 from cStringIO import StringIO
141 getvalue = stream.getvalue
142 dumper = Dumper(stream, canonical=canonical, indent=indent, width=width,
143 allow_unicode=allow_unicode, line_break=line_break,
144 encoding=encoding, version=version, tags=tags,
145 explicit_start=explicit_start, explicit_end=explicit_end)
149 dumper.serialize(node)
156 def serialize(node, stream=None, Dumper=Dumper, **kwds):
158 Serialize a representation tree into a YAML stream.
159 If stream is None, return the produced string instead.
161 return serialize_all([node], stream, Dumper=Dumper, **kwds)
163 def dump_all(documents, stream=None, Dumper=Dumper,
164 default_style=None, default_flow_style=None,
165 canonical=None, indent=None, width=None,
166 allow_unicode=None, line_break=None,
167 encoding='utf-8', explicit_start=None, explicit_end=None,
168 version=None, tags=None):
170 Serialize a sequence of Python objects into a YAML stream.
171 If stream is None, return the produced string instead.
176 from StringIO import StringIO
178 from cStringIO import StringIO
180 getvalue = stream.getvalue
181 dumper = Dumper(stream, default_style=default_style,
182 default_flow_style=default_flow_style,
183 canonical=canonical, indent=indent, width=width,
184 allow_unicode=allow_unicode, line_break=line_break,
185 encoding=encoding, version=version, tags=tags,
186 explicit_start=explicit_start, explicit_end=explicit_end)
189 for data in documents:
190 dumper.represent(data)
197 def dump(data, stream=None, Dumper=Dumper, **kwds):
199 Serialize a Python object into a YAML stream.
200 If stream is None, return the produced string instead.
202 return dump_all([data], stream, Dumper=Dumper, **kwds)
204 def safe_dump_all(documents, stream=None, **kwds):
206 Serialize a sequence of Python objects into a YAML stream.
207 Produce only basic YAML tags.
208 If stream is None, return the produced string instead.
210 return dump_all(documents, stream, Dumper=SafeDumper, **kwds)
212 def safe_dump(data, stream=None, **kwds):
214 Serialize a Python object into a YAML stream.
215 Produce only basic YAML tags.
216 If stream is None, return the produced string instead.
218 return dump_all([data], stream, Dumper=SafeDumper, **kwds)
220 def add_implicit_resolver(tag, regexp, first=None,
221 Loader=Loader, Dumper=Dumper):
223 Add an implicit scalar detector.
224 If an implicit scalar value matches the given regexp,
225 the corresponding tag is assigned to the scalar.
226 first is a sequence of possible initial characters or None.
228 Loader.add_implicit_resolver(tag, regexp, first)
229 Dumper.add_implicit_resolver(tag, regexp, first)
231 def add_path_resolver(tag, path, kind=None, Loader=Loader, Dumper=Dumper):
233 Add a path based resolver for the given tag.
234 A path is a list of keys that forms a path
235 to a node in the representation tree.
236 Keys can be string values, integers, or None.
238 Loader.add_path_resolver(tag, path, kind)
239 Dumper.add_path_resolver(tag, path, kind)
241 def add_constructor(tag, constructor, Loader=Loader):
243 Add a constructor for the given tag.
244 Constructor is a function that accepts a Loader instance
245 and a node object and produces the corresponding Python object.
247 Loader.add_constructor(tag, constructor)
249 def add_multi_constructor(tag_prefix, multi_constructor, Loader=Loader):
251 Add a multi-constructor for the given tag prefix.
252 Multi-constructor is called for a node if its tag starts with tag_prefix.
253 Multi-constructor accepts a Loader instance, a tag suffix,
254 and a node object and produces the corresponding Python object.
256 Loader.add_multi_constructor(tag_prefix, multi_constructor)
258 def add_representer(data_type, representer, Dumper=Dumper):
260 Add a representer for the given type.
261 Representer is a function accepting a Dumper instance
262 and an instance of the given data type
263 and producing the corresponding representation node.
265 Dumper.add_representer(data_type, representer)
267 def add_multi_representer(data_type, multi_representer, Dumper=Dumper):
269 Add a representer for the given type.
270 Multi-representer is a function accepting a Dumper instance
271 and an instance of the given data type or subtype
272 and producing the corresponding representation node.
274 Dumper.add_multi_representer(data_type, multi_representer)
276 class YAMLObjectMetaclass(type):
278 The metaclass for YAMLObject.
280 def __init__(cls, name, bases, kwds):
281 super(YAMLObjectMetaclass, cls).__init__(name, bases, kwds)
282 if 'yaml_tag' in kwds and kwds['yaml_tag'] is not None:
283 cls.yaml_loader.add_constructor(cls.yaml_tag, cls.from_yaml)
284 cls.yaml_dumper.add_representer(cls, cls.to_yaml)
286 class YAMLObject(object):
288 An object that can dump itself to a YAML stream
289 and load itself from a YAML stream.
292 __metaclass__ = YAMLObjectMetaclass
293 __slots__ = () # no direct instantiation, so allow immutable subclasses
299 yaml_flow_style = None
301 def from_yaml(cls, loader, node):
303 Convert a representation node to a Python object.
305 return loader.construct_yaml_object(node, cls)
306 from_yaml = classmethod(from_yaml)
308 def to_yaml(cls, dumper, data):
310 Convert a Python object to a representation node.
312 return dumper.represent_yaml_object(cls.yaml_tag, data, cls,
313 flow_style=cls.yaml_flow_style)
314 to_yaml = classmethod(to_yaml)