refactor: add tomllib preference

This commit is contained in:
Daylin Morgan 2023-10-17 09:38:32 -05:00
parent a96d0a6bbe
commit 3bfdf5c095
Signed by: daylin
GPG key ID: C1E52E7DD81DF79F

View file

@ -57,13 +57,14 @@ __version__ = "2023.1003-pep723"
##### START VENDORED TOMLI #####
try:
from tomllib import loads as toml_loads
except ImportError:
# MODIFIED FROM https://github.com/hukkin/tomli
# see below for original license
# SPDX-License-Identifier: MIT
# SPDX-FileCopyrightText: 2021 Taneli Hukkinen
# Licensed to PSF under a Contributor Agreement.
import string # noqa
from collections.abc import Iterable # noqa
from functools import lru_cache # noqa
@ -111,9 +112,9 @@ __tomli__RE_DATETIME = re.compile(
flags=re.VERBOSE,
)
def __tomli__match_to_datetime(match: re.Match) -> datetime | date:
"""Convert a `__tomli__RE_DATETIME` match to `datetime.datetime` or `datetime.date`.
"""Convert a `__tomli__RE_DATETIME` match to `datetime.datetime`
or `datetime.date`.
Raises ValueError if the match does not correspond to a valid date
or datetime.
"""
@ -145,7 +146,6 @@ def __tomli__match_to_datetime(match: re.Match) -> datetime | date:
tz = None
return datetime(year, month, day, hour, minute, sec, micros, tzinfo=tz)
@lru_cache(maxsize=None)
def __tomli__cached_tz(hour_str: str, minute_str: str, sign_str: str) -> timezone:
sign = 1 if sign_str == "+" else -1
@ -156,19 +156,16 @@ def __tomli__cached_tz(hour_str: str, minute_str: str, sign_str: str) -> timezon
)
)
def __tomli__match_to_localtime(match: re.Match) -> time:
hour_str, minute_str, sec_str, micros_str = match.groups()
micros = int(micros_str.ljust(6, "0")) if micros_str else 0
return time(int(hour_str), int(minute_str), int(sec_str), micros)
def __tomli__match_to_number(match: re.Match, parse_float: ParseFloat) -> Any:
if match.group("floatpart"):
return parse_float(match.group())
return int(match.group(), 0)
__tomli__ASCII_CTRL = frozenset(chr(i) for i in range(32)) | frozenset(chr(127))
# Neither of these sets include quotation mark or backslash. They are
# currently handled as separate cases in the parser functions.
@ -196,11 +193,9 @@ __tomli__BASIC_STR_ESCAPE_REPLACEMENTS = MappingProxyType(
}
)
class TOMLDecodeError(ValueError):
"""An error raised if a document is not valid TOML."""
def __tomli__load(
__fp: IO[bytes], *, parse_float: ParseFloat = float
) -> dict[str, Any]:
@ -214,7 +209,6 @@ def __tomli__load(
) from None
return __tomli__loads(s, parse_float=parse_float)
def __tomli__loads(
__s: str, *, parse_float: ParseFloat = float
) -> dict[str, Any]: # noqa: C901
@ -276,7 +270,6 @@ def __tomli__loads(
pos += 1
return out.data.dict
class Flags:
"""Flags that map to parsed keys/namespaces."""
@ -314,7 +307,11 @@ class Flags:
cont[k] = {"flags": set(), "recursive_flags": set(), "nested": {}}
cont = cont[k]["nested"]
if key_stem not in cont:
cont[key_stem] = {"flags": set(), "recursive_flags": set(), "nested": {}}
cont[key_stem] = {
"flags": set(),
"recursive_flags": set(),
"nested": {},
}
cont[key_stem]["recursive_flags" if recursive else "flags"].add(flag)
def is_(self, key: Key, flag: int) -> bool:
@ -334,7 +331,6 @@ class Flags:
return flag in cont["flags"] or flag in cont["recursive_flags"]
return False
class NestedDict:
def __init__(self) -> None:
# The parsed content of the TOML document
@ -368,12 +364,10 @@ class NestedDict:
else:
cont[last_key] = [{}]
class Output(NamedTuple):
data: NestedDict
flags: Flags
def __tomli__skip_chars(src: str, pos: Pos, chars: Iterable[str]) -> Pos:
try:
while src[pos] in chars:
@ -382,7 +376,6 @@ def __tomli__skip_chars(src: str, pos: Pos, chars: Iterable[str]) -> Pos:
pass
return pos
def __tomli__skip_until(
src: str,
pos: Pos,
@ -396,14 +389,17 @@ def __tomli__skip_until(
except ValueError:
new_pos = len(src)
if error_on_eof:
raise __tomli__suffixed_err(src, new_pos, f"Expected {expect!r}") from None
raise __tomli__suffixed_err(
src, new_pos, f"Expected {expect!r}"
) from None
if not error_on.isdisjoint(src[pos:new_pos]):
while src[pos] not in error_on:
pos += 1
raise __tomli__suffixed_err(src, pos, f"Found invalid character {src[pos]!r}")
raise __tomli__suffixed_err(
src, pos, f"Found invalid character {src[pos]!r}"
)
return new_pos
def __tomli__skip_comment(src: str, pos: Pos) -> Pos:
try:
char: str | None = src[pos]
@ -419,7 +415,6 @@ def __tomli__skip_comment(src: str, pos: Pos) -> Pos:
)
return pos
def __tomli__skip_comments_and_array_ws(src: str, pos: Pos) -> Pos:
while True:
pos_before_skip = pos
@ -428,7 +423,6 @@ def __tomli__skip_comments_and_array_ws(src: str, pos: Pos) -> Pos:
if pos == pos_before_skip:
return pos
def __tomli__create_dict_rule(src: str, pos: Pos, out: Output) -> tuple[Pos, Key]:
pos += 1 # Skip "["
pos = __tomli__skip_chars(src, pos, __tomli__TOML_WS)
@ -446,7 +440,6 @@ def __tomli__create_dict_rule(src: str, pos: Pos, out: Output) -> tuple[Pos, Key
)
return pos + 1, key
def __tomli__create_list_rule(src: str, pos: Pos, out: Output) -> tuple[Pos, Key]:
pos += 2 # Skip "[["
pos = __tomli__skip_chars(src, pos, __tomli__TOML_WS)
@ -469,7 +462,6 @@ def __tomli__create_list_rule(src: str, pos: Pos, out: Output) -> tuple[Pos, Key
)
return pos + 2, key
def __tomli__key_value_rule(
src: str, pos: Pos, out: Output, header: Key, parse_float: ParseFloat
) -> Pos:
@ -502,7 +494,6 @@ def __tomli__key_value_rule(
nest[key_stem] = value
return pos
def __tomli__parse_key_value_pair(
src: str, pos: Pos, parse_float: ParseFloat
) -> tuple[Pos, Key, Any]:
@ -520,7 +511,6 @@ def __tomli__parse_key_value_pair(
pos, value = __tomli__parse_value(src, pos, parse_float)
return pos, key, value
def __tomli__parse_key(src: str, pos: Pos) -> tuple[Pos, Key]:
pos, key_part = __tomli__parse_key_part(src, pos)
key: Key = (key_part,)
@ -538,7 +528,6 @@ def __tomli__parse_key(src: str, pos: Pos) -> tuple[Pos, Key]:
key += (key_part,)
pos = __tomli__skip_chars(src, pos, __tomli__TOML_WS)
def __tomli__parse_key_part(src: str, pos: Pos) -> tuple[Pos, str]:
try:
char: str | None = src[pos]
@ -552,14 +541,14 @@ def __tomli__parse_key_part(src: str, pos: Pos) -> tuple[Pos, str]:
return __tomli__parse_literal_str(src, pos)
if char == '"':
return __tomli__parse_one_line_basic_str(src, pos)
raise __tomli__suffixed_err(src, pos, "Invalid initial character for a key part")
raise __tomli__suffixed_err(
src, pos, "Invalid initial character for a key part"
)
def __tomli__parse_one_line_basic_str(src: str, pos: Pos) -> tuple[Pos, str]:
pos += 1
return __tomli__parse_basic_str(src, pos, multiline=False)
def __tomli__parse_array(
src: str, pos: Pos, parse_float: ParseFloat
) -> tuple[Pos, list]:
@ -582,7 +571,6 @@ def __tomli__parse_array(
if src.startswith("]", pos):
return pos + 1, array
def __tomli__parse_inline_table(
src: str, pos: Pos, parse_float: ParseFloat
) -> tuple[Pos, dict]:
@ -602,7 +590,9 @@ def __tomli__parse_inline_table(
try:
nest = nested_dict.get_or_create_nest(key_parent, access_lists=False)
except KeyError:
raise __tomli__suffixed_err(src, pos, "Cannot overwrite a value") from None
raise __tomli__suffixed_err(
src, pos, "Cannot overwrite a value"
) from None
if key_stem in nest:
raise __tomli__suffixed_err(
src, pos, f"Duplicate inline table key {key_stem!r}"
@ -619,7 +609,6 @@ def __tomli__parse_inline_table(
pos += 1
pos = __tomli__skip_chars(src, pos, __tomli__TOML_WS)
def __tomli__parse_basic_str_escape(
src: str, pos: Pos, *, multiline: bool = False
) -> tuple[Pos, str]:
@ -646,13 +635,15 @@ def __tomli__parse_basic_str_escape(
try:
return pos, __tomli__BASIC_STR_ESCAPE_REPLACEMENTS[escape_id]
except KeyError:
raise __tomli__suffixed_err(src, pos, "Unescaped '\\' in a string") from None
raise __tomli__suffixed_err(
src, pos, "Unescaped '\\' in a string"
) from None
def __tomli__parse_basic_str_escape_multiline(src: str, pos: Pos) -> tuple[Pos, str]:
def __tomli__parse_basic_str_escape_multiline(
src: str, pos: Pos
) -> tuple[Pos, str]:
return __tomli__parse_basic_str_escape(src, pos, multiline=True)
def __tomli__parse_hex_char(src: str, pos: Pos, hex_len: int) -> tuple[Pos, str]:
hex_str = src[pos : pos + hex_len]
if len(hex_str) != hex_len or not __tomli__HEXDIGIT_CHARS.issuperset(hex_str):
@ -665,16 +656,18 @@ def __tomli__parse_hex_char(src: str, pos: Pos, hex_len: int) -> tuple[Pos, str]
)
return pos, chr(hex_int)
def __tomli__parse_literal_str(src: str, pos: Pos) -> tuple[Pos, str]:
pos += 1 # Skip starting apostrophe
start_pos = pos
pos = __tomli__skip_until(
src, pos, "'", error_on=__tomli__ILLEGAL_LITERAL_STR_CHARS, error_on_eof=True
src,
pos,
"'",
error_on=__tomli__ILLEGAL_LITERAL_STR_CHARS,
error_on_eof=True,
)
return pos + 1, src[start_pos:pos] # Skip ending apostrophe
def __tomli__parse_multiline_str(
src: str, pos: Pos, *, literal: bool
) -> tuple[Pos, str]:
@ -705,8 +698,9 @@ def __tomli__parse_multiline_str(
pos += 1
return pos, result + (delim * 2)
def __tomli__parse_basic_str(src: str, pos: Pos, *, multiline: bool) -> tuple[Pos, str]:
def __tomli__parse_basic_str(
src: str, pos: Pos, *, multiline: bool
) -> tuple[Pos, str]:
if multiline:
error_on = __tomli__ILLEGAL_MULTILINE_BASIC_STR_CHARS
parse_escapes = __tomli__parse_basic_str_escape_multiline
@ -737,7 +731,6 @@ def __tomli__parse_basic_str(src: str, pos: Pos, *, multiline: bool) -> tuple[Po
raise __tomli__suffixed_err(src, pos, f"Illegal character {char!r}")
pos += 1
def __tomli__parse_value( # noqa: C901
src: str, pos: Pos, parse_float: ParseFloat
) -> tuple[Pos, Any]:
@ -785,7 +778,9 @@ def __tomli__parse_value( # noqa: C901
# char, so needs to be located after handling of dates and times.
number_match = __tomli__RE_NUMBER.match(src, pos)
if number_match:
return number_match.end(), __tomli__match_to_number(number_match, parse_float)
return number_match.end(), __tomli__match_to_number(
number_match, parse_float
)
# Special floats
first_three = src[pos : pos + 3]
if first_three in {"inf", "nan"}:
@ -795,7 +790,6 @@ def __tomli__parse_value( # noqa: C901
return pos + 4, parse_float(first_four)
raise __tomli__suffixed_err(src, pos, "Invalid value")
def __tomli__suffixed_err(src: str, pos: Pos, msg: str) -> TOMLDecodeError:
"""Return a `TOMLDecodeError` where error message is suffixed with
coordinates in source."""
@ -812,11 +806,9 @@ def __tomli__suffixed_err(src: str, pos: Pos, msg: str) -> TOMLDecodeError:
return TOMLDecodeError(f"{msg} (at {coord_repr(src, pos)})")
def __tomli__is_unicode_scalar_value(codepoint: int) -> bool:
return (0 <= codepoint <= 55295) or (57344 <= codepoint <= 1114111)
def __tomli__make_safe_parse_float(parse_float: ParseFloat) -> ParseFloat:
"""A decorator to make `parse_float` safe.
`parse_float` must not return dicts or lists, because these types
@ -836,16 +828,11 @@ def __tomli__make_safe_parse_float(parse_float: ParseFloat) -> ParseFloat:
return safe_parse_float
toml_loads = __tomli__loads
##### END VENDORED TOMLI #####
# fmt: on
# fmt: on
class Spinner:
"""spinner modified from:
https://raw.githubusercontent.com/Tagar/stuff/master/spinner.py
@ -2025,7 +2012,7 @@ METADATA_BLOCK = (
)
def read_metadata_block(script: str) -> dict | None:
def read_metadata_block(script: str) -> dict:
name = "pyproject"
matches = list(
filter(lambda m: m.group("type") == name, re.finditer(METADATA_BLOCK, script))
@ -2033,11 +2020,11 @@ def read_metadata_block(script: str) -> dict | None:
if len(matches) > 1:
raise ValueError(f"Multiple {name} blocks found")
elif len(matches) == 1:
return __tomli__loads(
return toml_loads(
"\n".join((line[2:] for line in matches[0].group(0).splitlines()[1:-1]))
)
else:
return None
return {}
# DEPENDENCY_BLOCK_MARKER = r"(?i)^#\s+script\s+dependencies:\s*$"