yamlcore 0.0.1__py3-none-any.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.
yamlcore/dumper.py ADDED
@@ -0,0 +1,86 @@
1
+ import yaml
2
+ from yamlcore.loader import CommonResolver
3
+
4
+
5
+ class CommonRepresenter(yaml.representer.BaseRepresenter):
6
+
7
+ inf_value = 1e300
8
+ while repr(inf_value) != repr(inf_value*inf_value):
9
+ inf_value *= inf_value
10
+
11
+ @classmethod
12
+ def init_representers(cls, name):
13
+ for key in _representers[name]:
14
+ callback = _representers[name][key]
15
+ cls.add_representer(key, callback)
16
+
17
+ def ignore_aliases(self, data):
18
+ if data is None:
19
+ return True
20
+ if isinstance(data, tuple) and data == ():
21
+ return True
22
+ if isinstance(data, (str, bytes, bool, int, float)):
23
+ return True
24
+
25
+
26
+ class CommonDumper(yaml.BaseDumper, CommonRepresenter, CommonResolver):
27
+
28
+ @classmethod
29
+ def init_tags(cls, tagset):
30
+ cls.init_representers(tagset)
31
+ cls.init_resolvers(tagset)
32
+
33
+
34
+ class CCommonDumper(yaml.CBaseDumper, CommonRepresenter, CommonResolver):
35
+
36
+ @classmethod
37
+ def init_tags(cls, tagset):
38
+ cls.init_representers(tagset)
39
+ cls.init_resolvers(tagset)
40
+
41
+
42
+ class CoreDumper(CommonDumper):
43
+ pass
44
+
45
+
46
+ class CCoreDumper(CCommonDumper):
47
+ pass
48
+
49
+
50
+ class CommonRepresenter(yaml.representer.BaseRepresenter):
51
+
52
+ inf_value = 1e300
53
+ while repr(inf_value) != repr(inf_value*inf_value):
54
+ inf_value *= inf_value
55
+
56
+ @classmethod
57
+ def init_representers(cls, name):
58
+ for key in _representers[name]:
59
+ callback = _representers[name][key]
60
+ cls.add_representer(key, callback)
61
+
62
+ def ignore_aliases(self, data):
63
+ if data is None:
64
+ return True
65
+ if isinstance(data, tuple) and data == ():
66
+ return True
67
+ if isinstance(data, (str, bytes, bool, int, float)):
68
+ return True
69
+
70
+
71
+ _representers = {
72
+ 'core': {
73
+ bool: yaml.representer.SafeRepresenter.represent_bool,
74
+ dict: yaml.representer.SafeRepresenter.represent_dict,
75
+ list: yaml.representer.SafeRepresenter.represent_list,
76
+ tuple: yaml.representer.SafeRepresenter.represent_list,
77
+ str: yaml.representer.SafeRepresenter.represent_str,
78
+ float: yaml.representer.SafeRepresenter.represent_float,
79
+ int: yaml.representer.SafeRepresenter.represent_int,
80
+ type(None): yaml.representer.SafeRepresenter.represent_none,
81
+ None: yaml.representer.SafeRepresenter.represent_undefined,
82
+ },
83
+ }
84
+
85
+ CoreDumper.init_tags('core')
86
+ CCoreDumper.init_tags('core')
yamlcore/loader.py ADDED
@@ -0,0 +1,157 @@
1
+ import yaml
2
+ import re
3
+
4
+
5
+ class CommonResolver(yaml.resolver.BaseResolver):
6
+
7
+ @classmethod
8
+ def init_resolvers(cls, key):
9
+ for args in _resolvers[key]:
10
+ cls.add_implicit_resolver(
11
+ 'tag:yaml.org,2002:' + args[0],
12
+ args[1], args[2]
13
+ )
14
+
15
+
16
+ class CommonConstructor(yaml.constructor.BaseConstructor):
17
+
18
+ inf_value = 1e300
19
+ while inf_value != inf_value*inf_value:
20
+ inf_value *= inf_value
21
+ nan_value = -inf_value/inf_value # Trying to make a quiet NaN (like C99).
22
+
23
+ bool_values = {
24
+ 'core': {
25
+ 'true': True,
26
+ 'True': True,
27
+ 'TRUE': True,
28
+ 'false': False,
29
+ 'False': False,
30
+ 'FALSE': False,
31
+ },
32
+ }
33
+
34
+ def construct_yaml_bool_core(self, node):
35
+ value = self.construct_scalar(node)
36
+ return self.bool_values["core"][value]
37
+
38
+ null_values = {
39
+ 'core': {
40
+ '': None,
41
+ '~': None,
42
+ 'null': None,
43
+ 'Null': None,
44
+ 'NULL': None,
45
+ },
46
+ }
47
+
48
+ def construct_yaml_null_core(self, node):
49
+ value = self.construct_scalar(node)
50
+ return self.null_values["core"][value]
51
+
52
+ def construct_yaml_int_core(self, node):
53
+ value = self.construct_scalar(node)
54
+ sign = +1
55
+ if value[0] == '-':
56
+ sign = -1
57
+ if value[0] in '+-':
58
+ value = value[1:]
59
+
60
+ if value == '0':
61
+ return 0
62
+ elif value.startswith('0o'):
63
+ return sign*int(value[2:], 8)
64
+ elif value.startswith('0x'):
65
+ return sign*int(value[2:], 16)
66
+ else:
67
+ return sign*int(value)
68
+
69
+ def construct_yaml_float_core(self, node):
70
+ value = self.construct_scalar(node)
71
+ value = value.lower()
72
+ sign = +1
73
+ if value[0] == '-':
74
+ sign = -1
75
+ if value[0] in '+-':
76
+ value = value[1:]
77
+ if value == '.inf':
78
+ return sign*self.inf_value
79
+ elif value == '.nan':
80
+ return self.nan_value
81
+ else:
82
+ return sign*float(value)
83
+
84
+ @classmethod
85
+ def init_constructors(cls, tagset):
86
+ if tagset not in _constructors:
87
+ return
88
+ for key in _constructors[tagset]:
89
+ callback = _constructors[tagset][key]
90
+ if (key is None):
91
+ cls.add_constructor(key, callback)
92
+ else:
93
+ cls.add_constructor('tag:yaml.org,2002:' + key, callback)
94
+
95
+
96
+ class CommonLoader(yaml.BaseLoader, CommonConstructor, CommonResolver):
97
+
98
+ @classmethod
99
+ def init_tagset(cls, tagset):
100
+ cls.init_constructors(tagset)
101
+ cls.init_resolvers(tagset)
102
+
103
+
104
+ class CCommonLoader(yaml.CBaseLoader, CommonConstructor, CommonResolver):
105
+
106
+ @classmethod
107
+ def init_tagset(cls, tagset):
108
+ cls.init_constructors(tagset)
109
+ cls.init_resolvers(tagset)
110
+
111
+
112
+ class CoreLoader(CommonLoader):
113
+ pass
114
+
115
+
116
+ class CCoreLoader(CCommonLoader):
117
+ pass
118
+
119
+
120
+ _constructors = {
121
+ 'core': {
122
+ 'str': yaml.constructor.SafeConstructor.construct_yaml_str,
123
+ 'seq': yaml.constructor.SafeConstructor.construct_yaml_seq,
124
+ 'map': yaml.constructor.SafeConstructor.construct_yaml_map,
125
+ None: yaml.constructor.SafeConstructor.construct_undefined,
126
+ 'int': CommonConstructor.construct_yaml_int_core,
127
+ 'float': CommonConstructor.construct_yaml_float_core,
128
+ 'null': CommonConstructor.construct_yaml_null_core,
129
+ 'bool': CommonConstructor.construct_yaml_bool_core,
130
+ },
131
+ }
132
+
133
+ _resolvers = {
134
+ 'core': [
135
+ ['bool',
136
+ re.compile(r'''^(?:|true|True|TRUE|false|False|FALSE)$''', re.X),
137
+ list('tTfF')],
138
+ ['int',
139
+ re.compile(r'''^(?:
140
+ |0o[0-7]+
141
+ |[-+]?(?:[0-9]+)
142
+ |0x[0-9a-fA-F]+
143
+ )$''', re.X),
144
+ list('-+0123456789')],
145
+ ['float',
146
+ re.compile(r'''^(?:[-+]?(?:\.[0-9]+|[0-9]+(\.[0-9]*)?)(?:[eE][-+]?[0-9]+)?
147
+ |[-+]?\.(?:inf|Inf|INF)
148
+ |\.(?:nan|NaN|NAN))$''', re.X),
149
+ list('-+0123456789.')],
150
+ ['null',
151
+ re.compile(r'''^(?:~||null|Null|NULL)$''', re.X),
152
+ ['~', 'n', 'N', '']],
153
+ ],
154
+ }
155
+
156
+ CoreLoader.init_tagset('core')
157
+ CCoreLoader.init_tagset('core')
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Tina Müller (tinita)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,89 @@
1
+ Metadata-Version: 2.1
2
+ Name: yamlcore
3
+ Version: 0.0.1
4
+ Summary: YAML 1.2 Support for PyYAML
5
+ Home-page: https://github.com/perlpunk/pyyaml-core
6
+ Author: Tina Müller
7
+ Author-email: cpan2@tinita.de
8
+ Project-URL: Bug Tracker, https://github.com/perlpunk/pyyaml-core/issues
9
+ Project-URL: repository, https://github.com/perlpunk/pyyaml-core
10
+ Classifier: Programming Language :: Python :: 3
11
+ Classifier: License :: OSI Approved :: MIT License
12
+ Classifier: Operating System :: OS Independent
13
+ Requires-Python: >=3.6
14
+ Description-Content-Type: text/markdown
15
+ License-File: LICENSE
16
+
17
+ ## pyyaml-core - YAML 1.2 Core Schema Support for PyYAML
18
+
19
+ This module can be used on top of PyYAML to load YAML 1.2 files.
20
+
21
+ Currently it supports enabling all YAML 1.2 Core Schema tags on top
22
+ of the PyYAML BaseLoader.
23
+
24
+ For more information see the [comparison of 1.1 and 1.2
25
+ schemas](https://perlpunk.github.io/yaml-test-schema/schemas.html).
26
+
27
+ ## Examples
28
+
29
+ import yaml
30
+ from yamlcore.loader import CoreLoader
31
+ from yamlcore.dumper import CoreDumper
32
+
33
+ y = """
34
+ ---
35
+ 1.1: # strings
36
+ - yes
37
+ - no # norway problem anymore
38
+ - 1__0
39
+ - 10:20
40
+ - +0b100
41
+ - 0x4_2
42
+
43
+ core:
44
+ - true
45
+ - 0o10
46
+ - 0x42
47
+ - ~
48
+ - .inf
49
+ """
50
+
51
+ d = yaml.load(y, Loader=CoreLoader)
52
+ out = yaml.dump(d, Dumper=CoreDumper)
53
+
54
+ You can also use `CCoreLoader` and `CCoreDumper` for using the
55
+ [libyaml](https://github.com/yaml/libyaml) based parser and emitter.
56
+
57
+ ## Why?
58
+
59
+ At the time of this writing, there is a [pending pull
60
+ request](https://github.com/yaml/pyyaml/pull/555) that adds YAML 1.2 Core
61
+ Schema Support for PyYAML.
62
+
63
+ It's blocked because there is a plan to redesign the API, and no new things
64
+ shall be added using the old API at this point.
65
+
66
+ So as long as PyYAML doesn't merge this, you can use this module as an
67
+ alternative.
68
+
69
+ MIT License
70
+
71
+ Copyright (c) 2024 Tina Müller (tinita)
72
+
73
+ Permission is hereby granted, free of charge, to any person obtaining a copy
74
+ of this software and associated documentation files (the "Software"), to deal
75
+ in the Software without restriction, including without limitation the rights
76
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
77
+ copies of the Software, and to permit persons to whom the Software is
78
+ furnished to do so, subject to the following conditions:
79
+
80
+ The above copyright notice and this permission notice shall be included in all
81
+ copies or substantial portions of the Software.
82
+
83
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
84
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
85
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
86
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
87
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
88
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
89
+ SOFTWARE.
@@ -0,0 +1,7 @@
1
+ yamlcore/dumper.py,sha256=oaY4ZQzvH3kwe5jiMh2yK_Pk_EYGl8HDrbjfH60IQLs,2435
2
+ yamlcore/loader.py,sha256=zJ5uPWadaLGOV6SW4MTTYi3t9mLl6xEO0TvcWSiiItE,4361
3
+ yamlcore-0.0.1.dist-info/LICENSE,sha256=X8_Fc4fSN5jEBo_o5jAao2dmDh0ZllFMo9s0gkvFhQM,1078
4
+ yamlcore-0.0.1.dist-info/METADATA,sha256=jB6EHn7oJaGrHtMhgcFDUifxkSeZ_aHf7uTBUG9B9Pk,2893
5
+ yamlcore-0.0.1.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
6
+ yamlcore-0.0.1.dist-info/top_level.txt,sha256=jk1AsDKileXVYp-qjNJiwiibd-wJDQw1ktuOaISLvk8,9
7
+ yamlcore-0.0.1.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: bdist_wheel (0.43.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1 @@
1
+ yamlcore