source: pyyaml/trunk/lib/yaml/__init__.py @ 314

Revision 314, 9.1 KB checked in by xi, 5 years ago (diff)

Bumped the version number.

RevLine 
[51]1
[57]2from error import *
[133]3
[51]4from tokens import *
5from events import *
[55]6from nodes import *
[51]7
[136]8from loader import *
9from dumper import *
[133]10
[314]11__version__ = '3.07'
[282]12
[223]13try:
14    from cyaml import *
[295]15    __with_libyaml__ = True
[223]16except ImportError:
[295]17    __with_libyaml__ = False
[223]18
[136]19def scan(stream, Loader=Loader):
20    """
21    Scan a YAML stream and produce scanning tokens.
22    """
23    loader = Loader(stream)
24    while loader.check_token():
25        yield loader.get_token()
[51]26
[136]27def parse(stream, Loader=Loader):
28    """
29    Parse a YAML stream and produce parsing events.
30    """
31    loader = Loader(stream)
32    while loader.check_event():
33        yield loader.get_event()
[53]34
[136]35def compose(stream, Loader=Loader):
36    """
37    Parse the first YAML document in a stream
38    and produce the corresponding representation tree.
39    """
40    loader = Loader(stream)
[258]41    return loader.get_single_node()
[133]42
[136]43def compose_all(stream, Loader=Loader):
44    """
45    Parse all YAML documents in a stream
46    and produce corresponsing representation trees.
47    """
48    loader = Loader(stream)
49    while loader.check_node():
50        yield loader.get_node()
[53]51
[258]52def load(stream, Loader=Loader):
53    """
54    Parse the first YAML document in a stream
55    and produce the corresponding Python object.
56    """
57    loader = Loader(stream)
58    return loader.get_single_data()
59
[136]60def load_all(stream, Loader=Loader):
61    """
62    Parse all YAML documents in a stream
63    and produce corresponding Python objects.
64    """
65    loader = Loader(stream)
66    while loader.check_data():
67        yield loader.get_data()
[133]68
[258]69def safe_load(stream):
[136]70    """
71    Parse the first YAML document in a stream
72    and produce the corresponding Python object.
[258]73    Resolve only basic YAML tags.
[136]74    """
[258]75    return load(stream, SafeLoader)
[136]76
77def safe_load_all(stream):
78    """
79    Parse all YAML documents in a stream
80    and produce corresponding Python objects.
81    Resolve only basic YAML tags.
82    """
83    return load_all(stream, SafeLoader)
84
85def emit(events, stream=None, Dumper=Dumper,
86        canonical=None, indent=None, width=None,
87        allow_unicode=None, line_break=None):
88    """
89    Emit YAML parsing events into a stream.
90    If stream is None, return the produced string instead.
91    """
92    getvalue = None
93    if stream is None:
[133]94        try:
95            from cStringIO import StringIO
96        except ImportError:
97            from StringIO import StringIO
[136]98        stream = StringIO()
99        getvalue = stream.getvalue
100    dumper = Dumper(stream, canonical=canonical, indent=indent, width=width,
101            allow_unicode=allow_unicode, line_break=line_break)
[133]102    for event in events:
[136]103        dumper.emit(event)
104    if getvalue:
105        return getvalue()
[133]106
[136]107def serialize_all(nodes, stream=None, Dumper=Dumper,
108        canonical=None, indent=None, width=None,
109        allow_unicode=None, line_break=None,
110        encoding='utf-8', explicit_start=None, explicit_end=None,
111        version=None, tags=None):
112    """
113    Serialize a sequence of representation trees into a YAML stream.
114    If stream is None, return the produced string instead.
115    """
116    getvalue = None
117    if stream is None:
[133]118        try:
119            from cStringIO import StringIO
120        except ImportError:
121            from StringIO import StringIO
[136]122        stream = StringIO()
123        getvalue = stream.getvalue
124    dumper = Dumper(stream, canonical=canonical, indent=indent, width=width,
125            allow_unicode=allow_unicode, line_break=line_break,
126            encoding=encoding, version=version, tags=tags,
127            explicit_start=explicit_start, explicit_end=explicit_end)
128    dumper.open()
129    for node in nodes:
130        dumper.serialize(node)
131    dumper.close()
132    if getvalue:
133        return getvalue()
[133]134
[136]135def serialize(node, stream=None, Dumper=Dumper, **kwds):
136    """
137    Serialize a representation tree into a YAML stream.
138    If stream is None, return the produced string instead.
139    """
140    return serialize_all([node], stream, Dumper=Dumper, **kwds)
[133]141
[136]142def dump_all(documents, stream=None, Dumper=Dumper,
[152]143        default_style=None, default_flow_style=None,
[136]144        canonical=None, indent=None, width=None,
145        allow_unicode=None, line_break=None,
146        encoding='utf-8', explicit_start=None, explicit_end=None,
147        version=None, tags=None):
148    """
149    Serialize a sequence of Python objects into a YAML stream.
150    If stream is None, return the produced string instead.
151    """
152    getvalue = None
153    if stream is None:
154        try:
155            from cStringIO import StringIO
156        except ImportError:
157            from StringIO import StringIO
158        stream = StringIO()
159        getvalue = stream.getvalue
[152]160    dumper = Dumper(stream, default_style=default_style,
161            default_flow_style=default_flow_style,
162            canonical=canonical, indent=indent, width=width,
[136]163            allow_unicode=allow_unicode, line_break=line_break,
164            encoding=encoding, version=version, tags=tags,
165            explicit_start=explicit_start, explicit_end=explicit_end)
166    dumper.open()
167    for data in documents:
168        dumper.represent(data)
169    dumper.close()
170    if getvalue:
171        return getvalue()
[133]172
[136]173def dump(data, stream=None, Dumper=Dumper, **kwds):
174    """
175    Serialize a Python object into a YAML stream.
176    If stream is None, return the produced string instead.
177    """
178    return dump_all([data], stream, Dumper=Dumper, **kwds)
[133]179
[136]180def safe_dump_all(documents, stream=None, **kwds):
181    """
182    Serialize a sequence of Python objects into a YAML stream.
183    Produce only basic YAML tags.
184    If stream is None, return the produced string instead.
185    """
186    return dump_all(documents, stream, Dumper=SafeDumper, **kwds)
187
188def safe_dump(data, stream=None, **kwds):
189    """
190    Serialize a Python object into a YAML stream.
191    Produce only basic YAML tags.
192    If stream is None, return the produced string instead.
193    """
194    return dump_all([data], stream, Dumper=SafeDumper, **kwds)
195
[153]196def add_implicit_resolver(tag, regexp, first=None,
[137]197        Loader=Loader, Dumper=Dumper):
[136]198    """
199    Add an implicit scalar detector.
200    If an implicit scalar value matches the given regexp,
201    the corresponding tag is assigned to the scalar.
202    first is a sequence of possible initial characters or None.
203    """
[137]204    Loader.add_implicit_resolver(tag, regexp, first)
205    Dumper.add_implicit_resolver(tag, regexp, first)
[136]206
[137]207def add_path_resolver(tag, path, kind=None, Loader=Loader, Dumper=Dumper):
[136]208    """
209    Add a path based resolver for the given tag.
210    A path is a list of keys that forms a path
211    to a node in the representation tree.
212    Keys can be string values, integers, or None.
213    """
[137]214    Loader.add_path_resolver(tag, path, kind)
215    Dumper.add_path_resolver(tag, path, kind)
[136]216
217def add_constructor(tag, constructor, Loader=Loader):
218    """
219    Add a constructor for the given tag.
220    Constructor is a function that accepts a Loader instance
221    and a node object and produces the corresponding Python object.
222    """
223    Loader.add_constructor(tag, constructor)
224
225def add_multi_constructor(tag_prefix, multi_constructor, Loader=Loader):
226    """
227    Add a multi-constructor for the given tag prefix.
228    Multi-constructor is called for a node if its tag starts with tag_prefix.
229    Multi-constructor accepts a Loader instance, a tag suffix,
230    and a node object and produces the corresponding Python object.
231    """
232    Loader.add_multi_constructor(tag_prefix, multi_constructor)
233
[137]234def add_representer(data_type, representer, Dumper=Dumper):
235    """
236    Add a representer for the given type.
237    Representer is a function accepting a Dumper instance
238    and an instance of the given data type
239    and producing the corresponding representation node.
240    """
241    Dumper.add_representer(data_type, representer)
242
[147]243def add_multi_representer(data_type, multi_representer, Dumper=Dumper):
244    """
245    Add a representer for the given type.
246    Multi-representer is a function accepting a Dumper instance
247    and an instance of the given data type or subtype
248    and producing the corresponding representation node.
249    """
250    Dumper.add_multi_representer(data_type, multi_representer)
251
[136]252class YAMLObjectMetaclass(type):
253    """
254    The metaclass for YAMLObject.
255    """
256    def __init__(cls, name, bases, kwds):
257        super(YAMLObjectMetaclass, cls).__init__(name, bases, kwds)
258        if 'yaml_tag' in kwds and kwds['yaml_tag'] is not None:
259            cls.yaml_loader.add_constructor(cls.yaml_tag, cls.from_yaml)
260            cls.yaml_dumper.add_representer(cls, cls.to_yaml)
261
262class YAMLObject(object):
263    """
264    An object that can dump itself to a YAML stream
265    and load itself from a YAML stream.
266    """
267
268    __metaclass__ = YAMLObjectMetaclass
[252]269    __slots__ = ()  # no direct instantiation, so allow immutable subclasses
[136]270
271    yaml_loader = Loader
272    yaml_dumper = Dumper
273
274    yaml_tag = None
275    yaml_flow_style = None
276
277    def from_yaml(cls, loader, node):
278        """
279        Convert a representation node to a Python object.
280        """
281        return loader.construct_yaml_object(node, cls)
282    from_yaml = classmethod(from_yaml)
283
284    def to_yaml(cls, dumper, data):
285        """
286        Convert a Python object to a representation node.
287        """
288        return dumper.represent_yaml_object(cls.yaml_tag, data, cls,
289                flow_style=cls.yaml_flow_style)
290    to_yaml = classmethod(to_yaml)
291
Note: See TracBrowser for help on using the repository browser.