cyaml.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. # coding: utf-8
  2. from _ruamel_yaml import CParser, CEmitter # type: ignore
  3. from ruamel.yaml.constructor import Constructor, BaseConstructor, SafeConstructor
  4. from ruamel.yaml.representer import Representer, SafeRepresenter, BaseRepresenter
  5. from ruamel.yaml.resolver import Resolver, BaseResolver
  6. from typing import Any, Union, Optional # NOQA
  7. from ruamel.yaml.compat import StreamTextType, StreamType, VersionType # NOQA
  8. __all__ = ['CBaseLoader', 'CSafeLoader', 'CLoader', 'CBaseDumper', 'CSafeDumper', 'CDumper']
  9. # this includes some hacks to solve the usage of resolver by lower level
  10. # parts of the parser
  11. class CBaseLoader(CParser, BaseConstructor, BaseResolver): # type: ignore
  12. def __init__(
  13. self,
  14. stream: StreamTextType,
  15. version: Optional[VersionType] = None,
  16. preserve_quotes: Optional[bool] = None,
  17. ) -> None:
  18. CParser.__init__(self, stream)
  19. self._parser = self._composer = self
  20. BaseConstructor.__init__(self, loader=self)
  21. BaseResolver.__init__(self, loadumper=self)
  22. # self.descend_resolver = self._resolver.descend_resolver
  23. # self.ascend_resolver = self._resolver.ascend_resolver
  24. # self.resolve = self._resolver.resolve
  25. class CSafeLoader(CParser, SafeConstructor, Resolver): # type: ignore
  26. def __init__(
  27. self,
  28. stream: StreamTextType,
  29. version: Optional[VersionType] = None,
  30. preserve_quotes: Optional[bool] = None,
  31. ) -> None:
  32. CParser.__init__(self, stream)
  33. self._parser = self._composer = self
  34. SafeConstructor.__init__(self, loader=self)
  35. Resolver.__init__(self, loadumper=self)
  36. # self.descend_resolver = self._resolver.descend_resolver
  37. # self.ascend_resolver = self._resolver.ascend_resolver
  38. # self.resolve = self._resolver.resolve
  39. class CLoader(CParser, Constructor, Resolver): # type: ignore
  40. def __init__(
  41. self,
  42. stream: StreamTextType,
  43. version: Optional[VersionType] = None,
  44. preserve_quotes: Optional[bool] = None,
  45. ) -> None:
  46. CParser.__init__(self, stream)
  47. self._parser = self._composer = self
  48. Constructor.__init__(self, loader=self)
  49. Resolver.__init__(self, loadumper=self)
  50. # self.descend_resolver = self._resolver.descend_resolver
  51. # self.ascend_resolver = self._resolver.ascend_resolver
  52. # self.resolve = self._resolver.resolve
  53. class CBaseDumper(CEmitter, BaseRepresenter, BaseResolver): # type: ignore
  54. def __init__(
  55. self: StreamType,
  56. stream: Any,
  57. default_style: Any = None,
  58. default_flow_style: Any = None,
  59. canonical: Optional[bool] = None,
  60. indent: Optional[int] = None,
  61. width: Optional[int] = None,
  62. allow_unicode: Optional[bool] = None,
  63. line_break: Any = None,
  64. encoding: Any = None,
  65. explicit_start: Optional[bool] = None,
  66. explicit_end: Optional[bool] = None,
  67. version: Any = None,
  68. tags: Any = None,
  69. block_seq_indent: Any = None,
  70. top_level_colon_align: Any = None,
  71. prefix_colon: Any = None,
  72. ) -> None:
  73. # NOQA
  74. CEmitter.__init__(
  75. self,
  76. stream,
  77. canonical=canonical,
  78. indent=indent,
  79. width=width,
  80. encoding=encoding,
  81. allow_unicode=allow_unicode,
  82. line_break=line_break,
  83. explicit_start=explicit_start,
  84. explicit_end=explicit_end,
  85. version=version,
  86. tags=tags,
  87. )
  88. self._emitter = self._serializer = self._representer = self
  89. BaseRepresenter.__init__(
  90. self,
  91. default_style=default_style,
  92. default_flow_style=default_flow_style,
  93. dumper=self,
  94. )
  95. BaseResolver.__init__(self, loadumper=self)
  96. class CSafeDumper(CEmitter, SafeRepresenter, Resolver): # type: ignore
  97. def __init__(
  98. self: StreamType,
  99. stream: Any,
  100. default_style: Any = None,
  101. default_flow_style: Any = None,
  102. canonical: Optional[bool] = None,
  103. indent: Optional[int] = None,
  104. width: Optional[int] = None,
  105. allow_unicode: Optional[bool] = None,
  106. line_break: Any = None,
  107. encoding: Any = None,
  108. explicit_start: Optional[bool] = None,
  109. explicit_end: Optional[bool] = None,
  110. version: Any = None,
  111. tags: Any = None,
  112. block_seq_indent: Any = None,
  113. top_level_colon_align: Any = None,
  114. prefix_colon: Any = None,
  115. ) -> None:
  116. # NOQA
  117. self._emitter = self._serializer = self._representer = self
  118. CEmitter.__init__(
  119. self,
  120. stream,
  121. canonical=canonical,
  122. indent=indent,
  123. width=width,
  124. encoding=encoding,
  125. allow_unicode=allow_unicode,
  126. line_break=line_break,
  127. explicit_start=explicit_start,
  128. explicit_end=explicit_end,
  129. version=version,
  130. tags=tags,
  131. )
  132. self._emitter = self._serializer = self._representer = self
  133. SafeRepresenter.__init__(
  134. self, default_style=default_style, default_flow_style=default_flow_style,
  135. )
  136. Resolver.__init__(self)
  137. class CDumper(CEmitter, Representer, Resolver): # type: ignore
  138. def __init__(
  139. self: StreamType,
  140. stream: Any,
  141. default_style: Any = None,
  142. default_flow_style: Any = None,
  143. canonical: Optional[bool] = None,
  144. indent: Optional[int] = None,
  145. width: Optional[int] = None,
  146. allow_unicode: Optional[bool] = None,
  147. line_break: Any = None,
  148. encoding: Any = None,
  149. explicit_start: Optional[bool] = None,
  150. explicit_end: Optional[bool] = None,
  151. version: Any = None,
  152. tags: Any = None,
  153. block_seq_indent: Any = None,
  154. top_level_colon_align: Any = None,
  155. prefix_colon: Any = None,
  156. ) -> None:
  157. # NOQA
  158. CEmitter.__init__(
  159. self,
  160. stream,
  161. canonical=canonical,
  162. indent=indent,
  163. width=width,
  164. encoding=encoding,
  165. allow_unicode=allow_unicode,
  166. line_break=line_break,
  167. explicit_start=explicit_start,
  168. explicit_end=explicit_end,
  169. version=version,
  170. tags=tags,
  171. )
  172. self._emitter = self._serializer = self._representer = self
  173. Representer.__init__(
  174. self, default_style=default_style, default_flow_style=default_flow_style,
  175. )
  176. Resolver.__init__(self)