yamlcore 0.0.1__tar.gz
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-0.0.1/LICENSE +21 -0
- yamlcore-0.0.1/PKG-INFO +89 -0
- yamlcore-0.0.1/Readme.md +51 -0
- yamlcore-0.0.1/pyproject.toml +3 -0
- yamlcore-0.0.1/setup.cfg +27 -0
- yamlcore-0.0.1/tests/test-schema.py +158 -0
- yamlcore-0.0.1/yamlcore/dumper.py +86 -0
- yamlcore-0.0.1/yamlcore/loader.py +157 -0
- yamlcore-0.0.1/yamlcore.egg-info/PKG-INFO +89 -0
- yamlcore-0.0.1/yamlcore.egg-info/SOURCES.txt +11 -0
- yamlcore-0.0.1/yamlcore.egg-info/dependency_links.txt +1 -0
- yamlcore-0.0.1/yamlcore.egg-info/top_level.txt +1 -0
yamlcore-0.0.1/LICENSE
ADDED
|
@@ -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.
|
yamlcore-0.0.1/PKG-INFO
ADDED
|
@@ -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.
|
yamlcore-0.0.1/Readme.md
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
## pyyaml-core - YAML 1.2 Core Schema Support for PyYAML
|
|
2
|
+
|
|
3
|
+
This module can be used on top of PyYAML to load YAML 1.2 files.
|
|
4
|
+
|
|
5
|
+
Currently it supports enabling all YAML 1.2 Core Schema tags on top
|
|
6
|
+
of the PyYAML BaseLoader.
|
|
7
|
+
|
|
8
|
+
For more information see the [comparison of 1.1 and 1.2
|
|
9
|
+
schemas](https://perlpunk.github.io/yaml-test-schema/schemas.html).
|
|
10
|
+
|
|
11
|
+
## Examples
|
|
12
|
+
|
|
13
|
+
import yaml
|
|
14
|
+
from yamlcore.loader import CoreLoader
|
|
15
|
+
from yamlcore.dumper import CoreDumper
|
|
16
|
+
|
|
17
|
+
y = """
|
|
18
|
+
---
|
|
19
|
+
1.1: # strings
|
|
20
|
+
- yes
|
|
21
|
+
- no # norway problem anymore
|
|
22
|
+
- 1__0
|
|
23
|
+
- 10:20
|
|
24
|
+
- +0b100
|
|
25
|
+
- 0x4_2
|
|
26
|
+
|
|
27
|
+
core:
|
|
28
|
+
- true
|
|
29
|
+
- 0o10
|
|
30
|
+
- 0x42
|
|
31
|
+
- ~
|
|
32
|
+
- .inf
|
|
33
|
+
"""
|
|
34
|
+
|
|
35
|
+
d = yaml.load(y, Loader=CoreLoader)
|
|
36
|
+
out = yaml.dump(d, Dumper=CoreDumper)
|
|
37
|
+
|
|
38
|
+
You can also use `CCoreLoader` and `CCoreDumper` for using the
|
|
39
|
+
[libyaml](https://github.com/yaml/libyaml) based parser and emitter.
|
|
40
|
+
|
|
41
|
+
## Why?
|
|
42
|
+
|
|
43
|
+
At the time of this writing, there is a [pending pull
|
|
44
|
+
request](https://github.com/yaml/pyyaml/pull/555) that adds YAML 1.2 Core
|
|
45
|
+
Schema Support for PyYAML.
|
|
46
|
+
|
|
47
|
+
It's blocked because there is a plan to redesign the API, and no new things
|
|
48
|
+
shall be added using the old API at this point.
|
|
49
|
+
|
|
50
|
+
So as long as PyYAML doesn't merge this, you can use this module as an
|
|
51
|
+
alternative.
|
yamlcore-0.0.1/setup.cfg
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
[metadata]
|
|
2
|
+
name = yamlcore
|
|
3
|
+
version = 0.0.1
|
|
4
|
+
author = Tina Müller
|
|
5
|
+
author_email = cpan2@tinita.de
|
|
6
|
+
description = YAML 1.2 Support for PyYAML
|
|
7
|
+
long_description = file: Readme.md, LICENSE
|
|
8
|
+
long_description_content_type = text/markdown
|
|
9
|
+
url = https://github.com/perlpunk/pyyaml-core
|
|
10
|
+
project_urls =
|
|
11
|
+
Bug Tracker = https://github.com/perlpunk/pyyaml-core/issues
|
|
12
|
+
repository = https://github.com/perlpunk/pyyaml-core
|
|
13
|
+
classifiers =
|
|
14
|
+
Programming Language :: Python :: 3
|
|
15
|
+
License :: OSI Approved :: MIT License
|
|
16
|
+
Operating System :: OS Independent
|
|
17
|
+
|
|
18
|
+
[options]
|
|
19
|
+
package_dir =
|
|
20
|
+
= .
|
|
21
|
+
packages = yamlcore
|
|
22
|
+
python_requires = >=3.6
|
|
23
|
+
|
|
24
|
+
[egg_info]
|
|
25
|
+
tag_build =
|
|
26
|
+
tag_date = 0
|
|
27
|
+
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import sys
|
|
2
|
+
import yaml
|
|
3
|
+
import math
|
|
4
|
+
|
|
5
|
+
from yamlcore.loader import CoreLoader, CCoreLoader
|
|
6
|
+
from yamlcore.dumper import CoreDumper, CCoreDumper
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def check_bool(value, expected):
|
|
10
|
+
if expected == 'false()' and value is False:
|
|
11
|
+
return 1
|
|
12
|
+
if expected == 'true()' and value is True:
|
|
13
|
+
return 1
|
|
14
|
+
print(value)
|
|
15
|
+
print(expected)
|
|
16
|
+
return 0
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def check_int(value, expected):
|
|
20
|
+
if (int(expected) == value):
|
|
21
|
+
return 1
|
|
22
|
+
print(value)
|
|
23
|
+
print(expected)
|
|
24
|
+
return 0
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def check_float(value, expected):
|
|
28
|
+
if expected == 'inf()':
|
|
29
|
+
if value == math.inf:
|
|
30
|
+
return 1
|
|
31
|
+
elif expected == 'inf-neg()':
|
|
32
|
+
if value == -math.inf:
|
|
33
|
+
return 1
|
|
34
|
+
elif expected == 'nan()':
|
|
35
|
+
if math.isnan(value):
|
|
36
|
+
return 1
|
|
37
|
+
elif (float(expected) == value):
|
|
38
|
+
return 1
|
|
39
|
+
else:
|
|
40
|
+
print(value)
|
|
41
|
+
print(expected)
|
|
42
|
+
return 0
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def check_str(value, expected):
|
|
46
|
+
if value == expected:
|
|
47
|
+
return 1
|
|
48
|
+
print(value)
|
|
49
|
+
print(expected)
|
|
50
|
+
return 0
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def _fail(input, test):
|
|
54
|
+
print("Input: >>" + input + "<<")
|
|
55
|
+
print(test)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
types = {
|
|
59
|
+
'str': [str, check_str],
|
|
60
|
+
'int': [int, check_int],
|
|
61
|
+
'float': [float, check_float],
|
|
62
|
+
'inf': [float, check_float],
|
|
63
|
+
'nan': [float, check_float],
|
|
64
|
+
'bool': [bool, check_bool],
|
|
65
|
+
}
|
|
66
|
+
loaders = {
|
|
67
|
+
'core': CoreLoader,
|
|
68
|
+
}
|
|
69
|
+
dumpers = {
|
|
70
|
+
'core': CoreDumper,
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def load_test_files(tagset):
|
|
75
|
+
|
|
76
|
+
data_filename = "tests/data/"+tagset+".schema"
|
|
77
|
+
with open(data_filename, 'rb') as file:
|
|
78
|
+
tests = yaml.load(file, Loader=yaml.SafeLoader)
|
|
79
|
+
return tests
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def schema_test(tagset, loader, dumper):
|
|
83
|
+
tests = load_test_files(tagset)
|
|
84
|
+
|
|
85
|
+
i = 0
|
|
86
|
+
fail = 0
|
|
87
|
+
for i, (input, test) in enumerate(sorted(tests.items())):
|
|
88
|
+
|
|
89
|
+
exp_type = test[0]
|
|
90
|
+
data = test[1]
|
|
91
|
+
exp_dump = test[2]
|
|
92
|
+
|
|
93
|
+
# Test loading
|
|
94
|
+
load_ok = 0
|
|
95
|
+
try:
|
|
96
|
+
doc_input = """---\n""" + input
|
|
97
|
+
loaded = yaml.load(doc_input, Loader=loader)
|
|
98
|
+
load_ok = 1
|
|
99
|
+
except:
|
|
100
|
+
print("Error:", sys.exc_info()[0], '(', sys.exc_info()[1], ')')
|
|
101
|
+
fail += 1
|
|
102
|
+
_fail(input, test)
|
|
103
|
+
assert (load_ok)
|
|
104
|
+
|
|
105
|
+
if exp_type == 'null':
|
|
106
|
+
load_ok = 0
|
|
107
|
+
if loaded is None:
|
|
108
|
+
load_ok = 1
|
|
109
|
+
else:
|
|
110
|
+
fail += 1
|
|
111
|
+
_fail(input, test)
|
|
112
|
+
assert (load_ok)
|
|
113
|
+
else:
|
|
114
|
+
t = types[exp_type][0]
|
|
115
|
+
code = types[exp_type][1]
|
|
116
|
+
|
|
117
|
+
instance_ok = 0
|
|
118
|
+
type_ok = 0
|
|
119
|
+
if isinstance(loaded, t):
|
|
120
|
+
instance_ok = 1
|
|
121
|
+
if code(loaded, data):
|
|
122
|
+
type_ok = 1
|
|
123
|
+
else:
|
|
124
|
+
fail += 1
|
|
125
|
+
print("Expected data: >>" + str(data) + "<< Got: >>" + str(loaded) + "<<")
|
|
126
|
+
_fail(input, test)
|
|
127
|
+
else:
|
|
128
|
+
fail += 1
|
|
129
|
+
print("Expected type: >>" + exp_type + "<< Got: >>" + str(loaded) + "<<")
|
|
130
|
+
_fail(input, test)
|
|
131
|
+
assert (instance_ok)
|
|
132
|
+
assert (type_ok)
|
|
133
|
+
|
|
134
|
+
dump = yaml.dump(loaded, explicit_end=False, Dumper=dumper)
|
|
135
|
+
# strip trailing newlines and footers
|
|
136
|
+
if (dump == '...\n'):
|
|
137
|
+
dump = ''
|
|
138
|
+
if dump.endswith('\n...\n'):
|
|
139
|
+
dump = dump[:-5]
|
|
140
|
+
if dump.endswith('\n'):
|
|
141
|
+
dump = dump[:-1]
|
|
142
|
+
|
|
143
|
+
dump_ok = 0
|
|
144
|
+
if dump == exp_dump:
|
|
145
|
+
dump_ok = 1
|
|
146
|
+
else:
|
|
147
|
+
print("Compare: >>" + dump + "<< >>" + exp_dump + "<<")
|
|
148
|
+
fail += 1
|
|
149
|
+
_fail(input, test)
|
|
150
|
+
assert (dump_ok)
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
def test_core_schema_py():
|
|
154
|
+
schema_test('core', CoreLoader, CoreDumper)
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
def test_core_schema_c():
|
|
158
|
+
schema_test('core', CCoreLoader, CCoreDumper)
|
|
@@ -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')
|
|
@@ -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,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 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
yamlcore
|