updates
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
from tomlkit.api import TOMLDocument
|
||||
from tomlkit.api import aot
|
||||
from tomlkit.api import array
|
||||
from tomlkit.api import boolean
|
||||
from tomlkit.api import comment
|
||||
from tomlkit.api import date
|
||||
from tomlkit.api import datetime
|
||||
from tomlkit.api import document
|
||||
from tomlkit.api import dump
|
||||
from tomlkit.api import dumps
|
||||
from tomlkit.api import float_
|
||||
from tomlkit.api import inline_table
|
||||
from tomlkit.api import integer
|
||||
from tomlkit.api import item
|
||||
from tomlkit.api import key
|
||||
from tomlkit.api import key_value
|
||||
from tomlkit.api import load
|
||||
from tomlkit.api import loads
|
||||
from tomlkit.api import nl
|
||||
from tomlkit.api import parse
|
||||
from tomlkit.api import register_encoder
|
||||
from tomlkit.api import string
|
||||
from tomlkit.api import table
|
||||
from tomlkit.api import time
|
||||
from tomlkit.api import unregister_encoder
|
||||
from tomlkit.api import value
|
||||
from tomlkit.api import ws
|
||||
|
||||
|
||||
__version__ = "0.13.3"
|
||||
__all__ = [
|
||||
"TOMLDocument",
|
||||
"aot",
|
||||
"array",
|
||||
"boolean",
|
||||
"comment",
|
||||
"date",
|
||||
"datetime",
|
||||
"document",
|
||||
"dump",
|
||||
"dumps",
|
||||
"float_",
|
||||
"inline_table",
|
||||
"integer",
|
||||
"item",
|
||||
"key",
|
||||
"key_value",
|
||||
"load",
|
||||
"loads",
|
||||
"nl",
|
||||
"parse",
|
||||
"register_encoder",
|
||||
"string",
|
||||
"table",
|
||||
"time",
|
||||
"unregister_encoder",
|
||||
"value",
|
||||
"ws",
|
||||
]
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
22
Backend/venv/lib/python3.12/site-packages/tomlkit/_compat.py
Normal file
22
Backend/venv/lib/python3.12/site-packages/tomlkit/_compat.py
Normal file
@@ -0,0 +1,22 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import contextlib
|
||||
import sys
|
||||
|
||||
from typing import Any
|
||||
|
||||
|
||||
PY38 = sys.version_info >= (3, 8)
|
||||
|
||||
|
||||
def decode(string: Any, encodings: list[str] | None = None):
|
||||
if not isinstance(string, bytes):
|
||||
return string
|
||||
|
||||
encodings = encodings or ["utf-8", "latin1", "ascii"]
|
||||
|
||||
for encoding in encodings:
|
||||
with contextlib.suppress(UnicodeEncodeError, UnicodeDecodeError):
|
||||
return string.decode(encoding)
|
||||
|
||||
return string.decode(encodings[0], errors="ignore")
|
||||
82
Backend/venv/lib/python3.12/site-packages/tomlkit/_types.py
Normal file
82
Backend/venv/lib/python3.12/site-packages/tomlkit/_types.py
Normal file
@@ -0,0 +1,82 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
from typing import Any
|
||||
from typing import TypeVar
|
||||
|
||||
|
||||
WT = TypeVar("WT", bound="WrapperType")
|
||||
|
||||
if TYPE_CHECKING: # pragma: no cover
|
||||
# Define _CustomList and _CustomDict as a workaround for:
|
||||
# https://github.com/python/mypy/issues/11427
|
||||
#
|
||||
# According to this issue, the typeshed contains a "lie"
|
||||
# (it adds MutableSequence to the ancestry of list and MutableMapping to
|
||||
# the ancestry of dict) which completely messes with the type inference for
|
||||
# Table, InlineTable, Array and Container.
|
||||
#
|
||||
# Importing from builtins is preferred over simple assignment, see issues:
|
||||
# https://github.com/python/mypy/issues/8715
|
||||
# https://github.com/python/mypy/issues/10068
|
||||
from builtins import dict as _CustomDict
|
||||
from builtins import float as _CustomFloat
|
||||
from builtins import int as _CustomInt
|
||||
from builtins import list as _CustomList
|
||||
from typing import Callable
|
||||
from typing import Concatenate
|
||||
from typing import ParamSpec
|
||||
from typing import Protocol
|
||||
|
||||
P = ParamSpec("P")
|
||||
|
||||
class WrapperType(Protocol):
|
||||
def _new(self: WT, value: Any) -> WT: ...
|
||||
|
||||
else:
|
||||
from collections.abc import MutableMapping
|
||||
from collections.abc import MutableSequence
|
||||
from numbers import Integral
|
||||
from numbers import Real
|
||||
|
||||
class _CustomList(MutableSequence, list):
|
||||
"""Adds MutableSequence mixin while pretending to be a builtin list"""
|
||||
|
||||
def __add__(self, other):
|
||||
new_list = self.copy()
|
||||
new_list.extend(other)
|
||||
return new_list
|
||||
|
||||
def __iadd__(self, other):
|
||||
self.extend(other)
|
||||
return self
|
||||
|
||||
class _CustomDict(MutableMapping, dict):
|
||||
"""Adds MutableMapping mixin while pretending to be a builtin dict"""
|
||||
|
||||
def __or__(self, other):
|
||||
new_dict = self.copy()
|
||||
new_dict.update(other)
|
||||
return new_dict
|
||||
|
||||
def __ior__(self, other):
|
||||
self.update(other)
|
||||
return self
|
||||
|
||||
class _CustomInt(Integral, int):
|
||||
"""Adds Integral mixin while pretending to be a builtin int"""
|
||||
|
||||
class _CustomFloat(Real, float):
|
||||
"""Adds Real mixin while pretending to be a builtin float"""
|
||||
|
||||
|
||||
def wrap_method(
|
||||
original_method: Callable[Concatenate[WT, P], Any],
|
||||
) -> Callable[Concatenate[WT, P], Any]:
|
||||
def wrapper(self: WT, *args: P.args, **kwargs: P.kwargs) -> Any:
|
||||
result = original_method(self, *args, **kwargs)
|
||||
if result is NotImplemented:
|
||||
return result
|
||||
return self._new(result)
|
||||
|
||||
return wrapper
|
||||
158
Backend/venv/lib/python3.12/site-packages/tomlkit/_utils.py
Normal file
158
Backend/venv/lib/python3.12/site-packages/tomlkit/_utils.py
Normal file
@@ -0,0 +1,158 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
|
||||
from collections.abc import Mapping
|
||||
from datetime import date
|
||||
from datetime import datetime
|
||||
from datetime import time
|
||||
from datetime import timedelta
|
||||
from datetime import timezone
|
||||
from typing import Collection
|
||||
|
||||
from tomlkit._compat import decode
|
||||
|
||||
|
||||
RFC_3339_LOOSE = re.compile(
|
||||
"^"
|
||||
r"(([0-9]+)-(\d{2})-(\d{2}))?" # Date
|
||||
"("
|
||||
"([Tt ])?" # Separator
|
||||
r"(\d{2}):(\d{2}):(\d{2})(\.([0-9]+))?" # Time
|
||||
r"(([Zz])|([\+|\-]([01][0-9]|2[0-3]):([0-5][0-9])))?" # Timezone
|
||||
")?"
|
||||
"$"
|
||||
)
|
||||
|
||||
RFC_3339_DATETIME = re.compile(
|
||||
"^"
|
||||
"([0-9]+)-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])" # Date
|
||||
"[Tt ]" # Separator
|
||||
r"([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]|60)(\.([0-9]+))?" # Time
|
||||
r"(([Zz])|([\+|\-]([01][0-9]|2[0-3]):([0-5][0-9])))?" # Timezone
|
||||
"$"
|
||||
)
|
||||
|
||||
RFC_3339_DATE = re.compile("^([0-9]+)-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])$")
|
||||
|
||||
RFC_3339_TIME = re.compile(
|
||||
r"^([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]|60)(\.([0-9]+))?$"
|
||||
)
|
||||
|
||||
_utc = timezone(timedelta(), "UTC")
|
||||
|
||||
|
||||
def parse_rfc3339(string: str) -> datetime | date | time:
|
||||
m = RFC_3339_DATETIME.match(string)
|
||||
if m:
|
||||
year = int(m.group(1))
|
||||
month = int(m.group(2))
|
||||
day = int(m.group(3))
|
||||
hour = int(m.group(4))
|
||||
minute = int(m.group(5))
|
||||
second = int(m.group(6))
|
||||
microsecond = 0
|
||||
|
||||
if m.group(7):
|
||||
microsecond = int((f"{m.group(8):<06s}")[:6])
|
||||
|
||||
if m.group(9):
|
||||
# Timezone
|
||||
tz = m.group(9)
|
||||
if tz.upper() == "Z":
|
||||
tzinfo = _utc
|
||||
else:
|
||||
sign = m.group(11)[0]
|
||||
hour_offset, minute_offset = int(m.group(12)), int(m.group(13))
|
||||
offset = timedelta(seconds=hour_offset * 3600 + minute_offset * 60)
|
||||
if sign == "-":
|
||||
offset = -offset
|
||||
|
||||
tzinfo = timezone(offset, f"{sign}{m.group(12)}:{m.group(13)}")
|
||||
|
||||
return datetime(
|
||||
year, month, day, hour, minute, second, microsecond, tzinfo=tzinfo
|
||||
)
|
||||
else:
|
||||
return datetime(year, month, day, hour, minute, second, microsecond)
|
||||
|
||||
m = RFC_3339_DATE.match(string)
|
||||
if m:
|
||||
year = int(m.group(1))
|
||||
month = int(m.group(2))
|
||||
day = int(m.group(3))
|
||||
|
||||
return date(year, month, day)
|
||||
|
||||
m = RFC_3339_TIME.match(string)
|
||||
if m:
|
||||
hour = int(m.group(1))
|
||||
minute = int(m.group(2))
|
||||
second = int(m.group(3))
|
||||
microsecond = 0
|
||||
|
||||
if m.group(4):
|
||||
microsecond = int((f"{m.group(5):<06s}")[:6])
|
||||
|
||||
return time(hour, minute, second, microsecond)
|
||||
|
||||
raise ValueError("Invalid RFC 339 string")
|
||||
|
||||
|
||||
# https://toml.io/en/v1.0.0#string
|
||||
CONTROL_CHARS = frozenset(chr(c) for c in range(0x20)) | {chr(0x7F)}
|
||||
_escaped = {
|
||||
"b": "\b",
|
||||
"t": "\t",
|
||||
"n": "\n",
|
||||
"f": "\f",
|
||||
"r": "\r",
|
||||
'"': '"',
|
||||
"\\": "\\",
|
||||
}
|
||||
_compact_escapes = {
|
||||
**{v: f"\\{k}" for k, v in _escaped.items()},
|
||||
'"""': '""\\"',
|
||||
}
|
||||
_basic_escapes = CONTROL_CHARS | {'"', "\\"}
|
||||
|
||||
|
||||
def _unicode_escape(seq: str) -> str:
|
||||
return "".join(f"\\u{ord(c):04x}" for c in seq)
|
||||
|
||||
|
||||
def escape_string(s: str, escape_sequences: Collection[str] = _basic_escapes) -> str:
|
||||
s = decode(s)
|
||||
|
||||
res = []
|
||||
start = 0
|
||||
|
||||
def flush(inc=1):
|
||||
if start != i:
|
||||
res.append(s[start:i])
|
||||
|
||||
return i + inc
|
||||
|
||||
found_sequences = {seq for seq in escape_sequences if seq in s}
|
||||
|
||||
i = 0
|
||||
while i < len(s):
|
||||
for seq in found_sequences:
|
||||
seq_len = len(seq)
|
||||
if s[i:].startswith(seq):
|
||||
start = flush(seq_len)
|
||||
res.append(_compact_escapes.get(seq) or _unicode_escape(seq))
|
||||
i += seq_len - 1 # fast-forward escape sequence
|
||||
i += 1
|
||||
|
||||
flush()
|
||||
|
||||
return "".join(res)
|
||||
|
||||
|
||||
def merge_dicts(d1: dict, d2: dict) -> dict:
|
||||
for k, v in d2.items():
|
||||
if k in d1 and isinstance(d1[k], dict) and isinstance(v, Mapping):
|
||||
merge_dicts(d1[k], v)
|
||||
else:
|
||||
d1[k] = d2[k]
|
||||
312
Backend/venv/lib/python3.12/site-packages/tomlkit/api.py
Normal file
312
Backend/venv/lib/python3.12/site-packages/tomlkit/api.py
Normal file
@@ -0,0 +1,312 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import contextlib
|
||||
import datetime as _datetime
|
||||
|
||||
from collections.abc import Mapping
|
||||
from typing import IO
|
||||
from typing import Iterable
|
||||
from typing import TypeVar
|
||||
|
||||
from tomlkit._utils import parse_rfc3339
|
||||
from tomlkit.container import Container
|
||||
from tomlkit.exceptions import UnexpectedCharError
|
||||
from tomlkit.items import CUSTOM_ENCODERS
|
||||
from tomlkit.items import AoT
|
||||
from tomlkit.items import Array
|
||||
from tomlkit.items import Bool
|
||||
from tomlkit.items import Comment
|
||||
from tomlkit.items import Date
|
||||
from tomlkit.items import DateTime
|
||||
from tomlkit.items import DottedKey
|
||||
from tomlkit.items import Encoder
|
||||
from tomlkit.items import Float
|
||||
from tomlkit.items import InlineTable
|
||||
from tomlkit.items import Integer
|
||||
from tomlkit.items import Item as _Item
|
||||
from tomlkit.items import Key
|
||||
from tomlkit.items import SingleKey
|
||||
from tomlkit.items import String
|
||||
from tomlkit.items import StringType as _StringType
|
||||
from tomlkit.items import Table
|
||||
from tomlkit.items import Time
|
||||
from tomlkit.items import Trivia
|
||||
from tomlkit.items import Whitespace
|
||||
from tomlkit.items import item
|
||||
from tomlkit.parser import Parser
|
||||
from tomlkit.toml_document import TOMLDocument
|
||||
|
||||
|
||||
def loads(string: str | bytes) -> TOMLDocument:
|
||||
"""
|
||||
Parses a string into a TOMLDocument.
|
||||
|
||||
Alias for parse().
|
||||
"""
|
||||
return parse(string)
|
||||
|
||||
|
||||
def dumps(data: Mapping, sort_keys: bool = False) -> str:
|
||||
"""
|
||||
Dumps a TOMLDocument into a string.
|
||||
"""
|
||||
if not isinstance(data, (Table, InlineTable, Container)) and isinstance(
|
||||
data, Mapping
|
||||
):
|
||||
data = item(dict(data), _sort_keys=sort_keys)
|
||||
|
||||
try:
|
||||
# data should be a `Container` (and therefore implement `as_string`)
|
||||
# for all type safe invocations of this function
|
||||
return data.as_string() # type: ignore[attr-defined]
|
||||
except AttributeError as ex:
|
||||
msg = f"Expecting Mapping or TOML Table or Container, {type(data)} given"
|
||||
raise TypeError(msg) from ex
|
||||
|
||||
|
||||
def load(fp: IO[str] | IO[bytes]) -> TOMLDocument:
|
||||
"""
|
||||
Load toml document from a file-like object.
|
||||
"""
|
||||
return parse(fp.read())
|
||||
|
||||
|
||||
def dump(data: Mapping, fp: IO[str], *, sort_keys: bool = False) -> None:
|
||||
"""
|
||||
Dump a TOMLDocument into a writable file stream.
|
||||
|
||||
:param data: a dict-like object to dump
|
||||
:param sort_keys: if true, sort the keys in alphabetic order
|
||||
|
||||
:Example:
|
||||
|
||||
>>> with open("output.toml", "w") as fp:
|
||||
... tomlkit.dump(data, fp)
|
||||
"""
|
||||
fp.write(dumps(data, sort_keys=sort_keys))
|
||||
|
||||
|
||||
def parse(string: str | bytes) -> TOMLDocument:
|
||||
"""
|
||||
Parses a string or bytes into a TOMLDocument.
|
||||
"""
|
||||
return Parser(string).parse()
|
||||
|
||||
|
||||
def document() -> TOMLDocument:
|
||||
"""
|
||||
Returns a new TOMLDocument instance.
|
||||
"""
|
||||
return TOMLDocument()
|
||||
|
||||
|
||||
# Items
|
||||
def integer(raw: str | int) -> Integer:
|
||||
"""Create an integer item from a number or string."""
|
||||
return item(int(raw))
|
||||
|
||||
|
||||
def float_(raw: str | float) -> Float:
|
||||
"""Create an float item from a number or string."""
|
||||
return item(float(raw))
|
||||
|
||||
|
||||
def boolean(raw: str) -> Bool:
|
||||
"""Turn `true` or `false` into a boolean item."""
|
||||
return item(raw == "true")
|
||||
|
||||
|
||||
def string(
|
||||
raw: str,
|
||||
*,
|
||||
literal: bool = False,
|
||||
multiline: bool = False,
|
||||
escape: bool = True,
|
||||
) -> String:
|
||||
"""Create a string item.
|
||||
|
||||
By default, this function will create *single line basic* strings, but
|
||||
boolean flags (e.g. ``literal=True`` and/or ``multiline=True``)
|
||||
can be used for personalization.
|
||||
|
||||
For more information, please check the spec: `<https://toml.io/en/v1.0.0#string>`__.
|
||||
|
||||
Common escaping rules will be applied for basic strings.
|
||||
This can be controlled by explicitly setting ``escape=False``.
|
||||
Please note that, if you disable escaping, you will have to make sure that
|
||||
the given strings don't contain any forbidden character or sequence.
|
||||
"""
|
||||
type_ = _StringType.select(literal, multiline)
|
||||
return String.from_raw(raw, type_, escape)
|
||||
|
||||
|
||||
def date(raw: str) -> Date:
|
||||
"""Create a TOML date."""
|
||||
value = parse_rfc3339(raw)
|
||||
if not isinstance(value, _datetime.date):
|
||||
raise ValueError("date() only accepts date strings.")
|
||||
|
||||
return item(value)
|
||||
|
||||
|
||||
def time(raw: str) -> Time:
|
||||
"""Create a TOML time."""
|
||||
value = parse_rfc3339(raw)
|
||||
if not isinstance(value, _datetime.time):
|
||||
raise ValueError("time() only accepts time strings.")
|
||||
|
||||
return item(value)
|
||||
|
||||
|
||||
def datetime(raw: str) -> DateTime:
|
||||
"""Create a TOML datetime."""
|
||||
value = parse_rfc3339(raw)
|
||||
if not isinstance(value, _datetime.datetime):
|
||||
raise ValueError("datetime() only accepts datetime strings.")
|
||||
|
||||
return item(value)
|
||||
|
||||
|
||||
def array(raw: str = "[]") -> Array:
|
||||
"""Create an array item for its string representation.
|
||||
|
||||
:Example:
|
||||
|
||||
>>> array("[1, 2, 3]") # Create from a string
|
||||
[1, 2, 3]
|
||||
>>> a = array()
|
||||
>>> a.extend([1, 2, 3]) # Create from a list
|
||||
>>> a
|
||||
[1, 2, 3]
|
||||
"""
|
||||
return value(raw)
|
||||
|
||||
|
||||
def table(is_super_table: bool | None = None) -> Table:
|
||||
"""Create an empty table.
|
||||
|
||||
:param is_super_table: if true, the table is a super table
|
||||
|
||||
:Example:
|
||||
|
||||
>>> doc = document()
|
||||
>>> foo = table(True)
|
||||
>>> bar = table()
|
||||
>>> bar.update({'x': 1})
|
||||
>>> foo.append('bar', bar)
|
||||
>>> doc.append('foo', foo)
|
||||
>>> print(doc.as_string())
|
||||
[foo.bar]
|
||||
x = 1
|
||||
"""
|
||||
return Table(Container(), Trivia(), False, is_super_table)
|
||||
|
||||
|
||||
def inline_table() -> InlineTable:
|
||||
"""Create an inline table.
|
||||
|
||||
:Example:
|
||||
|
||||
>>> table = inline_table()
|
||||
>>> table.update({'x': 1, 'y': 2})
|
||||
>>> print(table.as_string())
|
||||
{x = 1, y = 2}
|
||||
"""
|
||||
return InlineTable(Container(), Trivia(), new=True)
|
||||
|
||||
|
||||
def aot() -> AoT:
|
||||
"""Create an array of table.
|
||||
|
||||
:Example:
|
||||
|
||||
>>> doc = document()
|
||||
>>> aot = aot()
|
||||
>>> aot.append(item({'x': 1}))
|
||||
>>> doc.append('foo', aot)
|
||||
>>> print(doc.as_string())
|
||||
[[foo]]
|
||||
x = 1
|
||||
"""
|
||||
return AoT([])
|
||||
|
||||
|
||||
def key(k: str | Iterable[str]) -> Key:
|
||||
"""Create a key from a string. When a list of string is given,
|
||||
it will create a dotted key.
|
||||
|
||||
:Example:
|
||||
|
||||
>>> doc = document()
|
||||
>>> doc.append(key('foo'), 1)
|
||||
>>> doc.append(key(['bar', 'baz']), 2)
|
||||
>>> print(doc.as_string())
|
||||
foo = 1
|
||||
bar.baz = 2
|
||||
"""
|
||||
if isinstance(k, str):
|
||||
return SingleKey(k)
|
||||
return DottedKey([key(_k) for _k in k])
|
||||
|
||||
|
||||
def value(raw: str) -> _Item:
|
||||
"""Parse a simple value from a string.
|
||||
|
||||
:Example:
|
||||
|
||||
>>> value("1")
|
||||
1
|
||||
>>> value("true")
|
||||
True
|
||||
>>> value("[1, 2, 3]")
|
||||
[1, 2, 3]
|
||||
"""
|
||||
parser = Parser(raw)
|
||||
v = parser._parse_value()
|
||||
if not parser.end():
|
||||
raise parser.parse_error(UnexpectedCharError, char=parser._current)
|
||||
return v
|
||||
|
||||
|
||||
def key_value(src: str) -> tuple[Key, _Item]:
|
||||
"""Parse a key-value pair from a string.
|
||||
|
||||
:Example:
|
||||
|
||||
>>> key_value("foo = 1")
|
||||
(Key('foo'), 1)
|
||||
"""
|
||||
return Parser(src)._parse_key_value()
|
||||
|
||||
|
||||
def ws(src: str) -> Whitespace:
|
||||
"""Create a whitespace from a string."""
|
||||
return Whitespace(src, fixed=True)
|
||||
|
||||
|
||||
def nl() -> Whitespace:
|
||||
"""Create a newline item."""
|
||||
return ws("\n")
|
||||
|
||||
|
||||
def comment(string: str) -> Comment:
|
||||
"""Create a comment item."""
|
||||
return Comment(Trivia(comment_ws=" ", comment="# " + string))
|
||||
|
||||
|
||||
E = TypeVar("E", bound=Encoder)
|
||||
|
||||
|
||||
def register_encoder(encoder: E) -> E:
|
||||
"""Add a custom encoder, which should be a function that will be called
|
||||
if the value can't otherwise be converted. It should takes a single value
|
||||
and return a TOMLKit item or raise a ``ConvertError``.
|
||||
"""
|
||||
CUSTOM_ENCODERS.append(encoder)
|
||||
return encoder
|
||||
|
||||
|
||||
def unregister_encoder(encoder: Encoder) -> None:
|
||||
"""Unregister a custom encoder."""
|
||||
with contextlib.suppress(ValueError):
|
||||
CUSTOM_ENCODERS.remove(encoder)
|
||||
946
Backend/venv/lib/python3.12/site-packages/tomlkit/container.py
Normal file
946
Backend/venv/lib/python3.12/site-packages/tomlkit/container.py
Normal file
@@ -0,0 +1,946 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import copy
|
||||
|
||||
from typing import Any
|
||||
from typing import Iterator
|
||||
|
||||
from tomlkit._compat import decode
|
||||
from tomlkit._types import _CustomDict
|
||||
from tomlkit._utils import merge_dicts
|
||||
from tomlkit.exceptions import KeyAlreadyPresent
|
||||
from tomlkit.exceptions import NonExistentKey
|
||||
from tomlkit.exceptions import TOMLKitError
|
||||
from tomlkit.items import AoT
|
||||
from tomlkit.items import Comment
|
||||
from tomlkit.items import Item
|
||||
from tomlkit.items import Key
|
||||
from tomlkit.items import Null
|
||||
from tomlkit.items import SingleKey
|
||||
from tomlkit.items import Table
|
||||
from tomlkit.items import Trivia
|
||||
from tomlkit.items import Whitespace
|
||||
from tomlkit.items import item as _item
|
||||
|
||||
|
||||
_NOT_SET = object()
|
||||
|
||||
|
||||
class Container(_CustomDict):
|
||||
"""
|
||||
A container for items within a TOMLDocument.
|
||||
|
||||
This class implements the `dict` interface with copy/deepcopy protocol.
|
||||
"""
|
||||
|
||||
def __init__(self, parsed: bool = False) -> None:
|
||||
self._map: dict[SingleKey, int | tuple[int, ...]] = {}
|
||||
self._body: list[tuple[Key | None, Item]] = []
|
||||
self._parsed = parsed
|
||||
self._table_keys = []
|
||||
|
||||
@property
|
||||
def body(self) -> list[tuple[Key | None, Item]]:
|
||||
return self._body
|
||||
|
||||
def unwrap(self) -> dict[str, Any]:
|
||||
"""Returns as pure python object (ppo)"""
|
||||
unwrapped = {}
|
||||
for k, v in self.items():
|
||||
if k is None:
|
||||
continue
|
||||
|
||||
if isinstance(k, Key):
|
||||
k = k.key
|
||||
|
||||
if hasattr(v, "unwrap"):
|
||||
v = v.unwrap()
|
||||
|
||||
if k in unwrapped:
|
||||
merge_dicts(unwrapped[k], v)
|
||||
else:
|
||||
unwrapped[k] = v
|
||||
|
||||
return unwrapped
|
||||
|
||||
@property
|
||||
def value(self) -> dict[str, Any]:
|
||||
"""The wrapped dict value"""
|
||||
d = {}
|
||||
for k, v in self._body:
|
||||
if k is None:
|
||||
continue
|
||||
|
||||
k = k.key
|
||||
v = v.value
|
||||
|
||||
if isinstance(v, Container):
|
||||
v = v.value
|
||||
|
||||
if k in d:
|
||||
merge_dicts(d[k], v)
|
||||
else:
|
||||
d[k] = v
|
||||
|
||||
return d
|
||||
|
||||
def parsing(self, parsing: bool) -> None:
|
||||
self._parsed = parsing
|
||||
|
||||
for _, v in self._body:
|
||||
if isinstance(v, Table):
|
||||
v.value.parsing(parsing)
|
||||
elif isinstance(v, AoT):
|
||||
for t in v.body:
|
||||
t.value.parsing(parsing)
|
||||
|
||||
def add(self, key: Key | Item | str, item: Item | None = None) -> Container:
|
||||
"""
|
||||
Adds an item to the current Container.
|
||||
|
||||
:Example:
|
||||
|
||||
>>> # add a key-value pair
|
||||
>>> doc.add('key', 'value')
|
||||
>>> # add a comment or whitespace or newline
|
||||
>>> doc.add(comment('# comment'))
|
||||
"""
|
||||
if item is None:
|
||||
if not isinstance(key, (Comment, Whitespace)):
|
||||
raise ValueError(
|
||||
"Non comment/whitespace items must have an associated key"
|
||||
)
|
||||
|
||||
key, item = None, key
|
||||
|
||||
return self.append(key, item)
|
||||
|
||||
def _handle_dotted_key(self, key: Key, value: Item) -> None:
|
||||
if isinstance(value, (Table, AoT)):
|
||||
raise TOMLKitError("Can't add a table to a dotted key")
|
||||
name, *mid, last = key
|
||||
name._dotted = True
|
||||
table = current = Table(Container(True), Trivia(), False, is_super_table=True)
|
||||
for _name in mid:
|
||||
_name._dotted = True
|
||||
new_table = Table(Container(True), Trivia(), False, is_super_table=True)
|
||||
current.append(_name, new_table)
|
||||
current = new_table
|
||||
|
||||
last.sep = key.sep
|
||||
current.append(last, value)
|
||||
|
||||
self.append(name, table)
|
||||
return
|
||||
|
||||
def _get_last_index_before_table(self) -> int:
|
||||
last_index = -1
|
||||
for i, (k, v) in enumerate(self._body):
|
||||
if isinstance(v, Null):
|
||||
continue # Null elements are inserted after deletion
|
||||
|
||||
if isinstance(v, Whitespace) and not v.is_fixed():
|
||||
continue
|
||||
|
||||
if isinstance(v, (Table, AoT)) and not k.is_dotted():
|
||||
break
|
||||
last_index = i
|
||||
return last_index + 1
|
||||
|
||||
def _validate_out_of_order_table(self, key: SingleKey | None = None) -> None:
|
||||
if key is None:
|
||||
for k in self._map:
|
||||
assert k is not None
|
||||
self._validate_out_of_order_table(k)
|
||||
return
|
||||
if key not in self._map or not isinstance(self._map[key], tuple):
|
||||
return
|
||||
OutOfOrderTableProxy.validate(self, self._map[key])
|
||||
|
||||
def append(
|
||||
self, key: Key | str | None, item: Item, validate: bool = True
|
||||
) -> Container:
|
||||
"""Similar to :meth:`add` but both key and value must be given."""
|
||||
if not isinstance(key, Key) and key is not None:
|
||||
key = SingleKey(key)
|
||||
|
||||
if not isinstance(item, Item):
|
||||
item = _item(item)
|
||||
|
||||
if key is not None and key.is_multi():
|
||||
self._handle_dotted_key(key, item)
|
||||
return self
|
||||
|
||||
if isinstance(item, (AoT, Table)) and item.name is None:
|
||||
item.name = key.key
|
||||
|
||||
prev = self._previous_item()
|
||||
prev_ws = isinstance(prev, Whitespace) or ends_with_whitespace(prev)
|
||||
if isinstance(item, Table):
|
||||
if not self._parsed:
|
||||
item.invalidate_display_name()
|
||||
if (
|
||||
self._body
|
||||
and not (self._parsed or item.trivia.indent or prev_ws)
|
||||
and not key.is_dotted()
|
||||
):
|
||||
item.trivia.indent = "\n"
|
||||
|
||||
if isinstance(item, AoT) and self._body and not self._parsed:
|
||||
item.invalidate_display_name()
|
||||
if item and not ("\n" in item[0].trivia.indent or prev_ws):
|
||||
item[0].trivia.indent = "\n" + item[0].trivia.indent
|
||||
|
||||
if key is not None and key in self:
|
||||
current_idx = self._map[key]
|
||||
if isinstance(current_idx, tuple):
|
||||
current_body_element = self._body[current_idx[-1]]
|
||||
else:
|
||||
current_body_element = self._body[current_idx]
|
||||
|
||||
current = current_body_element[1]
|
||||
|
||||
if isinstance(item, Table):
|
||||
if not isinstance(current, (Table, AoT)):
|
||||
raise KeyAlreadyPresent(key)
|
||||
|
||||
if item.is_aot_element():
|
||||
# New AoT element found later on
|
||||
# Adding it to the current AoT
|
||||
if not isinstance(current, AoT):
|
||||
current = AoT([current, item], parsed=self._parsed)
|
||||
|
||||
self._replace(key, key, current)
|
||||
else:
|
||||
current.append(item)
|
||||
|
||||
return self
|
||||
elif current.is_aot():
|
||||
if not item.is_aot_element():
|
||||
# Tried to define a table after an AoT with the same name.
|
||||
raise KeyAlreadyPresent(key)
|
||||
|
||||
current.append(item)
|
||||
|
||||
return self
|
||||
elif current.is_super_table():
|
||||
if item.is_super_table():
|
||||
# We need to merge both super tables
|
||||
if (
|
||||
key.is_dotted()
|
||||
or current_body_element[0].is_dotted()
|
||||
or self._table_keys[-1] != current_body_element[0]
|
||||
):
|
||||
if key.is_dotted() and not self._parsed:
|
||||
idx = self._get_last_index_before_table()
|
||||
else:
|
||||
idx = len(self._body)
|
||||
|
||||
if idx < len(self._body):
|
||||
self._insert_at(idx, key, item)
|
||||
else:
|
||||
self._raw_append(key, item)
|
||||
|
||||
if validate:
|
||||
self._validate_out_of_order_table(key)
|
||||
|
||||
return self
|
||||
|
||||
# Create a new element to replace the old one
|
||||
current = copy.deepcopy(current)
|
||||
for k, v in item.value.body:
|
||||
current.append(k, v)
|
||||
self._body[
|
||||
(
|
||||
current_idx[-1]
|
||||
if isinstance(current_idx, tuple)
|
||||
else current_idx
|
||||
)
|
||||
] = (current_body_element[0], current)
|
||||
|
||||
return self
|
||||
elif current_body_element[0].is_dotted():
|
||||
raise TOMLKitError("Redefinition of an existing table")
|
||||
elif not item.is_super_table():
|
||||
raise KeyAlreadyPresent(key)
|
||||
elif isinstance(item, AoT):
|
||||
if not isinstance(current, AoT):
|
||||
# Tried to define an AoT after a table with the same name.
|
||||
raise KeyAlreadyPresent(key)
|
||||
|
||||
for table in item.body:
|
||||
current.append(table)
|
||||
|
||||
return self
|
||||
else:
|
||||
raise KeyAlreadyPresent(key)
|
||||
|
||||
is_table = isinstance(item, (Table, AoT))
|
||||
if (
|
||||
key is not None
|
||||
and self._body
|
||||
and not self._parsed
|
||||
and (not is_table or key.is_dotted())
|
||||
):
|
||||
# If there is already at least one table in the current container
|
||||
# and the given item is not a table, we need to find the last
|
||||
# item that is not a table and insert after it
|
||||
# If no such item exists, insert at the top of the table
|
||||
last_index = self._get_last_index_before_table()
|
||||
|
||||
if last_index < len(self._body):
|
||||
after_item = self._body[last_index][1]
|
||||
if not (
|
||||
isinstance(after_item, Whitespace)
|
||||
or "\n" in after_item.trivia.indent
|
||||
):
|
||||
after_item.trivia.indent = "\n" + after_item.trivia.indent
|
||||
return self._insert_at(last_index, key, item)
|
||||
else:
|
||||
previous_item = self._body[-1][1]
|
||||
if not (
|
||||
isinstance(previous_item, Whitespace)
|
||||
or ends_with_whitespace(previous_item)
|
||||
or "\n" in previous_item.trivia.trail
|
||||
):
|
||||
previous_item.trivia.trail += "\n"
|
||||
|
||||
self._raw_append(key, item)
|
||||
return self
|
||||
|
||||
def _raw_append(self, key: Key | None, item: Item) -> None:
|
||||
if key in self._map:
|
||||
current_idx = self._map[key]
|
||||
if not isinstance(current_idx, tuple):
|
||||
current_idx = (current_idx,)
|
||||
|
||||
current = self._body[current_idx[-1]][1]
|
||||
if key is not None and not isinstance(current, Table):
|
||||
raise KeyAlreadyPresent(key)
|
||||
|
||||
self._map[key] = (*current_idx, len(self._body))
|
||||
elif key is not None:
|
||||
self._map[key] = len(self._body)
|
||||
|
||||
self._body.append((key, item))
|
||||
if item.is_table():
|
||||
self._table_keys.append(key)
|
||||
|
||||
if key is not None:
|
||||
dict.__setitem__(self, key.key, item.value)
|
||||
|
||||
def _remove_at(self, idx: int) -> None:
|
||||
key = self._body[idx][0]
|
||||
index = self._map.get(key)
|
||||
if index is None:
|
||||
raise NonExistentKey(key)
|
||||
self._body[idx] = (None, Null())
|
||||
|
||||
if isinstance(index, tuple):
|
||||
index = list(index)
|
||||
index.remove(idx)
|
||||
if len(index) == 1:
|
||||
index = index.pop()
|
||||
else:
|
||||
index = tuple(index)
|
||||
self._map[key] = index
|
||||
else:
|
||||
dict.__delitem__(self, key.key)
|
||||
self._map.pop(key)
|
||||
|
||||
def remove(self, key: Key | str) -> Container:
|
||||
"""Remove a key from the container."""
|
||||
if not isinstance(key, Key):
|
||||
key = SingleKey(key)
|
||||
|
||||
idx = self._map.pop(key, None)
|
||||
if idx is None:
|
||||
raise NonExistentKey(key)
|
||||
|
||||
if isinstance(idx, tuple):
|
||||
for i in idx:
|
||||
self._body[i] = (None, Null())
|
||||
else:
|
||||
self._body[idx] = (None, Null())
|
||||
|
||||
dict.__delitem__(self, key.key)
|
||||
|
||||
return self
|
||||
|
||||
def _insert_after(
|
||||
self, key: Key | str, other_key: Key | str, item: Any
|
||||
) -> Container:
|
||||
if key is None:
|
||||
raise ValueError("Key cannot be null in insert_after()")
|
||||
|
||||
if key not in self:
|
||||
raise NonExistentKey(key)
|
||||
|
||||
if not isinstance(key, Key):
|
||||
key = SingleKey(key)
|
||||
|
||||
if not isinstance(other_key, Key):
|
||||
other_key = SingleKey(other_key)
|
||||
|
||||
item = _item(item)
|
||||
|
||||
idx = self._map[key]
|
||||
# Insert after the max index if there are many.
|
||||
if isinstance(idx, tuple):
|
||||
idx = max(idx)
|
||||
current_item = self._body[idx][1]
|
||||
if "\n" not in current_item.trivia.trail:
|
||||
current_item.trivia.trail += "\n"
|
||||
|
||||
# Increment indices after the current index
|
||||
for k, v in self._map.items():
|
||||
if isinstance(v, tuple):
|
||||
new_indices = []
|
||||
for v_ in v:
|
||||
if v_ > idx:
|
||||
v_ = v_ + 1
|
||||
|
||||
new_indices.append(v_)
|
||||
|
||||
self._map[k] = tuple(new_indices)
|
||||
elif v > idx:
|
||||
self._map[k] = v + 1
|
||||
|
||||
self._map[other_key] = idx + 1
|
||||
self._body.insert(idx + 1, (other_key, item))
|
||||
|
||||
if key is not None:
|
||||
dict.__setitem__(self, other_key.key, item.value)
|
||||
|
||||
return self
|
||||
|
||||
def _insert_at(self, idx: int, key: Key | str, item: Any) -> Container:
|
||||
if idx > len(self._body) - 1:
|
||||
raise ValueError(f"Unable to insert at position {idx}")
|
||||
|
||||
if not isinstance(key, Key):
|
||||
key = SingleKey(key)
|
||||
|
||||
item = _item(item)
|
||||
|
||||
if idx > 0:
|
||||
previous_item = self._body[idx - 1][1]
|
||||
if not (
|
||||
isinstance(previous_item, Whitespace)
|
||||
or ends_with_whitespace(previous_item)
|
||||
or isinstance(item, (AoT, Table))
|
||||
or "\n" in previous_item.trivia.trail
|
||||
):
|
||||
previous_item.trivia.trail += "\n"
|
||||
|
||||
# Increment indices after the current index
|
||||
for k, v in self._map.items():
|
||||
if isinstance(v, tuple):
|
||||
new_indices = []
|
||||
for v_ in v:
|
||||
if v_ >= idx:
|
||||
v_ = v_ + 1
|
||||
|
||||
new_indices.append(v_)
|
||||
|
||||
self._map[k] = tuple(new_indices)
|
||||
elif v >= idx:
|
||||
self._map[k] = v + 1
|
||||
|
||||
if key in self._map:
|
||||
current_idx = self._map[key]
|
||||
if not isinstance(current_idx, tuple):
|
||||
current_idx = (current_idx,)
|
||||
self._map[key] = (*current_idx, idx)
|
||||
else:
|
||||
self._map[key] = idx
|
||||
self._body.insert(idx, (key, item))
|
||||
|
||||
dict.__setitem__(self, key.key, item.value)
|
||||
|
||||
return self
|
||||
|
||||
def item(self, key: Key | str) -> Item:
|
||||
"""Get an item for the given key."""
|
||||
if not isinstance(key, Key):
|
||||
key = SingleKey(key)
|
||||
|
||||
idx = self._map.get(key)
|
||||
if idx is None:
|
||||
raise NonExistentKey(key)
|
||||
|
||||
if isinstance(idx, tuple):
|
||||
# The item we are getting is an out of order table
|
||||
# so we need a proxy to retrieve the proper objects
|
||||
# from the parent container
|
||||
return OutOfOrderTableProxy(self, idx)
|
||||
|
||||
return self._body[idx][1]
|
||||
|
||||
def last_item(self) -> Item | None:
|
||||
"""Get the last item."""
|
||||
if self._body:
|
||||
return self._body[-1][1]
|
||||
|
||||
def as_string(self) -> str:
|
||||
"""Render as TOML string."""
|
||||
s = ""
|
||||
for k, v in self._body:
|
||||
if k is not None:
|
||||
if isinstance(v, Table):
|
||||
if (
|
||||
s.strip(" ")
|
||||
and not s.strip(" ").endswith("\n")
|
||||
and "\n" not in v.trivia.indent
|
||||
):
|
||||
s += "\n"
|
||||
s += self._render_table(k, v)
|
||||
elif isinstance(v, AoT):
|
||||
if (
|
||||
s.strip(" ")
|
||||
and not s.strip(" ").endswith("\n")
|
||||
and "\n" not in v.trivia.indent
|
||||
):
|
||||
s += "\n"
|
||||
s += self._render_aot(k, v)
|
||||
else:
|
||||
s += self._render_simple_item(k, v)
|
||||
else:
|
||||
s += self._render_simple_item(k, v)
|
||||
|
||||
return s
|
||||
|
||||
def _render_table(self, key: Key, table: Table, prefix: str | None = None) -> str:
|
||||
cur = ""
|
||||
|
||||
if table.display_name is not None:
|
||||
_key = table.display_name
|
||||
else:
|
||||
_key = key.as_string()
|
||||
|
||||
if prefix is not None:
|
||||
_key = prefix + "." + _key
|
||||
|
||||
if not table.is_super_table() or (
|
||||
any(
|
||||
not isinstance(v, (Table, AoT, Whitespace, Null))
|
||||
for _, v in table.value.body
|
||||
)
|
||||
and not key.is_dotted()
|
||||
):
|
||||
open_, close = "[", "]"
|
||||
if table.is_aot_element():
|
||||
open_, close = "[[", "]]"
|
||||
|
||||
newline_in_table_trivia = (
|
||||
"\n" if "\n" not in table.trivia.trail and len(table.value) > 0 else ""
|
||||
)
|
||||
cur += (
|
||||
f"{table.trivia.indent}"
|
||||
f"{open_}"
|
||||
f"{decode(_key)}"
|
||||
f"{close}"
|
||||
f"{table.trivia.comment_ws}"
|
||||
f"{decode(table.trivia.comment)}"
|
||||
f"{table.trivia.trail}"
|
||||
f"{newline_in_table_trivia}"
|
||||
)
|
||||
elif table.trivia.indent == "\n":
|
||||
cur += table.trivia.indent
|
||||
|
||||
for k, v in table.value.body:
|
||||
if isinstance(v, Table):
|
||||
if (
|
||||
cur.strip(" ")
|
||||
and not cur.strip(" ").endswith("\n")
|
||||
and "\n" not in v.trivia.indent
|
||||
):
|
||||
cur += "\n"
|
||||
if v.is_super_table():
|
||||
if k.is_dotted() and not key.is_dotted():
|
||||
# Dotted key inside table
|
||||
cur += self._render_table(k, v)
|
||||
else:
|
||||
cur += self._render_table(k, v, prefix=_key)
|
||||
else:
|
||||
cur += self._render_table(k, v, prefix=_key)
|
||||
elif isinstance(v, AoT):
|
||||
if (
|
||||
cur.strip(" ")
|
||||
and not cur.strip(" ").endswith("\n")
|
||||
and "\n" not in v.trivia.indent
|
||||
):
|
||||
cur += "\n"
|
||||
cur += self._render_aot(k, v, prefix=_key)
|
||||
else:
|
||||
cur += self._render_simple_item(
|
||||
k, v, prefix=_key if key.is_dotted() else None
|
||||
)
|
||||
|
||||
return cur
|
||||
|
||||
def _render_aot(self, key, aot, prefix=None):
|
||||
_key = key.as_string()
|
||||
if prefix is not None:
|
||||
_key = prefix + "." + _key
|
||||
|
||||
cur = ""
|
||||
_key = decode(_key)
|
||||
for table in aot.body:
|
||||
cur += self._render_aot_table(table, prefix=_key)
|
||||
|
||||
return cur
|
||||
|
||||
def _render_aot_table(self, table: Table, prefix: str | None = None) -> str:
|
||||
cur = ""
|
||||
_key = prefix or ""
|
||||
open_, close = "[[", "]]"
|
||||
|
||||
cur += (
|
||||
f"{table.trivia.indent}"
|
||||
f"{open_}"
|
||||
f"{decode(_key)}"
|
||||
f"{close}"
|
||||
f"{table.trivia.comment_ws}"
|
||||
f"{decode(table.trivia.comment)}"
|
||||
f"{table.trivia.trail}"
|
||||
)
|
||||
|
||||
for k, v in table.value.body:
|
||||
if isinstance(v, Table):
|
||||
if v.is_super_table():
|
||||
if k.is_dotted():
|
||||
# Dotted key inside table
|
||||
cur += self._render_table(k, v)
|
||||
else:
|
||||
cur += self._render_table(k, v, prefix=_key)
|
||||
else:
|
||||
cur += self._render_table(k, v, prefix=_key)
|
||||
elif isinstance(v, AoT):
|
||||
cur += self._render_aot(k, v, prefix=_key)
|
||||
else:
|
||||
cur += self._render_simple_item(k, v)
|
||||
|
||||
return cur
|
||||
|
||||
def _render_simple_item(self, key, item, prefix=None):
|
||||
if key is None:
|
||||
return item.as_string()
|
||||
|
||||
_key = key.as_string()
|
||||
if prefix is not None:
|
||||
_key = prefix + "." + _key
|
||||
|
||||
return (
|
||||
f"{item.trivia.indent}"
|
||||
f"{decode(_key)}"
|
||||
f"{key.sep}"
|
||||
f"{decode(item.as_string())}"
|
||||
f"{item.trivia.comment_ws}"
|
||||
f"{decode(item.trivia.comment)}"
|
||||
f"{item.trivia.trail}"
|
||||
)
|
||||
|
||||
def __len__(self) -> int:
|
||||
return dict.__len__(self)
|
||||
|
||||
def __iter__(self) -> Iterator[str]:
|
||||
return iter(dict.keys(self))
|
||||
|
||||
# Dictionary methods
|
||||
def __getitem__(self, key: Key | str) -> Item | Container:
|
||||
item = self.item(key)
|
||||
if isinstance(item, Item) and item.is_boolean():
|
||||
return item.value
|
||||
|
||||
return item
|
||||
|
||||
def __setitem__(self, key: Key | str, value: Any) -> None:
|
||||
if key is not None and key in self:
|
||||
old_key = next(filter(lambda k: k == key, self._map))
|
||||
self._replace(old_key, key, value)
|
||||
else:
|
||||
self.append(key, value)
|
||||
|
||||
def __delitem__(self, key: Key | str) -> None:
|
||||
self.remove(key)
|
||||
|
||||
def setdefault(self, key: Key | str, default: Any) -> Any:
|
||||
super().setdefault(key, default=default)
|
||||
return self[key]
|
||||
|
||||
def _replace(self, key: Key | str, new_key: Key | str, value: Item) -> None:
|
||||
if not isinstance(key, Key):
|
||||
key = SingleKey(key)
|
||||
|
||||
idx = self._map.get(key)
|
||||
if idx is None:
|
||||
raise NonExistentKey(key)
|
||||
|
||||
self._replace_at(idx, new_key, value)
|
||||
|
||||
def _replace_at(
|
||||
self, idx: int | tuple[int], new_key: Key | str, value: Item
|
||||
) -> None:
|
||||
value = _item(value)
|
||||
|
||||
if isinstance(idx, tuple):
|
||||
for i in idx[1:]:
|
||||
self._body[i] = (None, Null())
|
||||
|
||||
idx = idx[0]
|
||||
|
||||
k, v = self._body[idx]
|
||||
if not isinstance(new_key, Key):
|
||||
if (
|
||||
isinstance(value, (AoT, Table)) != isinstance(v, (AoT, Table))
|
||||
or new_key != k.key
|
||||
):
|
||||
new_key = SingleKey(new_key)
|
||||
else: # Inherit the sep of the old key
|
||||
new_key = k
|
||||
|
||||
del self._map[k]
|
||||
self._map[new_key] = idx
|
||||
if new_key != k:
|
||||
dict.__delitem__(self, k)
|
||||
|
||||
if isinstance(value, (AoT, Table)) != isinstance(v, (AoT, Table)):
|
||||
# new tables should appear after all non-table values
|
||||
self.remove(k)
|
||||
for i in range(idx, len(self._body)):
|
||||
if isinstance(self._body[i][1], (AoT, Table)):
|
||||
self._insert_at(i, new_key, value)
|
||||
idx = i
|
||||
break
|
||||
else:
|
||||
idx = -1
|
||||
self.append(new_key, value)
|
||||
else:
|
||||
# Copying trivia
|
||||
if not isinstance(value, (Whitespace, AoT)):
|
||||
value.trivia.indent = v.trivia.indent
|
||||
value.trivia.comment_ws = value.trivia.comment_ws or v.trivia.comment_ws
|
||||
value.trivia.comment = value.trivia.comment or v.trivia.comment
|
||||
value.trivia.trail = v.trivia.trail
|
||||
self._body[idx] = (new_key, value)
|
||||
|
||||
if hasattr(value, "invalidate_display_name"):
|
||||
value.invalidate_display_name() # type: ignore[attr-defined]
|
||||
|
||||
if isinstance(value, Table):
|
||||
# Insert a cosmetic new line for tables if:
|
||||
# - it does not have it yet OR is not followed by one
|
||||
# - it is not the last item, or
|
||||
# - The table being replaced has a newline
|
||||
last, _ = self._previous_item_with_index()
|
||||
idx = last if idx < 0 else idx
|
||||
has_ws = ends_with_whitespace(value)
|
||||
replace_has_ws = (
|
||||
isinstance(v, Table)
|
||||
and v.value.body
|
||||
and isinstance(v.value.body[-1][1], Whitespace)
|
||||
)
|
||||
next_ws = idx < last and isinstance(self._body[idx + 1][1], Whitespace)
|
||||
if (idx < last or replace_has_ws) and not (next_ws or has_ws):
|
||||
value.append(None, Whitespace("\n"))
|
||||
|
||||
dict.__setitem__(self, new_key.key, value.value)
|
||||
|
||||
def __str__(self) -> str:
|
||||
return str(self.value)
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return repr(self.value)
|
||||
|
||||
def __eq__(self, other: dict) -> bool:
|
||||
if not isinstance(other, dict):
|
||||
return NotImplemented
|
||||
|
||||
return self.value == other
|
||||
|
||||
def _getstate(self, protocol):
|
||||
return (self._parsed,)
|
||||
|
||||
def __reduce__(self):
|
||||
return self.__reduce_ex__(2)
|
||||
|
||||
def __reduce_ex__(self, protocol):
|
||||
return (
|
||||
self.__class__,
|
||||
self._getstate(protocol),
|
||||
(self._map, self._body, self._parsed, self._table_keys),
|
||||
)
|
||||
|
||||
def __setstate__(self, state):
|
||||
self._map = state[0]
|
||||
self._body = state[1]
|
||||
self._parsed = state[2]
|
||||
self._table_keys = state[3]
|
||||
|
||||
for key, item in self._body:
|
||||
if key is not None:
|
||||
dict.__setitem__(self, key.key, item.value)
|
||||
|
||||
def copy(self) -> Container:
|
||||
return copy.copy(self)
|
||||
|
||||
def __copy__(self) -> Container:
|
||||
c = self.__class__(self._parsed)
|
||||
for k, v in dict.items(self):
|
||||
dict.__setitem__(c, k, v)
|
||||
|
||||
c._body += self.body
|
||||
c._map.update(self._map)
|
||||
|
||||
return c
|
||||
|
||||
def _previous_item_with_index(
|
||||
self, idx: int | None = None, ignore=(Null,)
|
||||
) -> tuple[int, Item] | None:
|
||||
"""Find the immediate previous item before index ``idx``"""
|
||||
if idx is None or idx > len(self._body):
|
||||
idx = len(self._body)
|
||||
for i in range(idx - 1, -1, -1):
|
||||
v = self._body[i][-1]
|
||||
if not isinstance(v, ignore):
|
||||
return i, v
|
||||
return None
|
||||
|
||||
def _previous_item(self, idx: int | None = None, ignore=(Null,)) -> Item | None:
|
||||
"""Find the immediate previous item before index ``idx``.
|
||||
If ``idx`` is not given, the last item is returned.
|
||||
"""
|
||||
prev = self._previous_item_with_index(idx, ignore)
|
||||
return prev[-1] if prev else None
|
||||
|
||||
|
||||
class OutOfOrderTableProxy(_CustomDict):
|
||||
@staticmethod
|
||||
def validate(container: Container, indices: tuple[int, ...]) -> None:
|
||||
"""Validate out of order tables in the given container"""
|
||||
# Append all items to a temp container to see if there is any error
|
||||
temp_container = Container(True)
|
||||
for i in indices:
|
||||
_, item = container._body[i]
|
||||
|
||||
if isinstance(item, Table):
|
||||
for k, v in item.value.body:
|
||||
temp_container.append(k, v, validate=False)
|
||||
|
||||
temp_container._validate_out_of_order_table()
|
||||
|
||||
def __init__(self, container: Container, indices: tuple[int, ...]) -> None:
|
||||
self._container = container
|
||||
self._internal_container = Container(True)
|
||||
self._tables: list[Table] = []
|
||||
self._tables_map: dict[Key, list[int]] = {}
|
||||
|
||||
for i in indices:
|
||||
_, item = self._container._body[i]
|
||||
|
||||
if isinstance(item, Table):
|
||||
self._tables.append(item)
|
||||
table_idx = len(self._tables) - 1
|
||||
for k, v in item.value.body:
|
||||
self._internal_container._raw_append(k, v)
|
||||
indices = self._tables_map.setdefault(k, [])
|
||||
if table_idx not in indices:
|
||||
indices.append(table_idx)
|
||||
if k is not None:
|
||||
dict.__setitem__(self, k.key, v)
|
||||
|
||||
self._internal_container._validate_out_of_order_table()
|
||||
|
||||
def unwrap(self) -> str:
|
||||
return self._internal_container.unwrap()
|
||||
|
||||
@property
|
||||
def value(self):
|
||||
return self._internal_container.value
|
||||
|
||||
def __getitem__(self, key: Key | str) -> Any:
|
||||
if key not in self._internal_container:
|
||||
raise NonExistentKey(key)
|
||||
|
||||
return self._internal_container[key]
|
||||
|
||||
def __setitem__(self, key: Key | str, value: Any) -> None:
|
||||
from .items import item
|
||||
|
||||
def _is_table_or_aot(it: Any) -> bool:
|
||||
return isinstance(item(it), (Table, AoT))
|
||||
|
||||
if key in self._tables_map:
|
||||
# Overwrite the first table and remove others
|
||||
indices = self._tables_map[key]
|
||||
while len(indices) > 1:
|
||||
table = self._tables[indices.pop()]
|
||||
self._remove_table(table)
|
||||
old_value = self._tables[indices[0]][key]
|
||||
if _is_table_or_aot(old_value) and not _is_table_or_aot(value):
|
||||
# Remove the entry from the map and set value again.
|
||||
del self._tables[indices[0]][key]
|
||||
del self._tables_map[key]
|
||||
self[key] = value
|
||||
return
|
||||
self._tables[indices[0]][key] = value
|
||||
elif self._tables:
|
||||
if not _is_table_or_aot(value): # if the value is a plain value
|
||||
for table in self._tables:
|
||||
# find the first table that allows plain values
|
||||
if any(not _is_table_or_aot(v) for _, v in table.items()):
|
||||
table[key] = value
|
||||
break
|
||||
else:
|
||||
self._tables[0][key] = value
|
||||
else:
|
||||
self._tables[0][key] = value
|
||||
else:
|
||||
self._container[key] = value
|
||||
|
||||
self._internal_container[key] = value
|
||||
if key is not None:
|
||||
dict.__setitem__(self, key, value)
|
||||
|
||||
def _remove_table(self, table: Table) -> None:
|
||||
"""Remove table from the parent container"""
|
||||
self._tables.remove(table)
|
||||
for idx, item in enumerate(self._container._body):
|
||||
if item[1] is table:
|
||||
self._container._remove_at(idx)
|
||||
break
|
||||
|
||||
def __delitem__(self, key: Key | str) -> None:
|
||||
if key not in self._tables_map:
|
||||
raise NonExistentKey(key)
|
||||
|
||||
for i in reversed(self._tables_map[key]):
|
||||
table = self._tables[i]
|
||||
del table[key]
|
||||
if not table and len(self._tables) > 1:
|
||||
self._remove_table(table)
|
||||
|
||||
del self._tables_map[key]
|
||||
del self._internal_container[key]
|
||||
if key is not None:
|
||||
dict.__delitem__(self, key)
|
||||
|
||||
def __iter__(self) -> Iterator[str]:
|
||||
return iter(dict.keys(self))
|
||||
|
||||
def __len__(self) -> int:
|
||||
return dict.__len__(self)
|
||||
|
||||
def setdefault(self, key: Key | str, default: Any) -> Any:
|
||||
super().setdefault(key, default=default)
|
||||
return self[key]
|
||||
|
||||
|
||||
def ends_with_whitespace(it: Any) -> bool:
|
||||
"""Returns ``True`` if the given item ``it`` is a ``Table`` or ``AoT`` object
|
||||
ending with a ``Whitespace``.
|
||||
"""
|
||||
return (
|
||||
isinstance(it, Table) and isinstance(it.value._previous_item(), Whitespace)
|
||||
) or (isinstance(it, AoT) and len(it) > 0 and isinstance(it[-1], Whitespace))
|
||||
234
Backend/venv/lib/python3.12/site-packages/tomlkit/exceptions.py
Normal file
234
Backend/venv/lib/python3.12/site-packages/tomlkit/exceptions.py
Normal file
@@ -0,0 +1,234 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Collection
|
||||
|
||||
|
||||
class TOMLKitError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class ParseError(ValueError, TOMLKitError):
|
||||
"""
|
||||
This error occurs when the parser encounters a syntax error
|
||||
in the TOML being parsed. The error references the line and
|
||||
location within the line where the error was encountered.
|
||||
"""
|
||||
|
||||
def __init__(self, line: int, col: int, message: str | None = None) -> None:
|
||||
self._line = line
|
||||
self._col = col
|
||||
|
||||
if message is None:
|
||||
message = "TOML parse error"
|
||||
|
||||
super().__init__(f"{message} at line {self._line} col {self._col}")
|
||||
|
||||
@property
|
||||
def line(self):
|
||||
return self._line
|
||||
|
||||
@property
|
||||
def col(self):
|
||||
return self._col
|
||||
|
||||
|
||||
class MixedArrayTypesError(ParseError):
|
||||
"""
|
||||
An array was found that had two or more element types.
|
||||
"""
|
||||
|
||||
def __init__(self, line: int, col: int) -> None:
|
||||
message = "Mixed types found in array"
|
||||
|
||||
super().__init__(line, col, message=message)
|
||||
|
||||
|
||||
class InvalidNumberError(ParseError):
|
||||
"""
|
||||
A numeric field was improperly specified.
|
||||
"""
|
||||
|
||||
def __init__(self, line: int, col: int) -> None:
|
||||
message = "Invalid number"
|
||||
|
||||
super().__init__(line, col, message=message)
|
||||
|
||||
|
||||
class InvalidDateTimeError(ParseError):
|
||||
"""
|
||||
A datetime field was improperly specified.
|
||||
"""
|
||||
|
||||
def __init__(self, line: int, col: int) -> None:
|
||||
message = "Invalid datetime"
|
||||
|
||||
super().__init__(line, col, message=message)
|
||||
|
||||
|
||||
class InvalidDateError(ParseError):
|
||||
"""
|
||||
A date field was improperly specified.
|
||||
"""
|
||||
|
||||
def __init__(self, line: int, col: int) -> None:
|
||||
message = "Invalid date"
|
||||
|
||||
super().__init__(line, col, message=message)
|
||||
|
||||
|
||||
class InvalidTimeError(ParseError):
|
||||
"""
|
||||
A date field was improperly specified.
|
||||
"""
|
||||
|
||||
def __init__(self, line: int, col: int) -> None:
|
||||
message = "Invalid time"
|
||||
|
||||
super().__init__(line, col, message=message)
|
||||
|
||||
|
||||
class InvalidNumberOrDateError(ParseError):
|
||||
"""
|
||||
A numeric or date field was improperly specified.
|
||||
"""
|
||||
|
||||
def __init__(self, line: int, col: int) -> None:
|
||||
message = "Invalid number or date format"
|
||||
|
||||
super().__init__(line, col, message=message)
|
||||
|
||||
|
||||
class InvalidUnicodeValueError(ParseError):
|
||||
"""
|
||||
A unicode code was improperly specified.
|
||||
"""
|
||||
|
||||
def __init__(self, line: int, col: int) -> None:
|
||||
message = "Invalid unicode value"
|
||||
|
||||
super().__init__(line, col, message=message)
|
||||
|
||||
|
||||
class UnexpectedCharError(ParseError):
|
||||
"""
|
||||
An unexpected character was found during parsing.
|
||||
"""
|
||||
|
||||
def __init__(self, line: int, col: int, char: str) -> None:
|
||||
message = f"Unexpected character: {char!r}"
|
||||
|
||||
super().__init__(line, col, message=message)
|
||||
|
||||
|
||||
class EmptyKeyError(ParseError):
|
||||
"""
|
||||
An empty key was found during parsing.
|
||||
"""
|
||||
|
||||
def __init__(self, line: int, col: int) -> None:
|
||||
message = "Empty key"
|
||||
|
||||
super().__init__(line, col, message=message)
|
||||
|
||||
|
||||
class EmptyTableNameError(ParseError):
|
||||
"""
|
||||
An empty table name was found during parsing.
|
||||
"""
|
||||
|
||||
def __init__(self, line: int, col: int) -> None:
|
||||
message = "Empty table name"
|
||||
|
||||
super().__init__(line, col, message=message)
|
||||
|
||||
|
||||
class InvalidCharInStringError(ParseError):
|
||||
"""
|
||||
The string being parsed contains an invalid character.
|
||||
"""
|
||||
|
||||
def __init__(self, line: int, col: int, char: str) -> None:
|
||||
message = f"Invalid character {char!r} in string"
|
||||
|
||||
super().__init__(line, col, message=message)
|
||||
|
||||
|
||||
class UnexpectedEofError(ParseError):
|
||||
"""
|
||||
The TOML being parsed ended before the end of a statement.
|
||||
"""
|
||||
|
||||
def __init__(self, line: int, col: int) -> None:
|
||||
message = "Unexpected end of file"
|
||||
|
||||
super().__init__(line, col, message=message)
|
||||
|
||||
|
||||
class InternalParserError(ParseError):
|
||||
"""
|
||||
An error that indicates a bug in the parser.
|
||||
"""
|
||||
|
||||
def __init__(self, line: int, col: int, message: str | None = None) -> None:
|
||||
msg = "Internal parser error"
|
||||
if message:
|
||||
msg += f" ({message})"
|
||||
|
||||
super().__init__(line, col, message=msg)
|
||||
|
||||
|
||||
class NonExistentKey(KeyError, TOMLKitError):
|
||||
"""
|
||||
A non-existent key was used.
|
||||
"""
|
||||
|
||||
def __init__(self, key):
|
||||
message = f'Key "{key}" does not exist.'
|
||||
|
||||
super().__init__(message)
|
||||
|
||||
|
||||
class KeyAlreadyPresent(TOMLKitError):
|
||||
"""
|
||||
An already present key was used.
|
||||
"""
|
||||
|
||||
def __init__(self, key):
|
||||
key = getattr(key, "key", key)
|
||||
message = f'Key "{key}" already exists.'
|
||||
|
||||
super().__init__(message)
|
||||
|
||||
|
||||
class InvalidControlChar(ParseError):
|
||||
def __init__(self, line: int, col: int, char: int, type: str) -> None:
|
||||
display_code = "\\u00"
|
||||
|
||||
if char < 16:
|
||||
display_code += "0"
|
||||
|
||||
display_code += hex(char)[2:]
|
||||
|
||||
message = (
|
||||
"Control characters (codes less than 0x1f and 0x7f)"
|
||||
f" are not allowed in {type}, "
|
||||
f"use {display_code} instead"
|
||||
)
|
||||
|
||||
super().__init__(line, col, message=message)
|
||||
|
||||
|
||||
class InvalidStringError(ValueError, TOMLKitError):
|
||||
def __init__(self, value: str, invalid_sequences: Collection[str], delimiter: str):
|
||||
repr_ = repr(value)[1:-1]
|
||||
super().__init__(
|
||||
f"Invalid string: {delimiter}{repr_}{delimiter}. "
|
||||
f"The character sequences {invalid_sequences} are invalid."
|
||||
)
|
||||
|
||||
|
||||
class ConvertError(TypeError, ValueError, TOMLKitError):
|
||||
"""Raised when item() fails to convert a value.
|
||||
It should be a TypeError, but due to historical reasons
|
||||
it needs to subclass ValueError as well.
|
||||
"""
|
||||
2013
Backend/venv/lib/python3.12/site-packages/tomlkit/items.py
Normal file
2013
Backend/venv/lib/python3.12/site-packages/tomlkit/items.py
Normal file
File diff suppressed because it is too large
Load Diff
1140
Backend/venv/lib/python3.12/site-packages/tomlkit/parser.py
Normal file
1140
Backend/venv/lib/python3.12/site-packages/tomlkit/parser.py
Normal file
File diff suppressed because it is too large
Load Diff
180
Backend/venv/lib/python3.12/site-packages/tomlkit/source.py
Normal file
180
Backend/venv/lib/python3.12/site-packages/tomlkit/source.py
Normal file
@@ -0,0 +1,180 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from copy import copy
|
||||
from typing import Any
|
||||
|
||||
from tomlkit.exceptions import ParseError
|
||||
from tomlkit.exceptions import UnexpectedCharError
|
||||
from tomlkit.toml_char import TOMLChar
|
||||
|
||||
|
||||
class _State:
|
||||
def __init__(
|
||||
self,
|
||||
source: Source,
|
||||
save_marker: bool | None = False,
|
||||
restore: bool | None = False,
|
||||
) -> None:
|
||||
self._source = source
|
||||
self._save_marker = save_marker
|
||||
self.restore = restore
|
||||
|
||||
def __enter__(self) -> _State:
|
||||
# Entering this context manager - save the state
|
||||
self._chars = copy(self._source._chars)
|
||||
self._idx = self._source._idx
|
||||
self._current = self._source._current
|
||||
self._marker = self._source._marker
|
||||
|
||||
return self
|
||||
|
||||
def __exit__(self, exception_type, exception_val, trace):
|
||||
# Exiting this context manager - restore the prior state
|
||||
if self.restore or exception_type:
|
||||
self._source._chars = self._chars
|
||||
self._source._idx = self._idx
|
||||
self._source._current = self._current
|
||||
if self._save_marker:
|
||||
self._source._marker = self._marker
|
||||
|
||||
|
||||
class _StateHandler:
|
||||
"""
|
||||
State preserver for the Parser.
|
||||
"""
|
||||
|
||||
def __init__(self, source: Source) -> None:
|
||||
self._source = source
|
||||
self._states = []
|
||||
|
||||
def __call__(self, *args, **kwargs):
|
||||
return _State(self._source, *args, **kwargs)
|
||||
|
||||
def __enter__(self) -> _State:
|
||||
state = self()
|
||||
self._states.append(state)
|
||||
return state.__enter__()
|
||||
|
||||
def __exit__(self, exception_type, exception_val, trace):
|
||||
state = self._states.pop()
|
||||
return state.__exit__(exception_type, exception_val, trace)
|
||||
|
||||
|
||||
class Source(str):
|
||||
EOF = TOMLChar("\0")
|
||||
|
||||
def __init__(self, _: str) -> None:
|
||||
super().__init__()
|
||||
|
||||
# Collection of TOMLChars
|
||||
self._chars = iter([(i, TOMLChar(c)) for i, c in enumerate(self)])
|
||||
|
||||
self._idx = 0
|
||||
self._marker = 0
|
||||
self._current = TOMLChar("")
|
||||
|
||||
self._state = _StateHandler(self)
|
||||
|
||||
self.inc()
|
||||
|
||||
def reset(self):
|
||||
# initialize both idx and current
|
||||
self.inc()
|
||||
|
||||
# reset marker
|
||||
self.mark()
|
||||
|
||||
@property
|
||||
def state(self) -> _StateHandler:
|
||||
return self._state
|
||||
|
||||
@property
|
||||
def idx(self) -> int:
|
||||
return self._idx
|
||||
|
||||
@property
|
||||
def current(self) -> TOMLChar:
|
||||
return self._current
|
||||
|
||||
@property
|
||||
def marker(self) -> int:
|
||||
return self._marker
|
||||
|
||||
def extract(self) -> str:
|
||||
"""
|
||||
Extracts the value between marker and index
|
||||
"""
|
||||
return self[self._marker : self._idx]
|
||||
|
||||
def inc(self, exception: type[ParseError] | None = None) -> bool:
|
||||
"""
|
||||
Increments the parser if the end of the input has not been reached.
|
||||
Returns whether or not it was able to advance.
|
||||
"""
|
||||
try:
|
||||
self._idx, self._current = next(self._chars)
|
||||
|
||||
return True
|
||||
except StopIteration:
|
||||
self._idx = len(self)
|
||||
self._current = self.EOF
|
||||
if exception:
|
||||
raise self.parse_error(exception) from None
|
||||
|
||||
return False
|
||||
|
||||
def inc_n(self, n: int, exception: type[ParseError] | None = None) -> bool:
|
||||
"""
|
||||
Increments the parser by n characters
|
||||
if the end of the input has not been reached.
|
||||
"""
|
||||
return all(self.inc(exception=exception) for _ in range(n))
|
||||
|
||||
def consume(self, chars, min=0, max=-1):
|
||||
"""
|
||||
Consume chars until min/max is satisfied is valid.
|
||||
"""
|
||||
while self.current in chars and max != 0:
|
||||
min -= 1
|
||||
max -= 1
|
||||
if not self.inc():
|
||||
break
|
||||
|
||||
# failed to consume minimum number of characters
|
||||
if min > 0:
|
||||
raise self.parse_error(UnexpectedCharError, self.current)
|
||||
|
||||
def end(self) -> bool:
|
||||
"""
|
||||
Returns True if the parser has reached the end of the input.
|
||||
"""
|
||||
return self._current is self.EOF
|
||||
|
||||
def mark(self) -> None:
|
||||
"""
|
||||
Sets the marker to the index's current position
|
||||
"""
|
||||
self._marker = self._idx
|
||||
|
||||
def parse_error(
|
||||
self,
|
||||
exception: type[ParseError] = ParseError,
|
||||
*args: Any,
|
||||
**kwargs: Any,
|
||||
) -> ParseError:
|
||||
"""
|
||||
Creates a generic "parse error" at the current position.
|
||||
"""
|
||||
line, col = self._to_linecol()
|
||||
|
||||
return exception(line, col, *args, **kwargs)
|
||||
|
||||
def _to_linecol(self) -> tuple[int, int]:
|
||||
cur = 0
|
||||
for i, line in enumerate(self.splitlines()):
|
||||
if cur + len(line) + 1 > self.idx:
|
||||
return (i + 1, self.idx - cur)
|
||||
|
||||
cur += len(line) + 1
|
||||
|
||||
return len(self.splitlines()), 0
|
||||
@@ -0,0 +1,52 @@
|
||||
import string
|
||||
|
||||
|
||||
class TOMLChar(str):
|
||||
def __init__(self, c):
|
||||
super().__init__()
|
||||
|
||||
if len(self) > 1:
|
||||
raise ValueError("A TOML character must be of length 1")
|
||||
|
||||
BARE = string.ascii_letters + string.digits + "-_"
|
||||
KV = "= \t"
|
||||
NUMBER = string.digits + "+-_.e"
|
||||
SPACES = " \t"
|
||||
NL = "\n\r"
|
||||
WS = SPACES + NL
|
||||
|
||||
def is_bare_key_char(self) -> bool:
|
||||
"""
|
||||
Whether the character is a valid bare key name or not.
|
||||
"""
|
||||
return self in self.BARE
|
||||
|
||||
def is_kv_sep(self) -> bool:
|
||||
"""
|
||||
Whether the character is a valid key/value separator or not.
|
||||
"""
|
||||
return self in self.KV
|
||||
|
||||
def is_int_float_char(self) -> bool:
|
||||
"""
|
||||
Whether the character if a valid integer or float value character or not.
|
||||
"""
|
||||
return self in self.NUMBER
|
||||
|
||||
def is_ws(self) -> bool:
|
||||
"""
|
||||
Whether the character is a whitespace character or not.
|
||||
"""
|
||||
return self in self.WS
|
||||
|
||||
def is_nl(self) -> bool:
|
||||
"""
|
||||
Whether the character is a new line character or not.
|
||||
"""
|
||||
return self in self.NL
|
||||
|
||||
def is_spaces(self) -> bool:
|
||||
"""
|
||||
Whether the character is a space or not
|
||||
"""
|
||||
return self in self.SPACES
|
||||
@@ -0,0 +1,7 @@
|
||||
from tomlkit.container import Container
|
||||
|
||||
|
||||
class TOMLDocument(Container):
|
||||
"""
|
||||
A TOML document.
|
||||
"""
|
||||
@@ -0,0 +1,59 @@
|
||||
import os
|
||||
import re
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from tomlkit.api import loads
|
||||
from tomlkit.toml_document import TOMLDocument
|
||||
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from _typeshed import StrPath as _StrPath
|
||||
else:
|
||||
from typing import Union
|
||||
|
||||
_StrPath = Union[str, os.PathLike]
|
||||
|
||||
|
||||
class TOMLFile:
|
||||
"""
|
||||
Represents a TOML file.
|
||||
|
||||
:param path: path to the TOML file
|
||||
"""
|
||||
|
||||
def __init__(self, path: _StrPath) -> None:
|
||||
self._path = path
|
||||
self._linesep = os.linesep
|
||||
|
||||
def read(self) -> TOMLDocument:
|
||||
"""Read the file content as a :class:`tomlkit.toml_document.TOMLDocument`."""
|
||||
with open(self._path, encoding="utf-8", newline="") as f:
|
||||
content = f.read()
|
||||
|
||||
# check if consistent line endings
|
||||
num_newline = content.count("\n")
|
||||
if num_newline > 0:
|
||||
num_win_eol = content.count("\r\n")
|
||||
if num_win_eol == num_newline:
|
||||
self._linesep = "\r\n"
|
||||
content = content.replace("\r\n", "\n")
|
||||
elif num_win_eol == 0:
|
||||
self._linesep = "\n"
|
||||
else:
|
||||
self._linesep = "mixed"
|
||||
|
||||
return loads(content)
|
||||
|
||||
def write(self, data: TOMLDocument) -> None:
|
||||
"""Write the TOMLDocument to the file."""
|
||||
content = data.as_string()
|
||||
|
||||
# apply linesep
|
||||
if self._linesep == "\n":
|
||||
content = content.replace("\r\n", "\n")
|
||||
elif self._linesep == "\r\n":
|
||||
content = re.sub(r"(?<!\r)\n", "\r\n", content)
|
||||
|
||||
with open(self._path, "w", encoding="utf-8", newline="") as f:
|
||||
f.write(content)
|
||||
Reference in New Issue
Block a user