zig-zon 0.2.6__tar.gz → 0.3.1__tar.gz

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.
Files changed (29) hide show
  1. zig-zon-0.3.1/PKG-INFO +177 -0
  2. {zig-zon-0.2.6 → zig-zon-0.3.1}/build.zig +18 -0
  3. {zig-zon-0.2.6 → zig-zon-0.3.1}/build.zig.zon +1 -1
  4. {zig-zon-0.2.6 → zig-zon-0.3.1}/setup.zig.json +1 -1
  5. zig-zon-0.3.1/src/zig_zon.pyi +95 -0
  6. zig-zon-0.3.1/src/zig_zon.zig +919 -0
  7. zig-zon-0.2.6/PKG-INFO +0 -90
  8. zig-zon-0.2.6/src/zig_zon.pyi +0 -5
  9. zig-zon-0.2.6/src/zig_zon.zig +0 -312
  10. {zig-zon-0.2.6 → zig-zon-0.3.1}/LICENSE +0 -0
  11. {zig-zon-0.2.6 → zig-zon-0.3.1}/pkg-info.zon +0 -0
  12. {zig-zon-0.2.6 → zig-zon-0.3.1}/setup.py +0 -0
  13. {zig-zon-0.2.6 → zig-zon-0.3.1}/zig-pkg/zig_python_sdist-0.0.8/LICENSE +0 -0
  14. {zig-zon-0.2.6 → zig-zon-0.3.1}/zig-pkg/zig_python_sdist-0.0.8/README.md +0 -0
  15. {zig-zon-0.2.6 → zig-zon-0.3.1}/zig-pkg/zig_python_sdist-0.0.8/build.zig +0 -0
  16. {zig-zon-0.2.6 → zig-zon-0.3.1}/zig-pkg/zig_python_sdist-0.0.8/build.zig.zon +0 -0
  17. {zig-zon-0.2.6 → zig-zon-0.3.1}/zig-pkg/zig_python_sdist-0.0.8/pkg-info.zon +0 -0
  18. {zig-zon-0.2.6 → zig-zon-0.3.1}/zig-pkg/zig_python_sdist-0.0.8/src/build/CPython.zig +0 -0
  19. {zig-zon-0.2.6 → zig-zon-0.3.1}/zig-pkg/zig_python_sdist-0.0.8/src/build/Compression.zig +0 -0
  20. {zig-zon-0.2.6 → zig-zon-0.3.1}/zig-pkg/zig_python_sdist-0.0.8/src/build/LowerDashString.zig +0 -0
  21. {zig-zon-0.2.6 → zig-zon-0.3.1}/zig-pkg/zig_python_sdist-0.0.8/src/build/SDistStep.zig +0 -0
  22. {zig-zon-0.2.6 → zig-zon-0.3.1}/zig-pkg/zig_python_sdist-0.0.8/src/build/VenvInstallTest.zig +0 -0
  23. {zig-zon-0.2.6 → zig-zon-0.3.1}/zig-pkg/zig_python_sdist-0.0.8/src/build/extract_link_info.py +0 -0
  24. {zig-zon-0.2.6 → zig-zon-0.3.1}/zig-pkg/zig_python_sdist-0.0.8/src/build/pip_runner.py +0 -0
  25. {zig-zon-0.2.6 → zig-zon-0.3.1}/zig-pkg/zig_python_sdist-0.0.8/src/build/pkg_info.zig +0 -0
  26. {zig-zon-0.2.6 → zig-zon-0.3.1}/zig-pkg/zig_python_sdist-0.0.8/src/build/python-translatec.h +0 -0
  27. {zig-zon-0.2.6 → zig-zon-0.3.1}/zig-pkg/zig_python_sdist-0.0.8/src/build/python.zig +0 -0
  28. {zig-zon-0.2.6 → zig-zon-0.3.1}/zig-pkg/zig_python_sdist-0.0.8/src/build/python_allocator.zig +0 -0
  29. {zig-zon-0.2.6 → zig-zon-0.3.1}/zig-pkg/zig_python_sdist-0.0.8/src/build/setup_json.zig +0 -0
