__init__.py 9.6 KB

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