ymlplus 0.1.0__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.
- yamlplus/__init__.py +16 -0
- yamlplus/__main__.py +8 -0
- yamlplus/_identifiers.py +72 -0
- yamlplus/_io.py +262 -0
- yamlplus/_numbers.py +40 -0
- yamlplus/_pyyaml.py +53 -0
- yamlplus/_syntax.py +511 -0
- yamlplus/_validation.py +197 -0
- yamlplus/_xid.py +2151 -0
- yamlplus/_yaml_surface.py +346 -0
- yamlplus/api.py +130 -0
- yamlplus/errors.py +14 -0
- ymlplus-0.1.0.dist-info/METADATA +92 -0
- ymlplus-0.1.0.dist-info/RECORD +18 -0
- ymlplus-0.1.0.dist-info/WHEEL +5 -0
- ymlplus-0.1.0.dist-info/entry_points.txt +2 -0
- ymlplus-0.1.0.dist-info/licenses/LICENSE +674 -0
- ymlplus-0.1.0.dist-info/top_level.txt +1 -0
yamlplus/_validation.py
ADDED
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
"""Profile membership, compatibility diagnostics, and located source repairs."""
|
|
2
|
+
|
|
3
|
+
from bisect import bisect_right
|
|
4
|
+
import json
|
|
5
|
+
import re
|
|
6
|
+
|
|
7
|
+
import yaml
|
|
8
|
+
|
|
9
|
+
from .errors import ValidationError
|
|
10
|
+
from ._numbers import REFUSE_DIGITS, WARN_DIGITS, decimal_digits, decimal_text, float_text
|
|
11
|
+
from ._pyyaml import Loader
|
|
12
|
+
from ._syntax import validate
|
|
13
|
+
from ._yaml_surface import check_syntax
|
|
14
|
+
|
|
15
|
+
_INT11 = re.compile(r"[-+]?(?:0b[01_]+|0[0-7_]+|0|[1-9][0-9_]*|0x[0-9a-fA-F_]+|[1-9][0-9_]*(?::[0-5]?[0-9])+)")
|
|
16
|
+
_INT12 = re.compile(r"[-+]?[0-9]+|0o[0-7]+|0x[0-9a-fA-F]+")
|
|
17
|
+
_FLOAT11 = re.compile(r"[-+]?(?:[0-9][0-9_]*)?\.[0-9.]*(?:[eE][-+][0-9]+)?|[-+]?[0-9][0-9_]*(?::[0-5]?[0-9])+\.[0-9_]*|[-+]?\.(?:inf|Inf|INF)|\.(?:nan|NaN|NAN)")
|
|
18
|
+
_FLOAT12 = re.compile(r"[-+]?(?:\.[0-9]+|[0-9]+(?:\.[0-9]*)?)(?:[eE][-+]?[0-9]+)?|[-+]?\.(?:inf|Inf|INF)|\.(?:nan|NaN|NAN)")
|
|
19
|
+
_IMPLICIT = (
|
|
20
|
+
("boolean", re.compile(r"y|Y|yes|Yes|YES|n|N|no|No|NO|true|True|TRUE|false|False|FALSE|on|On|ON|off|Off|OFF")),
|
|
21
|
+
("null", re.compile(r"~|null|Null|NULL|")),
|
|
22
|
+
("integer", re.compile(f"(?:{_INT11.pattern})|(?:{_INT12.pattern})")),
|
|
23
|
+
("float", re.compile(f"(?:{_FLOAT11.pattern})|(?:{_FLOAT12.pattern})")),
|
|
24
|
+
("timestamp", re.compile(r"[0-9]{4}-[0-9]{2}-[0-9]{2}|[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}(?:[Tt]|[ \t]+)[0-9]{1,2}:[0-9]{2}:[0-9]{2}(?:\.[0-9]*)?(?:(?:[ \t]*)Z|[-+][0-9]{1,2}(?::[0-9]{2})?)?")),
|
|
25
|
+
("merge", re.compile(r"<<")),
|
|
26
|
+
("special value", re.compile(r"=")),
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def implicit_type(text):
|
|
31
|
+
return next((kind for kind, pattern in _IMPLICIT if pattern.fullmatch(text)), None)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def quote(value):
|
|
35
|
+
text = json.dumps(value, ensure_ascii=False)
|
|
36
|
+
return "".join(f"\\u{ord(ch):04x}" if 0x7F <= ord(ch) <= 0x9F or
|
|
37
|
+
ord(ch) in (0x2028, 0x2029, 0xFEFF, 0xFFFE, 0xFFFF) else ch
|
|
38
|
+
for ch in text)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def where(line, column, source=None):
|
|
42
|
+
return f"{source}:{line}:{column}" if source is not None else f"line {line}, column {column}"
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def key_warning(key, kind, location, *, writing):
|
|
46
|
+
shown = repr(key[:64]) + ("..." if len(key) > 64 else "")
|
|
47
|
+
action = ("quoting it for profile output (an exact non-name key)" if writing else
|
|
48
|
+
"quote the key to prevent conversion (quotation makes it a non-name)")
|
|
49
|
+
return (f"warning: {location}: YAML may automatically convert key {shown} "
|
|
50
|
+
f"to {kind}; distinct keys can collide; {action}")
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def callback_option(callback, name):
|
|
54
|
+
if callback is not None and not callable(callback):
|
|
55
|
+
raise TypeError(f"{name} must be callable or None")
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def digit_option(value):
|
|
59
|
+
if not isinstance(value, int) or isinstance(value, bool):
|
|
60
|
+
raise TypeError("max_digits must be an integer")
|
|
61
|
+
if value < WARN_DIGITS:
|
|
62
|
+
raise ValueError(f"max_digits must be at least {WARN_DIGITS}")
|
|
63
|
+
return int.__int__(value)
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def send(callback, messages):
|
|
67
|
+
if callback is not None:
|
|
68
|
+
for message in messages:
|
|
69
|
+
callback(message)
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
class Notices:
|
|
73
|
+
def __init__(self):
|
|
74
|
+
self.kinds = {}
|
|
75
|
+
|
|
76
|
+
def add(self, kind, location):
|
|
77
|
+
count, first = self.kinds.get(kind, (0, location))
|
|
78
|
+
self.kinds[kind] = count + 1, first
|
|
79
|
+
|
|
80
|
+
def messages(self):
|
|
81
|
+
for kind, (count, first) in self.kinds.items():
|
|
82
|
+
meaning = "bytes as literal text, not decoded bytes" if kind == "bytes" else "bare None as text, not null"
|
|
83
|
+
yield (f"info: {first}: {count} {kind} value(s); YAML reads {meaning}; "
|
|
84
|
+
"this accepted compatibility difference has no shared typed spelling")
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def _zero_escapes(token):
|
|
88
|
+
if token.prefix in ("r", "rb") or token.kind == "BYTES" and token.prefix not in ("b", "rb"):
|
|
89
|
+
return []
|
|
90
|
+
spelling, found = token.lexeme, []
|
|
91
|
+
index = len(token.prefix) + (3 if spelling[len(token.prefix):].startswith('"""') else 1)
|
|
92
|
+
while index < len(spelling) - 1:
|
|
93
|
+
if spelling[index] != "\\":
|
|
94
|
+
index += 1
|
|
95
|
+
continue
|
|
96
|
+
if spelling[index + 1:index + 2] == "0" and spelling[index + 2:index + 3] in tuple("0123456789"):
|
|
97
|
+
end = index + 2
|
|
98
|
+
while end < min(index + 4, len(spelling) - 1) and spelling[end] in "01234567":
|
|
99
|
+
end += 1
|
|
100
|
+
found.append((index, end, "\\x" + format(int(spelling[index + 1:end], 8), "02x")))
|
|
101
|
+
index += 2
|
|
102
|
+
return found
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def analyze(text, max_digits=REFUSE_DIGITS, source=None, *, writing=False):
|
|
106
|
+
if not isinstance(text, str):
|
|
107
|
+
raise TypeError("text must be a string")
|
|
108
|
+
max_digits = digit_option(max_digits)
|
|
109
|
+
text = text.replace("\r\n", "\n")
|
|
110
|
+
starts = [0] + [index + 1 for index, ch in enumerate(text) if ch == "\n"]
|
|
111
|
+
|
|
112
|
+
def location(index):
|
|
113
|
+
line = bisect_right(starts, index)
|
|
114
|
+
return where(line, index - starts[line - 1], source)
|
|
115
|
+
|
|
116
|
+
for index, ch in enumerate(text):
|
|
117
|
+
point = ord(ch)
|
|
118
|
+
if ((point < 0x20 and ch not in "\n\t") or 0x7F <= point <= 0x9F
|
|
119
|
+
or point in (0x2028, 0x2029, 0xFEFF, 0xFFFE, 0xFFFF)
|
|
120
|
+
or 0xD800 <= point <= 0xDFFF):
|
|
121
|
+
line = bisect_right(starts, index)
|
|
122
|
+
raise ValidationError("source character is not shared by YAML; escape it inside a quoted string",
|
|
123
|
+
line, index - starts[line - 1], source)
|
|
124
|
+
tokens, warnings = validate(text, max_digits, source)
|
|
125
|
+
check_syntax(text, source)
|
|
126
|
+
try:
|
|
127
|
+
for _ in yaml.parse(text, Loader=Loader):
|
|
128
|
+
pass
|
|
129
|
+
except yaml.YAMLError as error:
|
|
130
|
+
mark = getattr(error, "problem_mark", None)
|
|
131
|
+
raise ValidationError(getattr(error, "problem", None) or "invalid YAML syntax",
|
|
132
|
+
mark.line + 1 if mark else 1, mark.column if mark else 0, source) from None
|
|
133
|
+
edits, notices, literals, depth = [], Notices(), {}, 0
|
|
134
|
+
for index, token in enumerate(tokens):
|
|
135
|
+
kind = token.kind
|
|
136
|
+
begin = starts[token.line - 1] + token.col
|
|
137
|
+
here = where(token.line, token.col, source)
|
|
138
|
+
if kind in ("STRING", "QKEY", "BYTES"):
|
|
139
|
+
literals[begin] = len(token.lexeme)
|
|
140
|
+
zeros = _zero_escapes(token)
|
|
141
|
+
if kind != "BYTES":
|
|
142
|
+
if token.prefix == "r":
|
|
143
|
+
warnings.append(f"warning: {here}: YAML reads the raw literal including its prefix/quotes; a shared string spelling is {quote(token.value)}")
|
|
144
|
+
elif token.lexeme.startswith("'") and "\\" in token.lexeme:
|
|
145
|
+
warnings.append(f"warning: {here}: YAML single quotes do not decode backslashes; a shared string spelling is {quote(token.value)}")
|
|
146
|
+
for start, end, replacement in zeros:
|
|
147
|
+
warnings.append(f"warning: {location(begin + start)}: active \\0 followed by a decimal digit is ambiguous; write {replacement} instead of {token.lexeme[start:end]}")
|
|
148
|
+
if zeros and writing:
|
|
149
|
+
if kind != "BYTES" and token.lexeme.startswith("'"):
|
|
150
|
+
edits.append((begin, begin + len(token.lexeme), quote(token.value)))
|
|
151
|
+
else:
|
|
152
|
+
edits.extend((begin + start, begin + end, replacement) for start, end, replacement in zeros)
|
|
153
|
+
if kind == "BYTES":
|
|
154
|
+
notices.add("bytes", here)
|
|
155
|
+
elif kind == "KEYWORD" and token.value is None:
|
|
156
|
+
notices.add("None", here)
|
|
157
|
+
elif kind in ("INT", "FLOAT"):
|
|
158
|
+
patterns = (_INT11, _INT12) if kind == "INT" else (_FLOAT11, _FLOAT12)
|
|
159
|
+
if not all(pattern.fullmatch(token.lexeme) for pattern in patterns):
|
|
160
|
+
if kind == "INT" and decimal_digits(token.value) > max_digits:
|
|
161
|
+
raise ValidationError("a shared decimal spelling exceeds max_digits", token.line, token.col, source)
|
|
162
|
+
alternative = decimal_text(token.value) if kind == "INT" else float_text(token.value)
|
|
163
|
+
warnings.append(f"warning: {here}: YAML 1.1/1.2 interpret this numeric spelling differently; write {alternative} for a shared {'integer' if kind == 'INT' else 'float'} spelling")
|
|
164
|
+
elif kind == "NAME":
|
|
165
|
+
converted = implicit_type(token.lexeme)
|
|
166
|
+
if converted:
|
|
167
|
+
warnings.append(key_warning(token.lexeme, converted, here, writing=writing))
|
|
168
|
+
if writing:
|
|
169
|
+
edits.append((begin, begin + len(token.lexeme), quote(token.value)))
|
|
170
|
+
if kind in ("LBRACKET", "LBRACE"):
|
|
171
|
+
depth += 1
|
|
172
|
+
elif kind in ("RBRACKET", "RBRACE"):
|
|
173
|
+
depth -= 1
|
|
174
|
+
elif kind == "COLON":
|
|
175
|
+
key = tokens[index - 1]
|
|
176
|
+
if key.line != token.line or token.col - key.col > 1024:
|
|
177
|
+
raise ValidationError("an implicit key and ':' must share a line within 1,024 source characters", key.line, key.col, source)
|
|
178
|
+
following = text[begin + 1:begin + 2]
|
|
179
|
+
shared = depth and (key.kind == "QKEY" or following in ("[", "{"))
|
|
180
|
+
if following and following not in " \t\n" and not shared:
|
|
181
|
+
warnings.append(f"warning: {here}: YAML reads a different field boundary; separate ':' from its value with whitespace")
|
|
182
|
+
if writing:
|
|
183
|
+
edits.append((begin + 1, begin + 1, " "))
|
|
184
|
+
index = 0
|
|
185
|
+
while index < len(text):
|
|
186
|
+
if index in literals:
|
|
187
|
+
index += literals[index]
|
|
188
|
+
elif text[index] == "#":
|
|
189
|
+
if index and text[index - 1] not in " \t\n":
|
|
190
|
+
warnings.append(f"warning: {location(index)}: YAML may consume the inline comment as scalar content; separate it with whitespace")
|
|
191
|
+
if writing:
|
|
192
|
+
edits.append((index, index, " "))
|
|
193
|
+
end = text.find("\n", index)
|
|
194
|
+
index = len(text) if end < 0 else end + 1
|
|
195
|
+
else:
|
|
196
|
+
index += 1
|
|
197
|
+
return text, warnings, list(notices.messages()), edits
|