PyYAML 6.0.4__cp315-cp315-win_arm64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
_yaml/__init__.py ADDED
@@ -0,0 +1,33 @@
1
+ # This is a stub package designed to roughly emulate the _yaml
2
+ # extension module, which previously existed as a standalone module
3
+ # and has been moved into the `yaml` package namespace.
4
+ # It does not perfectly mimic its old counterpart, but should get
5
+ # close enough for anyone who's relying on it even when they shouldn't.
6
+ import yaml
7
+
8
+ # in some circumstances, the yaml module we imoprted may be from a different version, so we need
9
+ # to tread carefully when poking at it here (it may not have the attributes we expect)
10
+ if not getattr(yaml, '__with_libyaml__', False):
11
+ from sys import version_info
12
+
13
+ exc = ModuleNotFoundError if version_info >= (3, 6) else ImportError
14
+ raise exc("No module named '_yaml'")
15
+ else:
16
+ from yaml._yaml import *
17
+ import warnings
18
+ warnings.warn(
19
+ 'The _yaml extension module is now located at yaml._yaml'
20
+ ' and its location is subject to change. To use the'
21
+ ' LibYAML-based parser and emitter, import from `yaml`:'
22
+ ' `from yaml import CLoader as Loader, CDumper as Dumper`.',
23
+ DeprecationWarning
24
+ )
25
+ del warnings
26
+ # Don't `del yaml` here because yaml is actually an existing
27
+ # namespace member of _yaml.
28
+
29
+ __name__ = '_yaml'
30
+ # If the module is top-level (i.e. not a part of any specific package)
31
+ # then the attribute should be set to ''.
32
+ # https://docs.python.org/3.8/library/types.html
33
+ __package__ = ''
@@ -0,0 +1,58 @@
1
+ Metadata-Version: 2.4
2
+ Name: PyYAML
3
+ Version: 6.0.4
4
+ Summary: YAML parser and emitter for Python
5
+ Home-page: https://pyyaml.org/
6
+ Download-URL: https://pypi.org/project/PyYAML/
7
+ Author: Kirill Simonov
8
+ Author-email: xi@resolvent.net
9
+ License: MIT
10
+ Project-URL: Bug Tracker, https://github.com/yaml/pyyaml/issues
11
+ Project-URL: CI, https://github.com/yaml/pyyaml/actions
12
+ Project-URL: Documentation, https://pyyaml.org/wiki/PyYAMLDocumentation
13
+ Project-URL: Mailing lists, http://lists.sourceforge.net/lists/listinfo/yaml-core
14
+ Project-URL: Source Code, https://github.com/yaml/pyyaml
15
+ Platform: Any
16
+ Classifier: Development Status :: 5 - Production/Stable
17
+ Classifier: Intended Audience :: Developers
18
+ Classifier: License :: OSI Approved :: MIT License
19
+ Classifier: Operating System :: OS Independent
20
+ Classifier: Programming Language :: Cython
21
+ Classifier: Programming Language :: Python
22
+ Classifier: Programming Language :: Python :: 3
23
+ Classifier: Programming Language :: Python :: 3.10
24
+ Classifier: Programming Language :: Python :: 3.11
25
+ Classifier: Programming Language :: Python :: 3.12
26
+ Classifier: Programming Language :: Python :: 3.13
27
+ Classifier: Programming Language :: Python :: 3.14
28
+ Classifier: Programming Language :: Python :: 3.15
29
+ Classifier: Programming Language :: Python :: Implementation :: CPython
30
+ Classifier: Programming Language :: Python :: Implementation :: PyPy
31
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
32
+ Classifier: Topic :: Text Processing :: Markup
33
+ Requires-Python: >=3.10
34
+ License-File: LICENSE
35
+ Dynamic: author
36
+ Dynamic: author-email
37
+ Dynamic: classifier
38
+ Dynamic: description
39
+ Dynamic: download-url
40
+ Dynamic: home-page
41
+ Dynamic: license
42
+ Dynamic: license-file
43
+ Dynamic: platform
44
+ Dynamic: project-url
45
+ Dynamic: requires-python
46
+ Dynamic: summary
47
+
48
+ YAML is a data serialization format designed for human readability
49
+ and interaction with scripting languages. PyYAML is a YAML parser
50
+ and emitter for Python.
51
+
52
+ PyYAML features a complete YAML 1.1 parser, Unicode support, pickle
53
+ support, capable extension API, and sensible error messages. PyYAML
54
+ supports standard YAML tags and provides Python-specific tags that
55
+ allow to represent an arbitrary Python object.
56
+
57
+ PyYAML is applicable for a broad range of tasks from complex
58
+ configuration files to object serialization and persistence.
@@ -0,0 +1,24 @@
1
+ _yaml/__init__.py,sha256=04Ae_5osxahpJHa3XBZUAf4wi6XX32gR8D6X6p64GEA,1402
2
+ pyyaml-6.0.4.dist-info/licenses/LICENSE,sha256=jTko-dxEkP1jVwfLiOsmvXZBAqcoKVQwfT5RZ6V36KQ,1101
3
+ yaml/__init__.py,sha256=wwM6uPQZf9BE7VXSCvw_RXc8GK-_ZtheGZc4alyRGoY,12311
4
+ yaml/_yaml.cp315-win_arm64.pyd,sha256=lvkKh32SmINENXaBom2ukd9am3ujx6jIJBLuYDugThA,284672
5
+ yaml/composer.py,sha256=_Ko30Wr6eDWUeUpauUGT3Lcg9QPBnOPVlTnIMRGJ9FM,4883
6
+ yaml/constructor.py,sha256=qq1UTZFjTSwbi32JZCGEgpmU_0KaXqU5sJEfoD2V1MU,28944
7
+ yaml/cyaml.py,sha256=6ZrAG9fAYvdVe2FK_w0hmXoG7ZYsoYUwapG8CiC72H0,3851
8
+ yaml/dumper.py,sha256=PLctZlYwZLp7XmeUdwRuv4nYOZ2UBnDIUy8-lKfLF-o,2837
9
+ yaml/emitter.py,sha256=jghtaU7eFwg31bG0B7RZea_29Adi9CKmXq_QjgQpCkQ,43006
10
+ yaml/error.py,sha256=Ah9z-toHJUbE9j-M8YpxgSRM5CgLCcwVzJgLLRF2Fxo,2533
11
+ yaml/events.py,sha256=50_TksgQiE4up-lKo_V-nBy-tAIxkIPQxY5qDhKCeHw,2445
12
+ yaml/loader.py,sha256=UVa-zIqmkFSCIYq_PgSGm4NSJttHY2Rf_zQ4_b1fHN0,2061
13
+ yaml/nodes.py,sha256=gPKNj8pKCdh2d4gr3gIYINnPOaOxGhJAUiYhGRnPE84,1440
14
+ yaml/parser.py,sha256=ilWp5vvgoHFGzvOZDItFoGjD6D42nhlZrZyjAwa0oJo,25495
15
+ yaml/reader.py,sha256=0dmzirOiDG4Xo41RnuQS7K9rkY3xjHiVasfDMNTqCNw,6794
16
+ yaml/representer.py,sha256=IuWP-cAW9sHKEnS0gCqSa894k1Bg4cgTxaDwIcbRQ-Y,14190
17
+ yaml/resolver.py,sha256=9L-VYfm4mWHxUD1Vg4X7rjDRK_7VZd6b92wzq7Y2IKY,9004
18
+ yaml/scanner.py,sha256=YEM3iLZSaQwXcQRg2l2R4MdT0zGP2F9eHkKGKnHyWQY,51279
19
+ yaml/serializer.py,sha256=ChuFgmhU01hj4xgI8GaKv6vfM2Bujwa9i7d2FAHj7cA,4165
20
+ yaml/tokens.py,sha256=lTQIzSVw8Mg9wv459-TjiOQe6wVziqaRlqX2_89rp54,2573
21
+ pyyaml-6.0.4.dist-info/METADATA,sha256=WF2-mcw01xitO_esU56PViS0J9Ty5poqOUP97lOIGqU,2361
22
+ pyyaml-6.0.4.dist-info/WHEEL,sha256=3uBjq9AZoVubrlH8N7Pasy_JpHMpNYFrtAB64uaHTRQ,101
23
+ pyyaml-6.0.4.dist-info/top_level.txt,sha256=rpj0IVMTisAjh_1vG3Ccf9v5jpCQwAz6cD1IVU5ZdhQ,11
24
+ pyyaml-6.0.4.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (84.2.1)
3
+ Root-Is-Purelib: false
4
+ Tag: cp315-cp315-win_arm64
5
+
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2017-2021 Ingy döt Net
2
+ Copyright (c) 2006-2016 Kirill Simonov
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
5
+ this software and associated documentation files (the "Software"), to deal in
6
+ the Software without restriction, including without limitation the rights to
7
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8
+ of the Software, and to permit persons to whom the Software is furnished to do
9
+ so, subject to the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be included in all
12
+ copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ SOFTWARE.
@@ -0,0 +1,2 @@
1
+ _yaml
2
+ yaml
yaml/__init__.py ADDED
@@ -0,0 +1,390 @@
1
+
2
+ from .error import *
3
+
4
+ from .tokens import *
5
+ from .events import *
6
+ from .nodes import *
7
+
8
+ from .loader import *
9
+ from .dumper import *
10
+
11
+ __version__ = '6.0.4'
12
+ try:
13
+ from .cyaml import *
14
+ __with_libyaml__ = True
15
+ except ImportError:
16
+ __with_libyaml__ = False
17
+
18
+ import io
19
+
20
+ #------------------------------------------------------------------------------
21
+ # XXX "Warnings control" is now deprecated. Leaving in the API function to not
22
+ # break code that uses it.
23
+ #------------------------------------------------------------------------------
24
+ def warnings(settings=None):
25
+ if settings is None:
26
+ return {}
27
+
28
+ #------------------------------------------------------------------------------
29
+ def scan(stream, Loader=Loader):
30
+ """
31
+ Scan a YAML stream and produce scanning tokens.
32
+ """
33
+ loader = Loader(stream)
34
+ try:
35
+ while loader.check_token():
36
+ yield loader.get_token()
37
+ finally:
38
+ loader.dispose()
39
+
40
+ def parse(stream, Loader=Loader):
41
+ """
42
+ Parse a YAML stream and produce parsing events.
43
+ """
44
+ loader = Loader(stream)
45
+ try:
46
+ while loader.check_event():
47
+ yield loader.get_event()
48
+ finally:
49
+ loader.dispose()
50
+
51
+ def compose(stream, Loader=Loader):
52
+ """
53
+ Parse the first YAML document in a stream
54
+ and produce the corresponding representation tree.
55
+ """
56
+ loader = Loader(stream)
57
+ try:
58
+ return loader.get_single_node()
59
+ finally:
60
+ loader.dispose()
61
+
62
+ def compose_all(stream, Loader=Loader):
63
+ """
64
+ Parse all YAML documents in a stream
65
+ and produce corresponding representation trees.
66
+ """
67
+ loader = Loader(stream)
68
+ try:
69
+ while loader.check_node():
70
+ yield loader.get_node()
71
+ finally:
72
+ loader.dispose()
73
+
74
+ def load(stream, Loader):
75
+ """
76
+ Parse the first YAML document in a stream
77
+ and produce the corresponding Python object.
78
+ """
79
+ loader = Loader(stream)
80
+ try:
81
+ return loader.get_single_data()
82
+ finally:
83
+ loader.dispose()
84
+
85
+ def load_all(stream, Loader):
86
+ """
87
+ Parse all YAML documents in a stream
88
+ and produce corresponding Python objects.
89
+ """
90
+ loader = Loader(stream)
91
+ try:
92
+ while loader.check_data():
93
+ yield loader.get_data()
94
+ finally:
95
+ loader.dispose()
96
+
97
+ def full_load(stream):
98
+ """
99
+ Parse the first YAML document in a stream
100
+ and produce the corresponding Python object.
101
+
102
+ Resolve all tags except those known to be
103
+ unsafe on untrusted input.
104
+ """
105
+ return load(stream, FullLoader)
106
+
107
+ def full_load_all(stream):
108
+ """
109
+ Parse all YAML documents in a stream
110
+ and produce corresponding Python objects.
111
+
112
+ Resolve all tags except those known to be
113
+ unsafe on untrusted input.
114
+ """
115
+ return load_all(stream, FullLoader)
116
+
117
+ def safe_load(stream):
118
+ """
119
+ Parse the first YAML document in a stream
120
+ and produce the corresponding Python object.
121
+
122
+ Resolve only basic YAML tags. This is known
123
+ to be safe for untrusted input.
124
+ """
125
+ return load(stream, SafeLoader)
126
+
127
+ def safe_load_all(stream):
128
+ """
129
+ Parse all YAML documents in a stream
130
+ and produce corresponding Python objects.
131
+
132
+ Resolve only basic YAML tags. This is known
133
+ to be safe for untrusted input.
134
+ """
135
+ return load_all(stream, SafeLoader)
136
+
137
+ def unsafe_load(stream):
138
+ """
139
+ Parse the first YAML document in a stream
140
+ and produce the corresponding Python object.
141
+
142
+ Resolve all tags, even those known to be
143
+ unsafe on untrusted input.
144
+ """
145
+ return load(stream, UnsafeLoader)
146
+
147
+ def unsafe_load_all(stream):
148
+ """
149
+ Parse all YAML documents in a stream
150
+ and produce corresponding Python objects.
151
+
152
+ Resolve all tags, even those known to be
153
+ unsafe on untrusted input.
154
+ """
155
+ return load_all(stream, UnsafeLoader)
156
+
157
+ def emit(events, stream=None, Dumper=Dumper,
158
+ canonical=None, indent=None, width=None,
159
+ allow_unicode=None, line_break=None):
160
+ """
161
+ Emit YAML parsing events into a stream.
162
+ If stream is None, return the produced string instead.
163
+ """
164
+ getvalue = None
165
+ if stream is None:
166
+ stream = io.StringIO()
167
+ getvalue = stream.getvalue
168
+ dumper = Dumper(stream, canonical=canonical, indent=indent, width=width,
169
+ allow_unicode=allow_unicode, line_break=line_break)
170
+ try:
171
+ for event in events:
172
+ dumper.emit(event)
173
+ finally:
174
+ dumper.dispose()
175
+ if getvalue:
176
+ return getvalue()
177
+
178
+ def serialize_all(nodes, stream=None, Dumper=Dumper,
179
+ canonical=None, indent=None, width=None,
180
+ allow_unicode=None, line_break=None,
181
+ encoding=None, explicit_start=None, explicit_end=None,
182
+ version=None, tags=None):
183
+ """
184
+ Serialize a sequence of representation trees into a YAML stream.
185
+ If stream is None, return the produced string instead.
186
+ """
187
+ getvalue = None
188
+ if stream is None:
189
+ if encoding is None:
190
+ stream = io.StringIO()
191
+ else:
192
+ stream = io.BytesIO()
193
+ getvalue = stream.getvalue
194
+ dumper = Dumper(stream, canonical=canonical, indent=indent, width=width,
195
+ allow_unicode=allow_unicode, line_break=line_break,
196
+ encoding=encoding, version=version, tags=tags,
197
+ explicit_start=explicit_start, explicit_end=explicit_end)
198
+ try:
199
+ dumper.open()
200
+ for node in nodes:
201
+ dumper.serialize(node)
202
+ dumper.close()
203
+ finally:
204
+ dumper.dispose()
205
+ if getvalue:
206
+ return getvalue()
207
+
208
+ def serialize(node, stream=None, Dumper=Dumper, **kwds):
209
+ """
210
+ Serialize a representation tree into a YAML stream.
211
+ If stream is None, return the produced string instead.
212
+ """
213
+ return serialize_all([node], stream, Dumper=Dumper, **kwds)
214
+
215
+ def dump_all(documents, stream=None, Dumper=Dumper,
216
+ default_style=None, default_flow_style=False,
217
+ canonical=None, indent=None, width=None,
218
+ allow_unicode=None, line_break=None,
219
+ encoding=None, explicit_start=None, explicit_end=None,
220
+ version=None, tags=None, sort_keys=True):
221
+ """
222
+ Serialize a sequence of Python objects into a YAML stream.
223
+ If stream is None, return the produced string instead.
224
+ """
225
+ getvalue = None
226
+ if stream is None:
227
+ if encoding is None:
228
+ stream = io.StringIO()
229
+ else:
230
+ stream = io.BytesIO()
231
+ getvalue = stream.getvalue
232
+ dumper = Dumper(stream, default_style=default_style,
233
+ default_flow_style=default_flow_style,
234
+ canonical=canonical, indent=indent, width=width,
235
+ allow_unicode=allow_unicode, line_break=line_break,
236
+ encoding=encoding, version=version, tags=tags,
237
+ explicit_start=explicit_start, explicit_end=explicit_end, sort_keys=sort_keys)
238
+ try:
239
+ dumper.open()
240
+ for data in documents:
241
+ dumper.represent(data)
242
+ dumper.close()
243
+ finally:
244
+ dumper.dispose()
245
+ if getvalue:
246
+ return getvalue()
247
+
248
+ def dump(data, stream=None, Dumper=Dumper, **kwds):
249
+ """
250
+ Serialize a Python object into a YAML stream.
251
+ If stream is None, return the produced string instead.
252
+ """
253
+ return dump_all([data], stream, Dumper=Dumper, **kwds)
254
+
255
+ def safe_dump_all(documents, stream=None, **kwds):
256
+ """
257
+ Serialize a sequence of Python objects into a YAML stream.
258
+ Produce only basic YAML tags.
259
+ If stream is None, return the produced string instead.
260
+ """
261
+ return dump_all(documents, stream, Dumper=SafeDumper, **kwds)
262
+
263
+ def safe_dump(data, stream=None, **kwds):
264
+ """
265
+ Serialize a Python object into a YAML stream.
266
+ Produce only basic YAML tags.
267
+ If stream is None, return the produced string instead.
268
+ """
269
+ return dump_all([data], stream, Dumper=SafeDumper, **kwds)
270
+
271
+ def add_implicit_resolver(tag, regexp, first=None,
272
+ Loader=None, Dumper=Dumper):
273
+ """
274
+ Add an implicit scalar detector.
275
+ If an implicit scalar value matches the given regexp,
276
+ the corresponding tag is assigned to the scalar.
277
+ first is a sequence of possible initial characters or None.
278
+ """
279
+ if Loader is None:
280
+ loader.Loader.add_implicit_resolver(tag, regexp, first)
281
+ loader.FullLoader.add_implicit_resolver(tag, regexp, first)
282
+ loader.UnsafeLoader.add_implicit_resolver(tag, regexp, first)
283
+ else:
284
+ Loader.add_implicit_resolver(tag, regexp, first)
285
+ Dumper.add_implicit_resolver(tag, regexp, first)
286
+
287
+ def add_path_resolver(tag, path, kind=None, Loader=None, Dumper=Dumper):
288
+ """
289
+ Add a path based resolver for the given tag.
290
+ A path is a list of keys that forms a path
291
+ to a node in the representation tree.
292
+ Keys can be string values, integers, or None.
293
+ """
294
+ if Loader is None:
295
+ loader.Loader.add_path_resolver(tag, path, kind)
296
+ loader.FullLoader.add_path_resolver(tag, path, kind)
297
+ loader.UnsafeLoader.add_path_resolver(tag, path, kind)
298
+ else:
299
+ Loader.add_path_resolver(tag, path, kind)
300
+ Dumper.add_path_resolver(tag, path, kind)
301
+
302
+ def add_constructor(tag, constructor, Loader=None):
303
+ """
304
+ Add a constructor for the given tag.
305
+ Constructor is a function that accepts a Loader instance
306
+ and a node object and produces the corresponding Python object.
307
+ """
308
+ if Loader is None:
309
+ loader.Loader.add_constructor(tag, constructor)
310
+ loader.FullLoader.add_constructor(tag, constructor)
311
+ loader.UnsafeLoader.add_constructor(tag, constructor)
312
+ else:
313
+ Loader.add_constructor(tag, constructor)
314
+
315
+ def add_multi_constructor(tag_prefix, multi_constructor, Loader=None):
316
+ """
317
+ Add a multi-constructor for the given tag prefix.
318
+ Multi-constructor is called for a node if its tag starts with tag_prefix.
319
+ Multi-constructor accepts a Loader instance, a tag suffix,
320
+ and a node object and produces the corresponding Python object.
321
+ """
322
+ if Loader is None:
323
+ loader.Loader.add_multi_constructor(tag_prefix, multi_constructor)
324
+ loader.FullLoader.add_multi_constructor(tag_prefix, multi_constructor)
325
+ loader.UnsafeLoader.add_multi_constructor(tag_prefix, multi_constructor)
326
+ else:
327
+ Loader.add_multi_constructor(tag_prefix, multi_constructor)
328
+
329
+ def add_representer(data_type, representer, Dumper=Dumper):
330
+ """
331
+ Add a representer for the given type.
332
+ Representer is a function accepting a Dumper instance
333
+ and an instance of the given data type
334
+ and producing the corresponding representation node.
335
+ """
336
+ Dumper.add_representer(data_type, representer)
337
+
338
+ def add_multi_representer(data_type, multi_representer, Dumper=Dumper):
339
+ """
340
+ Add a representer for the given type.
341
+ Multi-representer is a function accepting a Dumper instance
342
+ and an instance of the given data type or subtype
343
+ and producing the corresponding representation node.
344
+ """
345
+ Dumper.add_multi_representer(data_type, multi_representer)
346
+
347
+ class YAMLObjectMetaclass(type):
348
+ """
349
+ The metaclass for YAMLObject.
350
+ """
351
+ def __init__(cls, name, bases, kwds):
352
+ super(YAMLObjectMetaclass, cls).__init__(name, bases, kwds)
353
+ if 'yaml_tag' in kwds and kwds['yaml_tag'] is not None:
354
+ if isinstance(cls.yaml_loader, list):
355
+ for loader in cls.yaml_loader:
356
+ loader.add_constructor(cls.yaml_tag, cls.from_yaml)
357
+ else:
358
+ cls.yaml_loader.add_constructor(cls.yaml_tag, cls.from_yaml)
359
+
360
+ cls.yaml_dumper.add_representer(cls, cls.to_yaml)
361
+
362
+ class YAMLObject(metaclass=YAMLObjectMetaclass):
363
+ """
364
+ An object that can dump itself to a YAML stream
365
+ and load itself from a YAML stream.
366
+ """
367
+
368
+ __slots__ = () # no direct instantiation, so allow immutable subclasses
369
+
370
+ yaml_loader = [Loader, FullLoader, UnsafeLoader]
371
+ yaml_dumper = Dumper
372
+
373
+ yaml_tag = None
374
+ yaml_flow_style = None
375
+
376
+ @classmethod
377
+ def from_yaml(cls, loader, node):
378
+ """
379
+ Convert a representation node to a Python object.
380
+ """
381
+ return loader.construct_yaml_object(node, cls)
382
+
383
+ @classmethod
384
+ def to_yaml(cls, dumper, data):
385
+ """
386
+ Convert a Python object to a representation node.
387
+ """
388
+ return dumper.represent_yaml_object(cls.yaml_tag, data, cls,
389
+ flow_style=cls.yaml_flow_style)
390
+
Binary file
yaml/composer.py ADDED
@@ -0,0 +1,139 @@
1
+
2
+ __all__ = ['Composer', 'ComposerError']
3
+
4
+ from .error import MarkedYAMLError
5
+ from .events import *
6
+ from .nodes import *
7
+
8
+ class ComposerError(MarkedYAMLError):
9
+ pass
10
+
11
+ class Composer:
12
+
13
+ def __init__(self):
14
+ self.anchors = {}
15
+
16
+ def check_node(self):
17
+ # Drop the STREAM-START event.
18
+ if self.check_event(StreamStartEvent):
19
+ self.get_event()
20
+
21
+ # If there are more documents available?
22
+ return not self.check_event(StreamEndEvent)
23
+
24
+ def get_node(self):
25
+ # Get the root node of the next document.
26
+ if not self.check_event(StreamEndEvent):
27
+ return self.compose_document()
28
+
29
+ def get_single_node(self):
30
+ # Drop the STREAM-START event.
31
+ self.get_event()
32
+
33
+ # Compose a document if the stream is not empty.
34
+ document = None
35
+ if not self.check_event(StreamEndEvent):
36
+ document = self.compose_document()
37
+
38
+ # Ensure that the stream contains no more documents.
39
+ if not self.check_event(StreamEndEvent):
40
+ event = self.get_event()
41
+ raise ComposerError("expected a single document in the stream",
42
+ document.start_mark, "but found another document",
43
+ event.start_mark)
44
+
45
+ # Drop the STREAM-END event.
46
+ self.get_event()
47
+
48
+ return document
49
+
50
+ def compose_document(self):
51
+ # Drop the DOCUMENT-START event.
52
+ self.get_event()
53
+
54
+ # Compose the root node.
55
+ node = self.compose_node(None, None)
56
+
57
+ # Drop the DOCUMENT-END event.
58
+ self.get_event()
59
+
60
+ self.anchors = {}
61
+ return node
62
+
63
+ def compose_node(self, parent, index):
64
+ if self.check_event(AliasEvent):
65
+ event = self.get_event()
66
+ anchor = event.anchor
67
+ if anchor not in self.anchors:
68
+ raise ComposerError(None, None, "found undefined alias %r"
69
+ % anchor, event.start_mark)
70
+ return self.anchors[anchor]
71
+ event = self.peek_event()
72
+ anchor = event.anchor
73
+ if anchor is not None:
74
+ if anchor in self.anchors:
75
+ raise ComposerError("found duplicate anchor %r; first occurrence"
76
+ % anchor, self.anchors[anchor].start_mark,
77
+ "second occurrence", event.start_mark)
78
+ self.descend_resolver(parent, index)
79
+ if self.check_event(ScalarEvent):
80
+ node = self.compose_scalar_node(anchor)
81
+ elif self.check_event(SequenceStartEvent):
82
+ node = self.compose_sequence_node(anchor)
83
+ elif self.check_event(MappingStartEvent):
84
+ node = self.compose_mapping_node(anchor)
85
+ self.ascend_resolver()
86
+ return node
87
+
88
+ def compose_scalar_node(self, anchor):
89
+ event = self.get_event()
90
+ tag = event.tag
91
+ if tag is None or tag == '!':
92
+ tag = self.resolve(ScalarNode, event.value, event.implicit)
93
+ node = ScalarNode(tag, event.value,
94
+ event.start_mark, event.end_mark, style=event.style)
95
+ if anchor is not None:
96
+ self.anchors[anchor] = node
97
+ return node
98
+
99
+ def compose_sequence_node(self, anchor):
100
+ start_event = self.get_event()
101
+ tag = start_event.tag
102
+ if tag is None or tag == '!':
103
+ tag = self.resolve(SequenceNode, None, start_event.implicit)
104
+ node = SequenceNode(tag, [],
105
+ start_event.start_mark, None,
106
+ flow_style=start_event.flow_style)
107
+ if anchor is not None:
108
+ self.anchors[anchor] = node
109
+ index = 0
110
+ while not self.check_event(SequenceEndEvent):
111
+ node.value.append(self.compose_node(node, index))
112
+ index += 1
113
+ end_event = self.get_event()
114
+ node.end_mark = end_event.end_mark
115
+ return node
116
+
117
+ def compose_mapping_node(self, anchor):
118
+ start_event = self.get_event()
119
+ tag = start_event.tag
120
+ if tag is None or tag == '!':
121
+ tag = self.resolve(MappingNode, None, start_event.implicit)
122
+ node = MappingNode(tag, [],
123
+ start_event.start_mark, None,
124
+ flow_style=start_event.flow_style)
125
+ if anchor is not None:
126
+ self.anchors[anchor] = node
127
+ while not self.check_event(MappingEndEvent):
128
+ #key_event = self.peek_event()
129
+ item_key = self.compose_node(node, None)
130
+ #if item_key in node.value:
131
+ # raise ComposerError("while composing a mapping", start_event.start_mark,
132
+ # "found duplicate key", key_event.start_mark)
133
+ item_value = self.compose_node(node, item_key)
134
+ #node.value[item_key] = item_value
135
+ node.value.append((item_key, item_value))
136
+ end_event = self.get_event()
137
+ node.end_mark = end_event.end_mark
138
+ return node
139
+