from __future__ import annotations
import datetime as _datetime
from collections.abc import Mapping
from typing import IO
from typing import Iterable
from tomlkit._utils import parse_rfc3339
from tomlkit.container import Container
from tomlkit.exceptions import UnexpectedCharError
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 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, 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 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
"""
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: `