Files
moloapp/src/vendors/mistral/tools/minyaml.py
T

283 lines
8.3 KiB
Python

"""Minimal YAML reader (python3 stdlib only) for the block-style OpenAPI
documents Mistral publishes. Supports block mappings and sequences (also a
sequence at the same indent as its key), plain / single / double quoted
scalars (multi-line), flow [] and {} (multi-line), block scalars | and >.
Not a general YAML parser: no anchors, tags, merge keys or multi-document
streams. check_spec.py self-tests it against PyYAML when PyYAML is installed.
"""
import re
class YamlError(Exception):
pass
def _strip_comment(s):
out, q = [], None
for i, c in enumerate(s):
if q:
if c == q:
q = None
out.append(c)
elif c in "'\"" and (i == 0 or s[i - 1] in " [{,:"):
q = c
out.append(c)
elif c == "#" and (i == 0 or s[i - 1] == " "):
break
else:
out.append(c)
return "".join(out).rstrip()
def _plain(s):
if s in ("", "~", "null", "Null", "NULL"):
return None
if s in ("true", "True", "TRUE"):
return True
if s in ("false", "False", "FALSE"):
return False
if re.fullmatch(r"[-+]?\d+", s):
return int(s)
if re.fullmatch(r"[-+]?(\d+\.\d*|\.\d+|\d+)([eE][-+]?\d+)?", s):
return float(s)
return s
def _unquote(s):
if s[0] == "'":
return s[1:-1].replace("''", "'")
out, i, body = [], 0, s[1:-1]
esc = {"n": "\n", "t": "\t", "r": "\r", "\\": "\\", '"': '"', "/": "/", "0": "\0"}
while i < len(body):
c = body[i]
if c == "\\" and i + 1 < len(body):
n = body[i + 1]
if n == "u":
out.append(chr(int(body[i + 2:i + 6], 16)))
i += 6
continue
out.append(esc.get(n, n))
i += 2
continue
out.append(c)
i += 1
return "".join(out)
def _quote_end(s, start):
q = s[start]
i = start + 1
while i < len(s):
if q == "'" and s[i] == "'":
if i + 1 < len(s) and s[i + 1] == "'":
i += 2
continue
return i
if q == '"' and s[i] == "\\":
i += 2
continue
if q == '"' and s[i] == '"':
return i
i += 1
return -1
def _flow(s, i=0):
"""Parse one flow value starting at s[i]; returns (value, next index)."""
while i < len(s) and s[i] == " ":
i += 1
if s[i] == "[":
out, i = [], i + 1
while True:
while s[i] in " ,":
i += 1
if s[i] == "]":
return out, i + 1
v, i = _flow(s, i)
out.append(v)
if s[i] == "{":
out, i = {}, i + 1
while True:
while s[i] in " ,":
i += 1
if s[i] == "}":
return out, i + 1
k, i = _flow_scalar(s, i, ":")
while s[i] == " ":
i += 1
if s[i] == ":":
i += 1
v, i = _flow(s, i)
else:
v = None
out[k] = v
return _flow_scalar(s, i, ",]}")
def _flow_scalar(s, i, stops):
while s[i] == " ":
i += 1
if s[i] in "'\"":
e = _quote_end(s, i)
return _unquote(s[i:e + 1]), e + 1
j = i
while j < len(s) and s[j] not in stops:
j += 1
return _plain(s[i:j].strip()), j
def _balanced(s):
depth, q = 0, None
for i, c in enumerate(s):
if q:
if c == q:
q = None
elif c in "'\"":
q = c
elif c in "[{":
depth += 1
elif c in "]}":
depth -= 1
return depth <= 0 and q is None
class _Parser:
def __init__(self, text):
self.lines = text.split("\n")
self.i = 0
self.over = {} # line index -> (indent, text) rewrites for "- key: v"
def tok(self, i):
if i in self.over:
return self.over[i]
raw = self.lines[i]
t = raw.lstrip(" ")
return len(raw) - len(t), t
def skip(self):
while self.i < len(self.lines):
ind, t = self.tok(self.i)
if t.strip() == "" or (t.startswith("#") and self.i not in self.over):
self.i += 1
continue
if t.rstrip() in ("---", "..."):
self.i += 1
continue
return True
return False
def block(self, ind):
if not self.skip():
return None
cur, t = self.tok(self.i)
if cur != ind:
raise YamlError("bad indent at line %d" % (self.i + 1))
if t == "-" or t.startswith("- "):
return self.seq(ind)
return self.map(ind)
def seq(self, ind):
out = []
while self.skip():
cur, t = self.tok(self.i)
if cur != ind or not (t == "-" or t.startswith("- ")):
break
rest = t[1:]
stripped = rest.lstrip(" ")
if _strip_comment(stripped) == "":
self.i += 1
if self.skip() and self.tok(self.i)[0] > ind:
out.append(self.block(self.tok(self.i)[0]))
else:
out.append(None)
continue
off = 1 + len(rest) - len(stripped)
is_key = re.match(r"""^('[^']*'|"[^"]*"|[^\s'"\[{#!&*|>-][^:]*?|-[^\s][^:]*?):( |$)""", stripped)
if is_key or stripped == "-" or stripped.startswith("- "):
self.over[self.i] = (ind + off, stripped)
out.append(self.block(ind + off))
else:
self.i += 1
out.append(self.value(stripped, ind, self.i - 1))
return out
def split_key(self, t):
if t[0] in "'\"":
e = _quote_end(t, 0)
key = _unquote(t[:e + 1])
rest = t[e + 1:]
if not rest.startswith(":"):
raise YamlError("bad key: " + t[:60])
return key, rest[1:].strip()
m = re.match(r"^(.*?):(?: +(.*))?$", t)
if not m:
raise YamlError("not a mapping line: " + t[:60])
return m.group(1), (m.group(2) or "").strip()
def map(self, ind):
out = {}
while self.skip():
cur, t = self.tok(self.i)
if cur != ind or t == "-" or t.startswith("- "):
break
key, rest = self.split_key(t)
line_no = self.i
self.i += 1
out[key] = self.value(rest, ind, line_no)
return out
def value(self, rest, ind, line_no):
r = _strip_comment(rest)
if r == "":
if self.skip():
cur, t = self.tok(self.i)
if cur > ind:
return self.block(cur)
if cur == ind and (t == "-" or t.startswith("- ")):
return self.seq(ind)
return None
if r[0] in "|>" and re.fullmatch(r"[|>][-+0-9]*", r):
return self.block_scalar(ind, r[0])
if r[0] in "[{":
s = r
while not _balanced(s) and self.i < len(self.lines):
s += " " + _strip_comment(self.lines[self.i].strip())
self.i += 1
return _flow(s)[0]
if r[0] in "'\"":
s = r
while _quote_end(s, 0) < 0 and self.i < len(self.lines):
s += "\n" + self.lines[self.i].strip()
self.i += 1
return _unquote(s[:_quote_end(s, 0) + 1])
parts = [r]
while self.i < len(self.lines):
raw = self.lines[self.i]
t = raw.strip()
if t == "":
self.i += 1
continue
if len(raw) - len(raw.lstrip(" ")) <= ind:
break
parts.append(_strip_comment(t))
self.i += 1
return _plain(" ".join(parts)) if len(parts) == 1 else " ".join(parts)
def block_scalar(self, ind, style):
buf = []
while self.i < len(self.lines):
raw = self.lines[self.i]
if raw.strip() != "" and len(raw) - len(raw.lstrip(" ")) <= ind:
break
buf.append(raw.strip())
self.i += 1
return ("\n" if style == "|" else " ").join(buf).strip()
def loads(text):
p = _Parser(text)
v = p.block(0) if p.skip() else None
if p.skip():
raise YamlError("trailing content at line %d" % (p.i + 1))
return v