teptris 0.1.0__py3-none-any.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.
- teptris/__core.py +280 -0
- teptris/__init__.py +16 -0
- teptris/_platform/0.1.0/libteptris.dylib +0 -0
- teptris-0.1.0.dist-info/METADATA +34 -0
- teptris-0.1.0.dist-info/RECORD +8 -0
- teptris-0.1.0.dist-info/WHEEL +5 -0
- teptris-0.1.0.dist-info/licenses/LICENSE.txt +21 -0
- teptris-0.1.0.dist-info/top_level.txt +1 -0
teptris/__core.py
ADDED
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
"""ctypes core for teptris-py — no C extension, one shared library.
|
|
2
|
+
|
|
3
|
+
Library resolution (yeptris-py chain): TEPTRIS_LIB_PATH env, vendored
|
|
4
|
+
platform directory, the sibling C checkout's build tree, then the
|
|
5
|
+
system loader.
|
|
6
|
+
"""
|
|
7
|
+
import ctypes
|
|
8
|
+
import ctypes.util
|
|
9
|
+
import struct
|
|
10
|
+
import datetime as _dt
|
|
11
|
+
import os
|
|
12
|
+
from pathlib import Path
|
|
13
|
+
|
|
14
|
+
__all__ = ["TOMLDecodeError", "dumps", "load", "loads", "library_path"]
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class TOMLDecodeError(ValueError):
|
|
18
|
+
def __init__(self, msg, line=0, column=0):
|
|
19
|
+
self.line = line
|
|
20
|
+
self.column = column
|
|
21
|
+
super().__init__(msg)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
_NAMES = ("libteptris.dylib", "libteptris.so", "libteptris.dll")
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def _candidates():
|
|
28
|
+
out = []
|
|
29
|
+
env = os.environ.get("TEPTRIS_LIB_PATH")
|
|
30
|
+
if env:
|
|
31
|
+
out.append(env)
|
|
32
|
+
here = Path(__file__).resolve().parent
|
|
33
|
+
out.extend(str(p) for p in sorted(here.glob("_platform/*/libteptris.*")))
|
|
34
|
+
for name in _NAMES:
|
|
35
|
+
for build in ("build", "build-release", "build-shared", "build-bench"):
|
|
36
|
+
out.append(str(here.parent.parent / "teptris" / build / "src" / name))
|
|
37
|
+
found = ctypes.util.find_library("teptris")
|
|
38
|
+
if found:
|
|
39
|
+
out.append(found)
|
|
40
|
+
return out
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
_lib = None
|
|
44
|
+
library_path = None
|
|
45
|
+
for _c in _candidates():
|
|
46
|
+
try:
|
|
47
|
+
_lib = ctypes.CDLL(_c)
|
|
48
|
+
library_path = _c
|
|
49
|
+
break
|
|
50
|
+
except OSError:
|
|
51
|
+
continue
|
|
52
|
+
if _lib is None:
|
|
53
|
+
raise ImportError("libteptris not found (set TEPTRIS_LIB_PATH)")
|
|
54
|
+
|
|
55
|
+
_OK = 0
|
|
56
|
+
_STRING, _INTEGER, _FLOAT, _BOOLEAN = 0, 1, 2, 3
|
|
57
|
+
_DT, _DT_LOCAL, _DATE, _TIME = 4, 5, 6, 7
|
|
58
|
+
_ARRAY, _TABLE = 8, 9
|
|
59
|
+
|
|
60
|
+
class _ViewP(ctypes.Structure):
|
|
61
|
+
_fields_ = [("ptr", ctypes.c_char_p), ("len", ctypes.c_size_t)]
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
class _DateTime(ctypes.Structure):
|
|
65
|
+
_fields_ = [("year", ctypes.c_int32), ("month", ctypes.c_uint8),
|
|
66
|
+
("day", ctypes.c_uint8), ("hour", ctypes.c_uint8),
|
|
67
|
+
("minute", ctypes.c_uint8), ("second", ctypes.c_uint8),
|
|
68
|
+
("_pad", ctypes.c_uint8 * 3), ("nanosecond", ctypes.c_uint32),
|
|
69
|
+
("offset_seconds", ctypes.c_int32)]
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
_lib.teptris_parse.restype = ctypes.c_int
|
|
73
|
+
_lib.teptris_parse.argtypes = [ctypes.c_char_p, ctypes.c_size_t,
|
|
74
|
+
ctypes.c_void_p, ctypes.POINTER(ctypes.c_void_p)]
|
|
75
|
+
_lib.teptris_document_free.argtypes = [ctypes.c_void_p]
|
|
76
|
+
_lib.teptris_document_error.restype = ctypes.c_void_p
|
|
77
|
+
_lib.teptris_document_root.restype = ctypes.c_void_p
|
|
78
|
+
_lib.teptris_document_root.argtypes = [ctypes.c_void_p]
|
|
79
|
+
_lib.teptris_node_kind.restype = ctypes.c_int
|
|
80
|
+
_lib.teptris_node_kind.argtypes = [ctypes.c_void_p]
|
|
81
|
+
_lib.teptris_node_string.argtypes = [ctypes.c_void_p, ctypes.POINTER(_ViewP)]
|
|
82
|
+
_lib.teptris_node_integer.argtypes = [ctypes.c_void_p, ctypes.POINTER(ctypes.c_int64)]
|
|
83
|
+
_lib.teptris_node_float.argtypes = [ctypes.c_void_p, ctypes.POINTER(ctypes.c_double)]
|
|
84
|
+
_lib.teptris_node_boolean.argtypes = [ctypes.c_void_p, ctypes.POINTER(ctypes.c_bool)]
|
|
85
|
+
_lib.teptris_node_datetime.argtypes = [ctypes.c_void_p, ctypes.POINTER(_DateTime)]
|
|
86
|
+
_lib.teptris_node_array_length.restype = ctypes.c_size_t
|
|
87
|
+
_lib.teptris_node_array_length.argtypes = [ctypes.c_void_p]
|
|
88
|
+
_lib.teptris_node_array_at.restype = ctypes.c_void_p
|
|
89
|
+
_lib.teptris_node_array_at.argtypes = [ctypes.c_void_p, ctypes.c_size_t]
|
|
90
|
+
_lib.teptris_node_table_length.restype = ctypes.c_size_t
|
|
91
|
+
_lib.teptris_node_table_length.argtypes = [ctypes.c_void_p]
|
|
92
|
+
_lib.teptris_node_table_at.restype = ctypes.c_void_p
|
|
93
|
+
_lib.teptris_node_table_at.argtypes = [ctypes.c_void_p, ctypes.c_size_t,
|
|
94
|
+
ctypes.POINTER(_ViewP)]
|
|
95
|
+
_lib.teptris_document_flatten.restype = ctypes.c_int
|
|
96
|
+
_lib.teptris_document_flatten.argtypes = [ctypes.c_void_p,
|
|
97
|
+
ctypes.POINTER(ctypes.c_void_p),
|
|
98
|
+
ctypes.POINTER(ctypes.c_size_t)]
|
|
99
|
+
_lib.teptris_flatten_free.argtypes = [ctypes.c_void_p]
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
class _Error(ctypes.Structure):
|
|
103
|
+
_fields_ = [("status", ctypes.c_int), ("line", ctypes.c_size_t),
|
|
104
|
+
("column", ctypes.c_size_t), ("message", ctypes.c_char_p)]
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def loads(data):
|
|
108
|
+
if isinstance(data, str):
|
|
109
|
+
data = data.encode("utf-8")
|
|
110
|
+
elif not isinstance(data, (bytes, bytearray)):
|
|
111
|
+
raise TypeError("loads() expects str or bytes")
|
|
112
|
+
data = bytes(data)
|
|
113
|
+
handle = ctypes.c_void_p()
|
|
114
|
+
st = _lib.teptris_parse(data, len(data), None, ctypes.byref(handle))
|
|
115
|
+
if st != _OK:
|
|
116
|
+
msg, line, column = "parse failed", 0, 0
|
|
117
|
+
p = _lib.teptris_document_error(handle)
|
|
118
|
+
if p:
|
|
119
|
+
e = ctypes.cast(p, ctypes.POINTER(_Error)).contents
|
|
120
|
+
msg = e.message.decode("utf-8", "replace")
|
|
121
|
+
line, column = e.line, e.column
|
|
122
|
+
_lib.teptris_document_free(handle)
|
|
123
|
+
raise TOMLDecodeError(f"{msg} (line {line} column {column})",
|
|
124
|
+
line, column)
|
|
125
|
+
try:
|
|
126
|
+
return _loads_flat(handle)
|
|
127
|
+
finally:
|
|
128
|
+
_lib.teptris_document_free(handle)
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def load(fp):
|
|
132
|
+
if hasattr(fp, "read"):
|
|
133
|
+
return loads(fp.read())
|
|
134
|
+
with open(fp, "rb") as f:
|
|
135
|
+
return loads(f.read())
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
_U32 = struct.Struct("<I")
|
|
139
|
+
_I64 = struct.Struct("<q")
|
|
140
|
+
_F64 = struct.Struct("<d")
|
|
141
|
+
_DTS = struct.Struct("<qBBBBBIq") # year mon day hour min sec ns offset (25 B)
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def _loads_flat(handle):
|
|
145
|
+
bufp, lenp = ctypes.c_void_p(), ctypes.c_size_t()
|
|
146
|
+
st = _lib.teptris_document_flatten(handle, ctypes.byref(bufp),
|
|
147
|
+
ctypes.byref(lenp))
|
|
148
|
+
if st != _OK:
|
|
149
|
+
raise TOMLDecodeError("flatten failed")
|
|
150
|
+
try:
|
|
151
|
+
data = ctypes.string_at(bufp.value, lenp.value)
|
|
152
|
+
value, _ = _decode(data, 0)
|
|
153
|
+
return value
|
|
154
|
+
finally:
|
|
155
|
+
_lib.teptris_flatten_free(bufp.value)
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
def _decode(d, pos):
|
|
159
|
+
tag = d[pos]
|
|
160
|
+
if tag == 0x01: # table
|
|
161
|
+
(n,) = _U32.unpack_from(d, pos + 1)
|
|
162
|
+
pos += 5
|
|
163
|
+
out = {}
|
|
164
|
+
for _ in range(n):
|
|
165
|
+
(kl,) = _U32.unpack_from(d, pos)
|
|
166
|
+
pos += 4
|
|
167
|
+
key = d[pos:pos + kl].decode("utf-8")
|
|
168
|
+
pos += kl
|
|
169
|
+
val, pos = _decode(d, pos)
|
|
170
|
+
out[key] = val
|
|
171
|
+
return out, pos
|
|
172
|
+
if tag == 0x02: # array
|
|
173
|
+
(n,) = _U32.unpack_from(d, pos + 1)
|
|
174
|
+
pos += 5
|
|
175
|
+
arr = []
|
|
176
|
+
for _ in range(n):
|
|
177
|
+
v, pos = _decode(d, pos)
|
|
178
|
+
arr.append(v)
|
|
179
|
+
return arr, pos
|
|
180
|
+
if tag == 0x03: # string
|
|
181
|
+
(l,) = _U32.unpack_from(d, pos + 1)
|
|
182
|
+
return d[pos + 5:pos + 5 + l].decode("utf-8"), pos + 5 + l
|
|
183
|
+
if tag == 0x04:
|
|
184
|
+
return _I64.unpack_from(d, pos + 1)[0], pos + 9
|
|
185
|
+
if tag == 0x05:
|
|
186
|
+
return _F64.unpack_from(d, pos + 1)[0], pos + 9
|
|
187
|
+
if tag == 0x06:
|
|
188
|
+
return False, pos + 1
|
|
189
|
+
if tag == 0x07:
|
|
190
|
+
return True, pos + 1
|
|
191
|
+
if 0x08 <= tag <= 0x0B:
|
|
192
|
+
y, mon, day, h, mi, sec, ns, off = _DTS.unpack_from(d, pos + 1)
|
|
193
|
+
micro = ns // 1000
|
|
194
|
+
if tag == 0x0A:
|
|
195
|
+
return _dt.date(y, mon, day), pos + 26
|
|
196
|
+
if tag == 0x0B:
|
|
197
|
+
return _dt.time(h, mi, sec, micro), pos + 26
|
|
198
|
+
base = _dt.datetime(y, mon, day, h, mi, sec, micro)
|
|
199
|
+
if tag == 0x08:
|
|
200
|
+
return base.replace(tzinfo=_dt.timezone(_dt.timedelta(seconds=off))), pos + 26
|
|
201
|
+
return base, pos + 26
|
|
202
|
+
raise TOMLDecodeError(f"corrupt flat buffer at {pos}")
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
# ---- dumps (tomli_w-shaped) ---------------------------------------------
|
|
206
|
+
|
|
207
|
+
_ESCAPES = {"\\": "\\\\", '"': '\\"', "\b": "\\b", "\f": "\\f",
|
|
208
|
+
"\n": "\\n", "\r": "\\r", "\t": "\\t"}
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
def _key(k):
|
|
212
|
+
k = str(k)
|
|
213
|
+
if k.isascii() and k and all(c.isalnum() or c in "_-" for c in k):
|
|
214
|
+
return k
|
|
215
|
+
if "'" not in k and all(ord(c) >= 0x20 for c in k):
|
|
216
|
+
return f"'{k}'"
|
|
217
|
+
return '"' + "".join(_ESCAPES.get(c, c) for c in k) + '"'
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
def _value(v):
|
|
221
|
+
if isinstance(v, bool):
|
|
222
|
+
return "true" if v else "false"
|
|
223
|
+
if isinstance(v, int):
|
|
224
|
+
return str(v)
|
|
225
|
+
if isinstance(v, float):
|
|
226
|
+
if v != v:
|
|
227
|
+
return "nan"
|
|
228
|
+
if v == float("inf"):
|
|
229
|
+
return "inf"
|
|
230
|
+
if v == float("-inf"):
|
|
231
|
+
return "-inf"
|
|
232
|
+
s = repr(v)
|
|
233
|
+
return s if any(c in s for c in ".eE") else s + ".0"
|
|
234
|
+
if isinstance(v, str):
|
|
235
|
+
if "'" not in v and all(ord(c) >= 0x20 for c in v):
|
|
236
|
+
return f"'{v}'"
|
|
237
|
+
return '"' + "".join(_ESCAPES.get(c, c) for c in v) + '"'
|
|
238
|
+
if isinstance(v, _dt.datetime):
|
|
239
|
+
s = v.isoformat()
|
|
240
|
+
return s if v.tzinfo is not None else s
|
|
241
|
+
if isinstance(v, _dt.date):
|
|
242
|
+
return v.isoformat()
|
|
243
|
+
if isinstance(v, _dt.time):
|
|
244
|
+
return v.isoformat()
|
|
245
|
+
if isinstance(v, list):
|
|
246
|
+
return "[" + ", ".join(_value(e) for e in v) + "]"
|
|
247
|
+
if isinstance(v, dict):
|
|
248
|
+
return "{" + ", ".join(f"{_key(k)} = {_value(e)}"
|
|
249
|
+
for k, e in v.items()) + "}"
|
|
250
|
+
raise TypeError(f"cannot dump {type(v).__name__}")
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
def _is_aot(v):
|
|
254
|
+
return isinstance(v, list) and v and all(isinstance(e, dict) for e in v)
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
def _emit_table(obj, out, prefix):
|
|
258
|
+
for k, v in obj.items():
|
|
259
|
+
if isinstance(v, dict) or _is_aot(v):
|
|
260
|
+
continue
|
|
261
|
+
out.append(f"{_key(k)} = {_value(v)}\n")
|
|
262
|
+
for k, v in obj.items():
|
|
263
|
+
if not (isinstance(v, dict) or _is_aot(v)):
|
|
264
|
+
continue
|
|
265
|
+
path = f"{prefix}.{_key(k)}" if prefix else _key(k)
|
|
266
|
+
if _is_aot(v):
|
|
267
|
+
for item in v:
|
|
268
|
+
out.append(f"[[{path}]]\n")
|
|
269
|
+
_emit_table(item, out, path)
|
|
270
|
+
else:
|
|
271
|
+
out.append(f"[{path}]\n")
|
|
272
|
+
_emit_table(v, out, path)
|
|
273
|
+
|
|
274
|
+
|
|
275
|
+
def dumps(obj):
|
|
276
|
+
if not isinstance(obj, dict):
|
|
277
|
+
raise TypeError("dumps() expects a dict at the top level")
|
|
278
|
+
out = []
|
|
279
|
+
_emit_table(obj, out, None)
|
|
280
|
+
return "".join(out)
|
teptris/__init__.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"""teptris — TOML for Python at libleptris speed.
|
|
2
|
+
|
|
3
|
+
ctypes binding over libteptris (C11 TOML 1.0). API shape mirrors
|
|
4
|
+
tomllib / tomli: ``loads(str) -> dict`` raising ``TOMLDecodeError``,
|
|
5
|
+
plus a ``dumps(obj) -> str`` writer in the tomli_w spirit. Datetime
|
|
6
|
+
mapping mirrors tomllib: aware/naive ``datetime``, ``date``, ``time``.
|
|
7
|
+
"""
|
|
8
|
+
from .__core import (
|
|
9
|
+
TOMLDecodeError,
|
|
10
|
+
dumps,
|
|
11
|
+
load,
|
|
12
|
+
loads,
|
|
13
|
+
library_path,
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
__all__ = ["TOMLDecodeError", "dumps", "load", "loads", "library_path"]
|
|
Binary file
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: teptris
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: TOML for Python at libleptris speed (ctypes, no C extension)
|
|
5
|
+
License: MIT
|
|
6
|
+
Requires-Python: >=3.8
|
|
7
|
+
Description-Content-Type: text/markdown
|
|
8
|
+
License-File: LICENSE.txt
|
|
9
|
+
Dynamic: license-file
|
|
10
|
+
|
|
11
|
+
# teptris-py — TOML for Python at libleptris speed
|
|
12
|
+
|
|
13
|
+
A ctypes binding (no C extension) over
|
|
14
|
+
[libteptris](https://github.com/leptris/teptris) — the TOML counterpart
|
|
15
|
+
of leptris-py (XML) and yeptris-py (YAML). API shape mirrors
|
|
16
|
+
`tomllib`/`tomli` (`loads`/`load` raising `TOMLDecodeError`) plus a
|
|
17
|
+
`dumps` writer in the `tomli_w` spirit. Datetime mapping mirrors
|
|
18
|
+
tomllib: aware/naive `datetime`, `date`, `time`.
|
|
19
|
+
|
|
20
|
+
## Development
|
|
21
|
+
|
|
22
|
+
The package loads a shared `libteptris` — from `TEPTRIS_LIB_PATH`, a
|
|
23
|
+
vendored `platform/` directory, the sibling C checkout's build tree, or
|
|
24
|
+
the system loader.
|
|
25
|
+
|
|
26
|
+
```sh
|
|
27
|
+
cmake -B ../teptris/build-shared -S ../teptris \
|
|
28
|
+
-DCMAKE_BUILD_TYPE=Release -DTEPTRIS_BUILD_SHARED=ON -DTEPTRIS_BUILD_STATIC=OFF
|
|
29
|
+
cmake --build ../teptris/build-shared
|
|
30
|
+
TEPTRIS_LIB_PATH=../teptris/build-shared/src/libteptris.dylib \
|
|
31
|
+
python3 -m unittest discover -s tests -v
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Publishing and version numbers are USER release decisions.
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
teptris/__core.py,sha256=lJ0S5R-2FxCvjvbiBgNNCIKmCKNESeTznhUNwrjrXEM,9604
|
|
2
|
+
teptris/__init__.py,sha256=Nb1OJ_zB48A0UcQaXEFesGuu10C44DS1Ox7UnQJwkXI,498
|
|
3
|
+
teptris/_platform/0.1.0/libteptris.dylib,sha256=20B_QG5Yw_jSRm786DstM1AZUr-GVZX33g6_GSvv9Hw,104208
|
|
4
|
+
teptris-0.1.0.dist-info/licenses/LICENSE.txt,sha256=uOMiK9Xqmgm4yt24rkR9ZIzYHnvoqweNGtzkkJ7AEII,1077
|
|
5
|
+
teptris-0.1.0.dist-info/METADATA,sha256=AhfPqC0kKUU1v2mQfb0Ib9E3Wh1ZK1MwyzkMwggAxrw,1186
|
|
6
|
+
teptris-0.1.0.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
|
|
7
|
+
teptris-0.1.0.dist-info/top_level.txt,sha256=31LlG_3yPYjK2UUviEO-havzjTbm21CMfmMLRGhT2lA,8
|
|
8
|
+
teptris-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 teptris contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
teptris
|