toml-rs 0.2.2__pp311-pypy311_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
toml_rs/__init__.py
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
__all__ = (
|
|
2
|
+
"TOMLDecodeError",
|
|
3
|
+
"TOMLEncodeError",
|
|
4
|
+
"__version__",
|
|
5
|
+
"dump",
|
|
6
|
+
"dumps",
|
|
7
|
+
"load",
|
|
8
|
+
"loads",
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
from collections.abc import Callable
|
|
12
|
+
from pathlib import Path
|
|
13
|
+
from typing import Any, BinaryIO, Literal, TextIO, TypeAlias
|
|
14
|
+
|
|
15
|
+
from ._toml_rs import (
|
|
16
|
+
_dumps,
|
|
17
|
+
_loads,
|
|
18
|
+
_version,
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
__version__: str = _version
|
|
22
|
+
|
|
23
|
+
TomlVersion: TypeAlias = Literal["1.0.0", "1.1.0"]
|
|
24
|
+
|
|
25
|
+
DEFAULT_TOML_VERSION = "1.0.0"
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def load(
|
|
29
|
+
fp: BinaryIO,
|
|
30
|
+
/,
|
|
31
|
+
*,
|
|
32
|
+
parse_float: Callable[[str], Any] = float,
|
|
33
|
+
toml_version: TomlVersion = DEFAULT_TOML_VERSION,
|
|
34
|
+
) -> dict[str, Any]:
|
|
35
|
+
_bytes = fp.read()
|
|
36
|
+
try:
|
|
37
|
+
_str = _bytes.decode()
|
|
38
|
+
except AttributeError:
|
|
39
|
+
msg = "File must be opened in binary mode, e.g. use `open('foo.toml', 'rb')`"
|
|
40
|
+
raise TypeError(msg) from None
|
|
41
|
+
return loads(_str, parse_float=parse_float, toml_version=toml_version)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def loads(
|
|
45
|
+
s: str,
|
|
46
|
+
/,
|
|
47
|
+
*,
|
|
48
|
+
parse_float: Callable[[str], Any] = float,
|
|
49
|
+
toml_version: TomlVersion = DEFAULT_TOML_VERSION,
|
|
50
|
+
) -> dict[str, Any]:
|
|
51
|
+
if not isinstance(s, str):
|
|
52
|
+
raise TypeError(f"Expected str object, not '{type(s).__qualname__}'")
|
|
53
|
+
return _loads(s, parse_float=parse_float, toml_version=toml_version)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def dump(
|
|
57
|
+
obj: Any,
|
|
58
|
+
/,
|
|
59
|
+
file: str | Path | TextIO,
|
|
60
|
+
inline_tables: set[str] | None = None,
|
|
61
|
+
*,
|
|
62
|
+
pretty: bool = False,
|
|
63
|
+
toml_version: TomlVersion = DEFAULT_TOML_VERSION,
|
|
64
|
+
) -> int:
|
|
65
|
+
_str = _dumps(
|
|
66
|
+
obj,
|
|
67
|
+
inline_tables=inline_tables,
|
|
68
|
+
pretty=pretty,
|
|
69
|
+
toml_version=toml_version,
|
|
70
|
+
)
|
|
71
|
+
if isinstance(file, str):
|
|
72
|
+
file = Path(file)
|
|
73
|
+
if isinstance(file, Path):
|
|
74
|
+
return file.write_text(_str, encoding="utf-8")
|
|
75
|
+
else:
|
|
76
|
+
return file.write(_str)
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def dumps(
|
|
80
|
+
obj: Any,
|
|
81
|
+
/,
|
|
82
|
+
inline_tables: set[str] | None = None,
|
|
83
|
+
*,
|
|
84
|
+
pretty: bool = False,
|
|
85
|
+
toml_version: TomlVersion = DEFAULT_TOML_VERSION,
|
|
86
|
+
) -> str:
|
|
87
|
+
return _dumps(
|
|
88
|
+
obj,
|
|
89
|
+
inline_tables=inline_tables,
|
|
90
|
+
pretty=pretty,
|
|
91
|
+
toml_version=toml_version,
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
class TOMLDecodeError(ValueError):
|
|
96
|
+
def __init__(self, msg: str, doc: str, pos: int, *args: Any):
|
|
97
|
+
msg = msg.rstrip()
|
|
98
|
+
super().__init__(msg)
|
|
99
|
+
lineno = doc.count("\n", 0, pos) + 1
|
|
100
|
+
if lineno == 1:
|
|
101
|
+
colno = pos + 1
|
|
102
|
+
else:
|
|
103
|
+
colno = pos - doc.rindex("\n", 0, pos)
|
|
104
|
+
self.msg = msg
|
|
105
|
+
self.doc = doc
|
|
106
|
+
self.pos = pos
|
|
107
|
+
self.colno = colno
|
|
108
|
+
self.lineno = lineno
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
class TOMLEncodeError(TypeError):
|
|
112
|
+
def __init__(self, msg: str, *args: Any):
|
|
113
|
+
msg = msg.rstrip()
|
|
114
|
+
super().__init__(msg)
|
|
115
|
+
self.msg = msg
|
|
Binary file
|
toml_rs/py.typed
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# Marker file for PEP 561
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: toml-rs
|
|
3
|
+
Version: 0.2.2
|
|
4
|
+
Classifier: Typing :: Typed
|
|
5
|
+
Classifier: Programming Language :: Rust
|
|
6
|
+
Classifier: Programming Language :: Python
|
|
7
|
+
Classifier: Programming Language :: Python :: 3
|
|
8
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
9
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
13
|
+
Classifier: Programming Language :: Python :: Implementation :: PyPy
|
|
14
|
+
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
15
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
16
|
+
Classifier: License :: OSI Approved :: The Unlicense (Unlicense)
|
|
17
|
+
License-File: UNLICENSE
|
|
18
|
+
Summary: A High-Performance TOML Parser for Python written in Rust
|
|
19
|
+
Author-email: chirizxc <chirizxc@proton.me>
|
|
20
|
+
Maintainer-email: chirizxc <chirizxc@proton.me>
|
|
21
|
+
License-Expression: UNLICENSE
|
|
22
|
+
Requires-Python: >=3.10
|
|
23
|
+
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
|
|
24
|
+
Project-URL: Source, https://github.com/lava-sh/toml-rs
|
|
25
|
+
Project-URL: Homepage, https://github.com/lava-sh/toml-rs
|
|
26
|
+
Project-URL: Bug Tracker, https://github.com/lava-sh/toml-rs/issues
|
|
27
|
+
|
|
28
|
+
<div align="center">
|
|
29
|
+
|
|
30
|
+
# toml-rs
|
|
31
|
+
|
|
32
|
+
*A High-Performance TOML v1.0.0 and v1.1.0 parser for Python written in Rust*
|
|
33
|
+
|
|
34
|
+
[](https://pypi.org/project/toml_rs/)
|
|
35
|
+
[](https://pypi.org/project/toml_rs/)
|
|
36
|
+
[](https://pypi.org/project/toml_rs/)
|
|
37
|
+
|
|
38
|
+
[](https://pypi.org/project/toml_rs/)
|
|
39
|
+
[](https://github.com/lava-sh/toml-rs)
|
|
40
|
+
|
|
41
|
+
</div>
|
|
42
|
+
|
|
43
|
+
## Features
|
|
44
|
+
|
|
45
|
+
* The fastest TOML parser in Python (see [benchmarks](https://github.com/lava-sh/toml-rs/tree/main/benchmark))
|
|
46
|
+
|
|
47
|
+
* Drop-in compatibility with most [`tomllib`](https://docs.python.org/3/library/tomllib.html) use cases (see [below](#differences-with-tomllib))
|
|
48
|
+
|
|
49
|
+
## Installation
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
# Using pip
|
|
53
|
+
pip install toml-rs
|
|
54
|
+
|
|
55
|
+
# Using uv
|
|
56
|
+
uv pip install toml-rs
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## Examples
|
|
60
|
+
|
|
61
|
+
```python
|
|
62
|
+
import tomllib
|
|
63
|
+
from pprint import pprint
|
|
64
|
+
|
|
65
|
+
import toml_rs
|
|
66
|
+
|
|
67
|
+
toml = """\
|
|
68
|
+
title = "TOML Example"
|
|
69
|
+
|
|
70
|
+
[owner]
|
|
71
|
+
name = "Tom Preston-Werner"
|
|
72
|
+
dob = 1979-05-27T07:32:00-08:00
|
|
73
|
+
|
|
74
|
+
[database]
|
|
75
|
+
enabled = true
|
|
76
|
+
ports = [ 8000, 8001, 8002 ]
|
|
77
|
+
data = [ ["delta", "phi"], [3.14] ]
|
|
78
|
+
temp_targets = { cpu = 79.5, case = 72.0 }
|
|
79
|
+
|
|
80
|
+
[servers]
|
|
81
|
+
[servers.alpha]
|
|
82
|
+
ip = "10.0.0.1"
|
|
83
|
+
role = "frontend"
|
|
84
|
+
[servers.beta]
|
|
85
|
+
ip = "10.0.0.2"
|
|
86
|
+
role = "backend"
|
|
87
|
+
"""
|
|
88
|
+
|
|
89
|
+
tomllib_loads = tomllib.loads(toml)
|
|
90
|
+
toml_rs_loads = toml_rs.loads(toml)
|
|
91
|
+
toml_rs_dumps = toml_rs.dumps(toml_rs_loads)
|
|
92
|
+
|
|
93
|
+
assert tomllib_loads == toml_rs_loads
|
|
94
|
+
|
|
95
|
+
print("toml_rs.loads:")
|
|
96
|
+
pprint(toml_rs_loads)
|
|
97
|
+
print("toml_rs.dumps:")
|
|
98
|
+
print(toml_rs_dumps)
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## Differences with [`tomllib`](https://docs.python.org/3/library/tomllib.html)
|
|
102
|
+
|
|
103
|
+
1. More understandable errors
|
|
104
|
+
|
|
105
|
+
```python
|
|
106
|
+
import tomllib
|
|
107
|
+
|
|
108
|
+
t = """\
|
|
109
|
+
x = 1
|
|
110
|
+
y = 2
|
|
111
|
+
v =
|
|
112
|
+
"""
|
|
113
|
+
print(tomllib.loads(t))
|
|
114
|
+
# tomllib.TOMLDecodeError: Invalid value (at line 3, column 5)
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
```python
|
|
118
|
+
import toml_rs
|
|
119
|
+
|
|
120
|
+
t = """\
|
|
121
|
+
x = 1
|
|
122
|
+
y = 2
|
|
123
|
+
v =
|
|
124
|
+
"""
|
|
125
|
+
print(toml_rs.loads(t))
|
|
126
|
+
# toml_rs.TOMLDecodeError: TOML parse error at line 3, column 5
|
|
127
|
+
# |
|
|
128
|
+
# 3 | v =
|
|
129
|
+
# | ^
|
|
130
|
+
# string values must be quoted, expected literal string
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
2. Strict compliance with TOML v1.0.0
|
|
134
|
+
|
|
135
|
+
From [TOML v1.0.0 spec](https://toml.io/en/v1.0.0#integer):
|
|
136
|
+
|
|
137
|
+
> Arbitrary 64-bit signed integers (from `−2^63` to `2^63−1`) should be accepted and handled losslessly. If an integer cannot be represented losslessly, an error must be thrown.
|
|
138
|
+
|
|
139
|
+
```python
|
|
140
|
+
import tomllib
|
|
141
|
+
|
|
142
|
+
t = "x = 999_999_999_999_999_999_999_999"
|
|
143
|
+
print(tomllib.loads(t))
|
|
144
|
+
# {'x': 999999999999999999999999} <== speс violation
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
```python
|
|
148
|
+
import toml_rs
|
|
149
|
+
|
|
150
|
+
t = "x = 999_999_999_999_999_999_999_999"
|
|
151
|
+
print(toml_rs.loads(t))
|
|
152
|
+
# toml_rs.TOMLDecodeError: TOML parse error at line 1, column 5
|
|
153
|
+
# |
|
|
154
|
+
# 1 | x = 999_999_999_999_999_999_999_999
|
|
155
|
+
# | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
156
|
+
# invalid type: integer `999999999999999999999999` as i128, expected any valid TOML value
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
Note: TOML v1.1.0 [allows parsers to support integers and floats beyond `i64`/`f64` limits](https://github.com/toml-lang/toml/pull/1058),
|
|
160
|
+
so the behavior will be the same as [`tomllib`](https://docs.python.org/3/library/tomllib.html)
|
|
161
|
+
|
|
162
|
+
3. Supports serialization (`toml_rs.dumps` and `toml_rs.dump`)
|
|
163
|
+
|
|
164
|
+
```python
|
|
165
|
+
from pathlib import Path
|
|
166
|
+
|
|
167
|
+
import toml_rs
|
|
168
|
+
|
|
169
|
+
data = {
|
|
170
|
+
"title": "TOML Example",
|
|
171
|
+
"owner": {"name": "Alice", "age": 30},
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
print(toml_rs.dumps(data))
|
|
175
|
+
|
|
176
|
+
toml_rs.dump(data, Path("example.toml"))
|
|
177
|
+
# or `toml_rs.dump(data, "example.toml")`
|
|
178
|
+
```
|
|
179
|
+
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
toml_rs-0.2.2.dist-info/METADATA,sha256=9Nkv9ELaRH5c0OSOJrMgc-tK2EnOmpUFDxu5kwY9CcE,4831
|
|
2
|
+
toml_rs-0.2.2.dist-info/WHEEL,sha256=STZ2cFMnDeKGVvLx9rTR8uRZet7TbXQTcL4SK-uJ1d8,163
|
|
3
|
+
toml_rs-0.2.2.dist-info/licenses/UNLICENSE,sha256=yiq99pWITHfqS0pbZMp7cy2dnbreTuvBwudsU-njvIM,1210
|
|
4
|
+
toml_rs/__init__.py,sha256=k6v3Moj70QjP7xSgHoZvh9p-V_7aAEcZGSLc-fwaC7M,2667
|
|
5
|
+
toml_rs/_toml_rs.pypy311-pp73-aarch64-linux-gnu.so,sha256=0UP_gc7XJqGxP_acbO8Yp2jrEjBoWl1WRYTel3hnRG0,947408
|
|
6
|
+
toml_rs/py.typed,sha256=8ZJUsxZiuOy1oJeVhsTWQhTG_6pTVHVXk5hJL79ebTk,25
|
|
7
|
+
toml_rs-0.2.2.dist-info/RECORD,,
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
This is free and unencumbered software released into the public domain.
|
|
2
|
+
|
|
3
|
+
Anyone is free to copy, modify, publish, use, compile, sell, or
|
|
4
|
+
distribute this software, either in source code form or as a compiled
|
|
5
|
+
binary, for any purpose, commercial or non-commercial, and by any
|
|
6
|
+
means.
|
|
7
|
+
|
|
8
|
+
In jurisdictions that recognize copyright laws, the author or authors
|
|
9
|
+
of this software dedicate any and all copyright interest in the
|
|
10
|
+
software to the public domain. We make this dedication for the benefit
|
|
11
|
+
of the public at large and to the detriment of our heirs and
|
|
12
|
+
successors. We intend this dedication to be an overt act of
|
|
13
|
+
relinquishment in perpetuity of all present and future rights to this
|
|
14
|
+
software under copyright law.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
19
|
+
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
|
20
|
+
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
21
|
+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
|
23
|
+
|
|
24
|
+
For more information, please refer to <http://unlicense.org/>
|