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

Revision 135, 3.1 KB checked in by xi, 8 years ago (diff)

Fix allow_unicode (ticket:3).

Line 
1
2from error import *
3
4from reader import *
5from scanner import *
6from parser import *
7from composer import *
8from resolver import *
9from constructor import *
10
11from emitter import *
12from serializer import *
13from representer import *
14
15from detector import *
16
17from tokens import *
18from events import *
19from nodes import *
20
21from yaml_object import *
22
23def parse(data, Reader=Reader, Scanner=Scanner, Parser=Parser):
24    reader = Reader(data)
25    scanner = Scanner(reader)
26    parser = Parser(scanner)
27    return parser
28
29def load_all(data, Reader=Reader, Scanner=Scanner, Parser=Parser,
30        Composer=Composer, Resolver=Resolver, Constructor=Constructor):
31    reader = Reader(data)
32    scanner = Scanner(reader)
33    parser = Parser(scanner)
34    composer = Composer(parser)
35    resolver = Resolver(composer)
36    constructor = Constructor(resolver)
37    return constructor
38
39def safe_load_all(data, Reader=Reader, Scanner=Scanner, Parser=Parser,
40        Composer=Composer, Resolver=Resolver, Constructor=SafeConstructor):
41    return load_all(data, Reader, Scanner, Parser, Composer, Resolver,
42            Constructor)
43
44def load(data, *args, **kwds):
45    for document in load_all(data, *args, **kwds):
46        return document
47
48def safe_load(data, *args, **kwds):
49    for document in safe_load_all(data, *args, **kwds):
50        return document
51
52def emit(events, writer=None, Emitter=Emitter):
53    if writer is None:
54        try:
55            from cStringIO import StringIO
56        except ImportError:
57            from StringIO import StringIO
58        writer = StringIO()
59        return_value = True
60    else:
61        return_value = False
62    emitter = Emitter(writer)
63    for event in events:
64        emitter.emit(event)
65    if return_value:
66        return writer.getvalue()
67
68def dump_all(natives, writer=None, Emitter=Emitter,
69        Serializer=Serializer, Representer=Representer,
70        encoding='utf-8', line_break=None, canonical=None,
71        indent=None, width=None, allow_unicode=None):
72    if writer is None:
73        try:
74            from cStringIO import StringIO
75        except ImportError:
76            from StringIO import StringIO
77        writer = StringIO()
78        return_value = True
79    else:
80        return_value = False
81    emitter = Emitter(writer)
82    serializer = Serializer(emitter, encoding=encoding, line_break=line_break,
83            canonical=canonical, indent=indent, width=width,
84            allow_unicode=allow_unicode)
85    representer = Representer(serializer)
86    for native in natives:
87        representer.represent(native)
88    representer.close()
89    if return_value:
90        return writer.getvalue()
91
92def safe_dump_all(natives, writer=None, Emitter=Emitter,
93        Serializer=Serializer, Representer=SafeRepresenter,
94        encoding='utf-8', line_break=None, canonical=None,
95        indent=None, width=None, allow_unicode=None):
96    return dump_all(natives, writer, Emitter, Serializer, Representer,
97            encoding, line_break, canonical, indent, width, allow_unicode)
98
99def dump(native, *args, **kwds):
100    return dump_all([native], *args, **kwds)
101
102def safe_dump(native, *args, **kwds):
103    return safe_dump_all([native], *args, **kwds)
104
Note: See TracBrowser for help on using the repository browser.