Fix allow_unicode (ticket:3).
[pyyaml/python3.git] / lib / yaml / __init__.py
blobe6a352631058f19b0acd8a055bcb7220bd97f462
2 from error import *
4 from reader import *
5 from scanner import *
6 from parser import *
7 from composer import *
8 from resolver import *
9 from constructor import *
11 from emitter import *
12 from serializer import *
13 from representer import *
15 from detector import *
17 from tokens import *
18 from events import *
19 from nodes import *
21 from yaml_object import *
23 def parse(data, Reader=Reader, Scanner=Scanner, Parser=Parser):
24 reader = Reader(data)
25 scanner = Scanner(reader)
26 parser = Parser(scanner)
27 return parser
29 def 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
39 def 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)
44 def load(data, *args, **kwds):
45 for document in load_all(data, *args, **kwds):
46 return document
48 def safe_load(data, *args, **kwds):
49 for document in safe_load_all(data, *args, **kwds):
50 return document
52 def 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()
68 def 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()
92 def 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)
99 def dump(native, *args, **kwds):
100 return dump_all([native], *args, **kwds)
102 def safe_dump(native, *args, **kwds):
103 return safe_dump_all([native], *args, **kwds)