zig-zon-0.3.1/PKG-INFO ADDED
@@ -0,0 +1,177 @@
1
+ Metadata-Version: 2.4
2
+ Name: zig-zon
3
+ Version: 0.3.1
4
+ Summary: a native cpython extension to parse zig object notation strings
5
+ Author-email: Tobias Simetsreiter <dasimmet@gmail.com>
6
+ Maintainer-email: Tobias Simetsreiter <dasimmet@gmail.com>
7
+ License-Expression: MIT
8
+ Keywords: zig,ziglang,cpython,native,sdist,zon
9
+ Requires-Python: >=3.8
10
+ Project-URL: Homepage, https://codeberg.org/dasimmet/python-zig-zon
11
+ Project-URL: Issues, https://codeberg.org/dasimmet/python-zig-zon/issues
12
+ Classifier: Development Status :: 4 - Beta
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Programming Language :: Zig
15
+ Classifier: Intended Audience :: Developers
16
+ Classifier: Topic :: Software Development :: Libraries
17
+ Description-Content-Type: text/markdown
18
+
19
+ # Python Zig Zon Parser
20
+
21
+ A native python extension built with zig to parse zig-object-notation strings
22
+ into python objects.
23
+
24
+ On PyPi it is only distributed in source, and built during install.
25
+ It therefore needs to download the `ziglang` python package.
26
+
27
+ Depending on your OS, you may need the python development headers
28
+ (e.g. `python3-dev`) installed.
29
+
30
+ ## Installation
31
+
32
+ ```bash
33
+ # from PyPi: https://pypi.org/project/zig-zon
34
+ pip install zig-zon
35
+ # directly from the Codeberg Release:
36
+ pip install https://codeberg.org/dasimmet/python-zig-zon/releases/download/0.3.1/zig_zon-0.3.1.tar.gz
37
+ ```
38
+
39
+ ## Parsing
40
+
41
+ ```python
42
+ >>> import zig_zon
43
+ >>> parsed = zig_zon.parse('.{.allyourcode = .are_belong_to_us, .asd = 123}')
44
+ >>> print(parsed)
45
+ {'allyourcode': 'are_belong_to_us', 'asd': 123}
46
+ ```
47
+
48
+ ### Parsing Options
49
+
50
+ - **`bytes`**: Controls how binary / non-UTF-8 string literals in ZON are handled:
51
+ - `"error"` (default): Non-UTF-8 string literals raise a `UnicodeDecodeError`.
52
+ - `"auto"`: Valid UTF-8 strings are returned as `zig_zon.String` (subclasses `str`); strings containing invalid UTF-8 bytes are returned as `zig_zon.ByteString` (subclasses `bytes`).
53
+ - `"always"`: Always returns `zig_zon.ByteString` for all string literals.
54
+
55
+ or, look at the [test file](https://codeberg.org/dasimmet/python-zig-zon/src/branch/main/src/test_zig_zon.py)
56
+
57
+ ## Stringification / Serialization
58
+
59
+ Use `zig_zon.stringify` to serialize Python objects to ZON strings, or `zig_zon.dump` to write directly to a file-like object:
60
+
61
+ ```python
62
+ >>> import zig_zon
63
+ >>> data = {
64
+ ... "name": zig_zon.Enum("my_pkg"),
65
+ ... "version": "0.1.0",
66
+ ... "fingerprint": zig_zon.Hex(0x1234abcd),
67
+ ... "dependencies": {
68
+ ... "dep1": {
69
+ ... "url": "https://example.com/dep1.tar.gz",
70
+ ... "hash": "1220...",
71
+ ... }
72
+ ... },
73
+ ... "paths": ["build.zig", "build.zig.zon", "src"],
74
+ ... }
75
+ >>> print(zig_zon.stringify(data))
76
+ .{
77
+ .name = .my_pkg,
78
+ .version = "0.1.0",
79
+ .fingerprint = 0x1234abcd,
80
+ .dependencies = .{
81
+ .dep1 = .{
82
+ .url = "https://example.com/dep1.tar.gz",
83
+ .hash = "1220...",
84
+ },
85
+ },
86
+ .paths = .{
87
+ "build.zig",
88
+ "build.zig.zon",
89
+ "src",
90
+ },
91
+ }
92
+ ```
93
+
94
+ Write to a file or stream:
95
+
96
+ ```python
97
+ with open("build.zig.zon", "w", encoding="utf-8") as f:
98
+ zig_zon.dump(data, f)
99
+ ```
100
+
101
+ ### Serialization Options
102
+
103
+ - **`integer_mode`**: Controls integer formatting:
104
+ - `"auto"` (default): Uses `Hex` -> hex (`0x...`), `Integer` -> decimal, plain `int` -> decimal.
105
+ - `"int"`: Always formats integers as decimal.
106
+ - `"hex"`: Always formats integers as hex (`0x...`).
107
+ - `"fallback_int"`: Uses `Hex` -> hex, `Integer` -> decimal, plain `int` -> decimal.
108
+ - `"fallback_hex"`: Uses `Integer` -> decimal, `Hex` -> hex, plain `int` -> hex.
109
+ - `"error"`: Errors out (`TypeError`) if an integer is not a `zig_zon.Integer` or `zig_zon.Hex` subclass.
110
+ - **`dict_as_tuples`** (bool, default `False`): If `True`, converts dictionaries to tuples of key-value tuples (`.{ .{ "key", value }, ... }`) rather than struct notation (`.{ .key = value }`).
111
+ - **`encoding`** (str, default `"utf-8"`): Specifies the encoding used for raw strings (`str`) and encoded strings (`bytes`/`bytearray`).
112
+ - **`whitespace`** (bool, default `True`): If `False`, omits unnecessary whitespace and newlines for compact output.
113
+
114
+ ### Supported Python Types for Serialization
115
+
116
+ - `list`, `tuple`, `zig_zon.Tuple`: Serialized as ZON tuples `.{ ... }`.
117
+ - `set`, `frozenset`: Serialized as ZON tuples `.{ ... }`.
118
+ - `namedtuple`: Serialized as ZON structs `.{ .field = value }`.
119
+ - `dict`, `zig_zon.Struct`: Serialized as structs or key-value tuples (with `dict_as_tuples=True`).
120
+ - `str`, `zig_zon.String`: Serialized as string literals `"..."`.
121
+ - `bytes`, `bytearray`: Decoded using `encoding` and serialized as string literals `"..."`.
122
+ - `zig_zon.ByteString`: Serialized directly as binary string literals `"..."` preserving raw byte escapes.
123
+ - `zig_zon.BareEnum`: Serialized as bare enums `.identifier`.
124
+ - `zig_zon.QuotedEnum`: Serialized as quoted enums `.@"identifier"`.
125
+ - `zig_zon.Enum`: Serialized as enum literals (bare or quoted as necessary).
126
+ - `int`, `zig_zon.Integer`, `zig_zon.Hex`: Formatted according to `integer_mode`.
127
+ - `float`, `zig_zon.Float`: Serialized as float literals.
128
+ - `bool`: Serialized as `true` or `false`.
129
+ - `None`: Serialized as `null`.
130
+
131
+ ## Supported Zon values and their python type
132
+
133
+ The parser returns custom classes for each Zig type that wrap and subclass standard Python types:
134
+
135
+ - `structs` -> `zig_zon.Struct` (subclasses `dict`)
136
+ - `tuples` -> `zig_zon.Tuple` (subclasses `list`)
137
+ - `integer literals` -> `zig_zon.Integer` (subclasses `int`)
138
+ - `hex literals` -> `zig_zon.Hex` (wraps / subclasses `int`)
139
+ - `float literals` -> `zig_zon.Float` (subclasses `float`)
140
+ - `booleans` -> `bool` (Python built-in `bool` is final and cannot be subclassed)
141
+ - `null` -> `None` (Python `NoneType` is final and cannot be subclassed)
142
+ - `string literals` -> `zig_zon.String` (subclasses `str`)
143
+ - `binary string literals` -> `zig_zon.ByteString` (subclasses `bytes`)
144
+ - `bare enum literals` -> `zig_zon.BareEnum` (subclasses `zig_zon.Enum` -> `str`)
145
+ - `quoted enum literals` -> `zig_zon.QuotedEnum` (subclasses `zig_zon.Enum` -> `str`)
146
+
147
+ Because the custom classes wrap and subclass their respective Python base types, both `isinstance(val, BaseType)` and `isinstance(val, zig_zon.CustomClass)` evaluate to `True`.
148
+
149
+ ## Not supported values
150
+
151
+ - `bigint`
152
+
153
+ ## How it works
154
+
155
+ The `zig build` commands produces a python source distribution archive ready
156
+ for uploading to Pypi. It includes the sources of [zig-python-sdist](https://codeberg.org/dasimmet/zig-python-sdist)
157
+ and a custom `setup.py` that handles the build of the cpython extension.
158
+
159
+ When you run pip install, the source distribution is downloaded, and since
160
+ this source distribution depends on the `ziglang` python package at build time,
161
+ a `zig build` command is used to compile the extension.
162
+
163
+ ## Developing
164
+
165
+ as opposed to other solutions for building cpython extensions in zig,
166
+ i want to develop this using:
167
+
168
+ ```
169
+ zig build
170
+ ```
171
+
172
+ for linking against the local python installation [zig-python-sdist](https://codeberg.org/dasimmet/zig-python-sdist)
173
+ runs a python script that imports the cpython headers into zig's build cache,
174
+ run `translate-c` on the result and offers a `cpython` module to import
175
+ in our zig code.
176
+ On windows, the extension library is also linked against
177
+ the python runtime static lib and renamed to a `.pyd` file for importing.
@@ -18,6 +18,7 @@ pub fn build(b: *Build) void {
18
18
  .name = "zig-zon",
19
19
  .version = zon.version,
20
20
  .description = b.path("README.md"),
21
+ .py_limited_api = "cp39",
21
22
  .build = .{
22
23
  .args = &.{
23
24
  ext_install_step.name,
@@ -116,4 +117,21 @@ pub fn build(b: *Build) void {
116
117
  b.fmt("src/{s}.pyi", .{lib.name}),
117
118
  );
118
119
  }
120
+
121
+ const opt_check_fmt = b.option(bool, "check-fmt", "Check formatting without modifying files") orelse false;
122
+ const opt_fmt_check = b.option(bool, "fmt-check", "Check formatting without modifying files") orelse false;
123
+ const check_fmt = opt_check_fmt or opt_fmt_check;
124
+ const fmt_step = b.step("fmt", "Run zig fmt");
125
+ const fmt = b.addFmt(.{
126
+ .paths = &.{
127
+ "src",
128
+ "build.zig",
129
+ "build.zig.zon",
130
+ },
131
+ .check = check_fmt,
132
+ });
133
+ fmt_step.dependOn(&fmt.step);
134
+ if (check_fmt) {
135
+ b.getInstallStep().dependOn(&fmt.step);
136
+ }
119
137
  }
@@ -1,7 +1,7 @@
1
1
  .{
2
2
  .name = .python_zig_zon,
3
3
  .fingerprint = 0x3d332656a3644fc9,
4
- .version = "0.2.6",
4
+ .version = "0.3.1",
5
5
  .minimum_zig_version = "0.16.0",
6
6
  .dependencies = .{
7
7
  .zig_python_sdist = .{
@@ -1 +1 @@
1
- {"name":"zig_zon","version":{"major":0,"minor":2,"patch":6},"zig_version":{"major":0,"minor":16,"patch":0},"build":{"args":["--fork=zig-pkg/zig_python_sdist-0.0.8","bdist-wheel","-Doptimize=ReleaseSmall"],"requires":[],"packages":[],"copy_patterns":[{"pattern":"zig-out/lib/*","package_dir":false}]},"has_ext_modules":true}
1
+ {"name":"zig_zon","version":{"major":0,"minor":3,"patch":1},"zig_version":{"major":0,"minor":16,"patch":0},"build":{"args":["--fork=zig-pkg/zig_python_sdist-0.0.8","bdist-wheel","-Doptimize=ReleaseSmall"],"requires":[],"packages":[],"copy_patterns":[{"pattern":"zig-out/lib/*","package_dir":false}]},"py_limited_api":"cp39","has_ext_modules":true}
@@ -0,0 +1,95 @@
1
+ """A native Python extension built with Zig to parse and stringify Zig Object Notation (ZON)."""
2
+
3
+ from typing import Any, IO, Literal, Optional
4
+
5
+ __version__: dict
6
+ __version_str__: str
7
+ __zig_builtin__: dict
8
+
9
+ class Struct(dict):
10
+ """a zon struct wrapping dict"""
11
+
12
+ class Tuple(list):
13
+ """a zon tuple wrapping list"""
14
+
15
+ class Integer(int):
16
+ """a zon integer wrapping int"""
17
+
18
+ class Hex(int):
19
+ """a zon hex wrapping int"""
20
+
21
+ class Float(float):
22
+ """a zon float wrapping float"""
23
+
24
+ class String(str):
25
+ """a zon string wrapping str"""
26
+
27
+ class ByteString(bytes):
28
+ """a zon bytestring wrapping bytes"""
29
+
30
+ class Enum(str):
31
+ """a zon enum wrapping str"""
32
+
33
+ class BareEnum(Enum):
34
+ """a zon bare enum wrapping Enum"""
35
+
36
+ class QuotedEnum(Enum):
37
+ """a zon quoted enum wrapping Enum"""
38
+
39
+ def parse(
40
+ zon: str,
41
+ bytes: Literal["error", "auto", "always"] = "error",
42
+ ) -> Struct | Tuple | Integer | Hex | Float | String | ByteString | BareEnum | QuotedEnum | bool | None:
43
+ """parse a zon literal to a python object
44
+
45
+ Args:
46
+ zon: The ZON string to parse.
47
+ bytes: How to handle binary strings ('error', 'auto', or 'always').
48
+
49
+ Returns:
50
+ The parsed Python object with custom subclasses.
51
+ """
52
+ ...
53
+
54
+ def stringify(
55
+ value: Any,
56
+ *,
57
+ integer_mode: Literal["auto", "int", "hex", "fallback_int", "fallback_hex", "error"] = "auto",
58
+ dict_as_tuples: bool = False,
59
+ encoding: str = "utf-8",
60
+ whitespace: bool = True,
61
+ ) -> str:
62
+ """Serialize a Python object to a Zig Object Notation (ZON) formatted string.
63
+
64
+ Args:
65
+ value: The Python object to serialize.
66
+ integer_mode: Formatting mode for integers ('auto', 'int', 'hex', 'fallback_int', 'fallback_hex', 'error').
67
+ dict_as_tuples: If True, serialize dicts as tuples of key-value tuples rather than structs.
68
+ encoding: The encoding used for strings and bytes (default 'utf-8').
69
+ whitespace: If True, emit formatted whitespace; if False, omit unnecessary whitespace.
70
+
71
+ Returns:
72
+ The ZON formatted string.
73
+ """
74
+ ...
75
+
76
+ def dump(
77
+ value: Any,
78
+ fp: IO[str],
79
+ *,
80
+ integer_mode: Literal["auto", "int", "hex", "fallback_int", "fallback_hex", "error"] = "auto",
81
+ dict_as_tuples: bool = False,
82
+ encoding: str = "utf-8",
83
+ whitespace: bool = True,
84
+ ) -> None:
85
+ """Serialize a Python object as a Zig Object Notation (ZON) formatted stream to a file-like object.
86
+
87
+ Args:
88
+ value: The Python object to serialize.
89
+ fp: File-like object supporting `.write(str)`.
90
+ integer_mode: Formatting mode for integers ('auto', 'int', 'hex', 'fallback_int', 'fallback_hex', 'error').
91
+ dict_as_tuples: If True, serialize dicts as tuples of key-value tuples rather than structs.
92
+ encoding: The encoding used for strings and bytes (default 'utf-8').
93
+ whitespace: If True, emit formatted whitespace; if False, omit unnecessary whitespace.
94
+ """
95
+ ...