refactor: add tomllib preference

This commit is contained in:
Daylin Morgan 2023-10-17 09:38:32 -05:00 committed by daylin
parent 12d7f6c322
commit c8051f9513

View file

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