1import datetime as _datetime
2
3from typing import Tuple
4
5from ._utils import parse_rfc3339
6from .container import Container
7from .items import AoT
8from .items import Array
9from .items import Bool
10from .items import Comment
11from .items import Date
12from .items import DateTime
13from .items import Float
14from .items import InlineTable
15from .items import Integer
16from .items import Item as _Item
17from .items import Key
18from .items import String
19from .items import Table
20from .items import Time
21from .items import Trivia
22from .items import Whitespace
23from .items import item
24from .parser import Parser
25from .toml_document import TOMLDocument as _TOMLDocument
26
27
28def loads(string):  # type: (str) -> _TOMLDocument
29    """
30    Parses a string into a TOMLDocument.
31
32    Alias for parse().
33    """
34    return parse(string)
35
36
37def dumps(data, sort_keys=False):  # type: (_TOMLDocument, bool) -> str
38    """
39    Dumps a TOMLDocument into a string.
40    """
41    if not isinstance(data, _TOMLDocument) and isinstance(data, dict):
42        data = item(data, _sort_keys=sort_keys)
43
44    return data.as_string()
45
46
47def parse(string):  # type: (str) -> _TOMLDocument
48    """
49    Parses a string into a TOMLDocument.
50    """
51    return Parser(string).parse()
52
53
54def document():  # type: () -> _TOMLDocument
55    """
56    Returns a new TOMLDocument instance.
57    """
58    return _TOMLDocument()
59
60
61# Items
62def integer(raw):  # type: (str) -> Integer
63    return item(int(raw))
64
65
66def float_(raw):  # type: (str) -> Float
67    return item(float(raw))
68
69
70def boolean(raw):  # type: (str) -> Bool
71    return item(raw == "true")
72
73
74def string(raw):  # type: (str) -> String
75    return item(raw)
76
77
78def date(raw):  # type: (str) -> Date
79    value = parse_rfc3339(raw)
80    if not isinstance(value, _datetime.date):
81        raise ValueError("date() only accepts date strings.")
82
83    return item(value)
84
85
86def time(raw):  # type: (str) -> Time
87    value = parse_rfc3339(raw)
88    if not isinstance(value, _datetime.time):
89        raise ValueError("time() only accepts time strings.")
90
91    return item(value)
92
93
94def datetime(raw):  # type: (str) -> DateTime
95    value = parse_rfc3339(raw)
96    if not isinstance(value, _datetime.datetime):
97        raise ValueError("datetime() only accepts datetime strings.")
98
99    return item(value)
100
101
102def array(raw=None):  # type: (str) -> Array
103    if raw is None:
104        raw = "[]"
105
106    return value(raw)
107
108
109def table():  # type: () -> Table
110    return Table(Container(), Trivia(), False)
111
112
113def inline_table():  # type: () -> InlineTable
114    return InlineTable(Container(), Trivia(), new=True)
115
116
117def aot():  # type: () -> AoT
118    return AoT([])
119
120
121def key(k):  # type: (str) -> Key
122    return Key(k)
123
124
125def value(raw):  # type: (str) -> _Item
126    return Parser(raw)._parse_value()
127
128
129def key_value(src):  # type: (str) -> Tuple[Key, _Item]
130    return Parser(src)._parse_key_value()
131
132
133def ws(src):  # type: (str) -> Whitespace
134    return Whitespace(src, fixed=True)
135
136
137def nl():  # type: () -> Whitespace
138    return ws("\n")
139
140
141def comment(string):  # type: (str) -> Comment
142    return Comment(Trivia(comment_ws="  ", comment="# " + string))
143