forked from python-poetry/tomlkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
282 lines (210 loc) · 6.54 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import datetime as _datetime
from collections.abc import Mapping
from typing import IO
from typing import Iterable
from typing import Tuple
from typing import Union
from ._utils import parse_rfc3339
from .container import Container
from .exceptions import UnexpectedCharError
from .items import AoT
from .items import Array
from .items import Bool
from .items import Comment
from .items import Date
from .items import DateTime
from .items import DottedKey
from .items import Float
from .items import InlineTable
from .items import Integer
from .items import Item as _Item
from .items import Key
from .items import SingleKey
from .items import String
from .items import StringType as _StringType
from .items import Table
from .items import Time
from .items import Trivia
from .items import Whitespace
from .items import item
from .parser import Parser
from .toml_document import TOMLDocument
def loads(string: Union[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) -> 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: Union[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: Union[str, int]) -> Integer:
"""Create an integer item from a number or string."""
return item(int(raw))
def float_(raw: Union[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,
*,
single_quotes: bool = False,
multiline: bool = False,
escape: Union[None, bool] = None,
) -> String:
"""Create a string item.
Boolean flags (e.g. ``single_quotes=True`` and/or ``multiline=True``)
can be used for personalization.
By default, single line strings are escaped, but multi line strings are not.
This can be controlled by explicitly setting ``escape``.
"""
escape = (not multiline) if escape is None else escape
type_ = _StringType.select(single_quotes, 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 = None) -> 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]
"""
if raw is None:
raw = "[]"
return value(raw)
def table(is_super_table: bool = False) -> 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: Union[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))