sltcodec 0.2.0__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.
@@ -0,0 +1,43 @@
1
+ name: Publish to PyPI
2
+
3
+ on:
4
+ workflow_dispatch:
5
+
6
+ permissions:
7
+ contents: write
8
+
9
+ jobs:
10
+ build-and-publish:
11
+ runs-on: ubuntu-latest
12
+
13
+ steps:
14
+ - uses: actions/checkout@v4
15
+
16
+ - name: Extract version from pyproject.toml
17
+ id: version
18
+ run: |
19
+ VERSION=$(grep -m1 '^version = "' pyproject.toml | sed -E 's/^version = "([^"]+)"$/\1/')
20
+ echo "version=$VERSION" >> "$GITHUB_OUTPUT"
21
+ echo "tag=v$VERSION" >> "$GITHUB_OUTPUT"
22
+
23
+ - name: Install uv
24
+ uses: astral-sh/setup-uv@v3
25
+
26
+ - name: Build distribution
27
+ run: uv build
28
+
29
+ - name: Publish to PyPI
30
+ uses: pypa/gh-action-pypi-publish@release/v1
31
+ with:
32
+ password: ${{ secrets.PYPI_API_TOKEN }}
33
+ packages-dir: dist
34
+
35
+ - name: Create GitHub release and upload artifacts
36
+ uses: softprops/action-gh-release@v2
37
+ with:
38
+ tag_name: ${{ steps.version.outputs.tag }}
39
+ name: ${{ steps.version.outputs.tag }}
40
+ generate_release_notes: true
41
+ files: |
42
+ dist/*.tar.gz
43
+ dist/*.whl
@@ -0,0 +1,24 @@
1
+ name: Publish to TestPyPI
2
+
3
+ on:
4
+ workflow_dispatch:
5
+
6
+ jobs:
7
+ build-and-publish:
8
+ runs-on: ubuntu-latest
9
+
10
+ steps:
11
+ - uses: actions/checkout@v4
12
+
13
+ - name: Install uv
14
+ uses: astral-sh/setup-uv@v3
15
+
16
+ - name: Build wheel
17
+ run: uv build
18
+
19
+ - name: Publish to TestPyPI
20
+ uses: pypa/gh-action-pypi-publish@release/v1
21
+ with:
22
+ password: ${{ secrets.TEST_PYPI_API_TOKEN }}
23
+ repository-url: https://test.pypi.org/legacy/
24
+ packages-dir: dist
@@ -0,0 +1,218 @@
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[codz]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ share/python-wheels/
24
+ *.egg-info/
25
+ .installed.cfg
26
+ *.egg
27
+ MANIFEST
28
+
29
+ # PyInstaller
30
+ # Usually these files are written by a python script from a template
31
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
32
+ *.manifest
33
+ *.spec
34
+
35
+ # Installer logs
36
+ pip-log.txt
37
+ pip-delete-this-directory.txt
38
+
39
+ # Unit test / coverage reports
40
+ htmlcov/
41
+ .tox/
42
+ .nox/
43
+ .coverage
44
+ .coverage.*
45
+ .cache
46
+ nosetests.xml
47
+ coverage.xml
48
+ *.cover
49
+ *.py.cover
50
+ .hypothesis/
51
+ .pytest_cache/
52
+ cover/
53
+
54
+ # Translations
55
+ *.mo
56
+ *.pot
57
+
58
+ # Django stuff:
59
+ *.log
60
+ local_settings.py
61
+ db.sqlite3
62
+ db.sqlite3-journal
63
+
64
+ # Flask stuff:
65
+ instance/
66
+ .webassets-cache
67
+
68
+ # Scrapy stuff:
69
+ .scrapy
70
+
71
+ # Sphinx documentation
72
+ docs/_build/
73
+
74
+ # PyBuilder
75
+ .pybuilder/
76
+ target/
77
+
78
+ # Jupyter Notebook
79
+ .ipynb_checkpoints
80
+
81
+ # IPython
82
+ profile_default/
83
+ ipython_config.py
84
+
85
+ # pyenv
86
+ # For a library or package, you might want to ignore these files since the code is
87
+ # intended to run in multiple environments; otherwise, check them in:
88
+ # .python-version
89
+
90
+ # pipenv
91
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
93
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
94
+ # install all needed dependencies.
95
+ # Pipfile.lock
96
+
97
+ # UV
98
+ # Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
99
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
100
+ # commonly ignored for libraries.
101
+ # uv.lock
102
+
103
+ # poetry
104
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
105
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
106
+ # commonly ignored for libraries.
107
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
108
+ # poetry.lock
109
+ # poetry.toml
110
+
111
+ # pdm
112
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
113
+ # pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
114
+ # https://pdm-project.org/en/latest/usage/project/#working-with-version-control
115
+ # pdm.lock
116
+ # pdm.toml
117
+ .pdm-python
118
+ .pdm-build/
119
+
120
+ # pixi
121
+ # Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
122
+ # pixi.lock
123
+ # Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
124
+ # in the .venv directory. It is recommended not to include this directory in version control.
125
+ .pixi
126
+
127
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
128
+ __pypackages__/
129
+
130
+ # Celery stuff
131
+ celerybeat-schedule
132
+ celerybeat.pid
133
+
134
+ # Redis
135
+ *.rdb
136
+ *.aof
137
+ *.pid
138
+
139
+ # RabbitMQ
140
+ mnesia/
141
+ rabbitmq/
142
+ rabbitmq-data/
143
+
144
+ # ActiveMQ
145
+ activemq-data/
146
+
147
+ # SageMath parsed files
148
+ *.sage.py
149
+
150
+ # Environments
151
+ .env
152
+ .envrc
153
+ .venv
154
+ env/
155
+ venv/
156
+ ENV/
157
+ env.bak/
158
+ venv.bak/
159
+
160
+ # Spyder project settings
161
+ .spyderproject
162
+ .spyproject
163
+
164
+ # Rope project settings
165
+ .ropeproject
166
+
167
+ # mkdocs documentation
168
+ /site
169
+
170
+ # mypy
171
+ .mypy_cache/
172
+ .dmypy.json
173
+ dmypy.json
174
+
175
+ # Pyre type checker
176
+ .pyre/
177
+
178
+ # pytype static type analyzer
179
+ .pytype/
180
+
181
+ # Cython debug symbols
182
+ cython_debug/
183
+
184
+ # PyCharm
185
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
186
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
187
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
188
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
189
+ # .idea/
190
+
191
+ # Abstra
192
+ # Abstra is an AI-powered process automation framework.
193
+ # Ignore directories containing user credentials, local state, and settings.
194
+ # Learn more at https://abstra.io/docs
195
+ .abstra/
196
+
197
+ # Visual Studio Code
198
+ # Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
199
+ # that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
200
+ # and can be added to the global gitignore or merged into this file. However, if you prefer,
201
+ # you could uncomment the following to ignore the entire vscode folder
202
+ # .vscode/
203
+ # Temporary file for partial code execution
204
+ tempCodeRunnerFile.py
205
+
206
+ # Ruff stuff:
207
+ .ruff_cache/
208
+
209
+ # PyPI configuration file
210
+ .pypirc
211
+
212
+ # Marimo
213
+ marimo/_static/
214
+ marimo/_lsp/
215
+ __marimo__/
216
+
217
+ # Streamlit
218
+ .streamlit/secrets.toml
@@ -0,0 +1 @@
1
+ 3.14
@@ -0,0 +1,24 @@
1
+ {
2
+ "version": "0.2.0",
3
+ "configurations": [
4
+ {
5
+ "name": "Debug Pytest (single test)",
6
+ "type": "python",
7
+ "request": "launch",
8
+ "module": "pytest",
9
+ "args": [
10
+ "-k",
11
+ "${input:test_name}"
12
+ ],
13
+ "console": "integratedTerminal"
14
+ }
15
+ ],
16
+ "inputs": [
17
+ {
18
+ "id": "test_name",
19
+ "type": "promptString",
20
+ "description": "Enter pytest -k expression",
21
+ "default": "test_encode_and_decode_round_trip"
22
+ }
23
+ ]
24
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "python.defaultInterpreterPath": "${workspaceFolder}/.venv/Scripts/python.exe"
3
+ }
sltcodec-0.2.0/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 fangface
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,233 @@
1
+ Metadata-Version: 2.4
2
+ Name: sltcodec
3
+ Version: 0.2.0
4
+ Summary: Decode and encode bytearrays according to struct layout definitions using sltcore.
5
+ Project-URL: Homepage, https://github.com/fangface-hub/StructLayoutToolkitCodec
6
+ Project-URL: Documentation, https://readthedocs.org
7
+ Project-URL: Repository, https://github.com/fangface-hub/StructLayoutToolkitCodec
8
+ Project-URL: Issues, https://github.com/fangface-hub/StructLayoutToolkitCodec/issues
9
+ Author: fangface
10
+ License-Expression: MIT
11
+ License-File: LICENSE
12
+ Keywords: byte,deserialization,layout,serialization,struct
13
+ Requires-Python: >=3.12
14
+ Requires-Dist: sltcalc>=0.1.0
15
+ Requires-Dist: sltcore>=1.3.0
16
+ Description-Content-Type: text/markdown
17
+
18
+ # StructLayoutToolkitCodec
19
+
20
+ sltcodec is a small package for decoding and encoding bytearrays according to structured layout definitions. It uses sltcore for bit-level access and provides a simple API for turning a layout definition into binary data and back again.
21
+
22
+ ## Installation
23
+
24
+ ```bash
25
+ pip install sltcodec
26
+ ```
27
+
28
+ ## Quick Example
29
+
30
+ `FieldDef.description` is an optional human-readable note that can be attached to each field definition.
31
+
32
+ `decode` returns a `StructInstance`.
33
+ `encode` accepts a `StructInstance` and a destination `bytearray`.
34
+
35
+ ```python
36
+ from sltcore import InfoSize
37
+ from sltcodec import (FieldDef, FieldInstance, StructDef, StructInstance,
38
+ decode, encode)
39
+
40
+ struct_def = [
41
+ FieldDef(name="flag",
42
+ offset=InfoSize(0, 0),
43
+ size=InfoSize(0, 1),
44
+ type="bool",
45
+ scale=1.0,
46
+ description="Whether the feature is enabled"),
47
+ FieldDef(name="value",
48
+ offset=InfoSize(0, 1),
49
+ size=InfoSize(1, 0),
50
+ type="unsigned int",
51
+ scale=1.0,
52
+ description="The encoded numeric payload"),
53
+ ]
54
+
55
+ encoded = encode(
56
+ StructInstance(
57
+ struct_def=StructDef(fields=struct_def),
58
+ field_instances=[
59
+ FieldInstance(struct_def[0], True),
60
+ FieldInstance(struct_def[1], 0xA5),
61
+ ],
62
+ ),
63
+ bytearray(),
64
+ )
65
+ decoded = decode(struct_def, encoded)
66
+
67
+ print(encoded)
68
+ print(decoded)
69
+ print(decoded.field_instances)
70
+ ```
71
+
72
+ Output:
73
+
74
+ ```python
75
+ bytearray(b"\xd2\x80")
76
+ StructInstance(struct_def=StructDef(...), field_instances=[...])
77
+ [FieldInstance(field_def=FieldDef(name='flag', offset=InfoSize(byte=0, bit=0), size=InfoSize(byte=0, bit=1), type='bool', scale=1.0, repeat=None), value=True),
78
+ FieldInstance(field_def=FieldDef(name='value', offset=InfoSize(byte=0, bit=1), size=InfoSize(byte=1, bit=0), type='unsigned int', scale=1.0, repeat=None), value=165)]
79
+
80
+ ```
81
+
82
+ ## Nested Field Types
83
+
84
+ `FieldDef.type` can be a `StructDef`, not only a primitive type name.
85
+ This allows you to define an inline nested structure and keep `encode_field` / `decode_field` behavior symmetrical.
86
+
87
+ ```python
88
+ from sltcore import InfoSize
89
+ from sltcodec import FieldDef, FieldInstance, StructDef, StructInstance, decode, encode
90
+
91
+ child_struct_def = [
92
+ FieldDef(name="left", offset=InfoSize(0, 0), size=InfoSize(1, 0), type="unsigned int"),
93
+ FieldDef(name="right", offset=InfoSize(1, 0), size=InfoSize(1, 0), type="unsigned int"),
94
+ ]
95
+
96
+ parent_field_def = FieldDef(
97
+ name="pair",
98
+ offset=InfoSize(0, 0),
99
+ size=InfoSize(2, 0),
100
+ type=StructDef(
101
+ name="Pair",
102
+ description="Two adjacent unsigned ints",
103
+ fields=child_struct_def,
104
+ ),
105
+ )
106
+
107
+ encoded = encode(
108
+ StructInstance(
109
+ struct_def=StructDef(fields=[parent_field_def]),
110
+ field_instances=[
111
+ FieldInstance(
112
+ parent_field_def,
113
+ StructInstance(
114
+ struct_def=StructDef(fields=child_struct_def),
115
+ field_instances=[
116
+ FieldInstance(child_struct_def[0], 3),
117
+ FieldInstance(child_struct_def[1], 4),
118
+ ],
119
+ ),
120
+ )
121
+ ],
122
+ ),
123
+ bytearray(),
124
+ )
125
+
126
+ decoded = decode([parent_field_def], encoded)
127
+ print(encoded)
128
+ print(decoded)
129
+ print(decoded.field_instances)
130
+ ```
131
+
132
+ Output:
133
+
134
+ ```python
135
+ bytearray(b"\x03\x04")
136
+ StructInstance(struct_def=StructDef(...), field_instances=[...])
137
+ [FieldInstance(field_def=FieldDef(name='pair', offset=InfoSize(byte=0, bit=0), size=InfoSize(byte=2, bit=0), type=[...], scale=1.0, repeat=None),
138
+ value=StructInstance(struct_def=StructDef(...), field_instances=[...]))]
139
+ ```
140
+
141
+ ## Expression-Based Type Selection
142
+
143
+ `FieldDef.type` can also be an expression string.
144
+ The expression is evaluated with previously processed field values in scope, so you can switch decoding/encoding type dynamically.
145
+
146
+ ```python
147
+ from sltcore import InfoSize
148
+ from sltcodec import (FieldDef, FieldInstance, StructDef, StructInstance,
149
+ decode, encode)
150
+
151
+ struct_def = [
152
+ FieldDef(name="kind", offset=InfoSize(0, 0), size=InfoSize(1, 0), type="unsigned int"),
153
+ FieldDef(
154
+ name="payload",
155
+ offset=InfoSize(1, 0),
156
+ size="{1: 1, 2: 4}[kind]",
157
+ type="{1: 'int', 2: 'float'}[kind]",
158
+ ),
159
+ ]
160
+
161
+ # kind=1 -> payload is int (1 byte)
162
+ encoded_int = encode(
163
+ StructInstance(
164
+ struct_def=StructDef(fields=struct_def),
165
+ field_instances=[
166
+ FieldInstance(struct_def[0], 1),
167
+ FieldInstance(struct_def[1], 7),
168
+ ],
169
+ ),
170
+ bytearray(),
171
+ )
172
+ decoded_int = decode(struct_def, encoded_int)
173
+
174
+ # kind=2 -> payload is float (4 bytes)
175
+ encoded_float = encode(
176
+ StructInstance(
177
+ struct_def=StructDef(fields=struct_def),
178
+ field_instances=[
179
+ FieldInstance(struct_def[0], 2),
180
+ FieldInstance(struct_def[1], 1.5),
181
+ ],
182
+ ),
183
+ bytearray(),
184
+ )
185
+ decoded_float = decode(struct_def, encoded_float)
186
+
187
+ print(encoded_int, decoded_int)
188
+ print(encoded_float, decoded_float)
189
+ ```
190
+
191
+ ## Saving And Loading StructDef Dictionaries
192
+
193
+ You can persist reusable structure definitions by name with
194
+ `save_struct_def_dict` / `load_struct_def_dict`.
195
+
196
+ ```python
197
+ from pathlib import Path
198
+
199
+ from sltcore import InfoSize
200
+ from sltcodec import FieldDef, StructDef, load_struct_def_dict, save_struct_def_dict
201
+
202
+ struct_defs = {
203
+ "Header": StructDef(
204
+ name="Header",
205
+ description="Simple header",
206
+ fields=[
207
+ FieldDef(name="kind", offset=InfoSize(0, 0), size=InfoSize(1, 0), type="unsigned int"),
208
+ FieldDef(name="flags", offset=InfoSize(1, 0), size=InfoSize(1, 0), type="unsigned int"),
209
+ ],
210
+ )
211
+ }
212
+
213
+ path = Path("struct_defs.json")
214
+ save_struct_def_dict(path, struct_defs)
215
+ loaded = load_struct_def_dict(path)
216
+
217
+ print(loaded["Header"].name)
218
+ print(loaded["Header"].description)
219
+ ```
220
+
221
+ Example output:
222
+
223
+ ```python
224
+ bytearray(b"\x01\x07") StructInstance(struct_def=StructDef(...), field_instances=[...])
225
+ bytearray(b"\x02?\xc0\x00\x00") StructInstance(struct_def=StructDef(...), field_instances=[...])
226
+ ```
227
+
228
+ ## Development
229
+
230
+ ```bash
231
+ uv sync --all-extras
232
+ uv run pytest
233
+ ```