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