Skip to content

Commit 06dfa47

Browse files
authored
(2.5.0) Stub files and various improvements
Merge pull request #32 from MatrixEditor/dev/2.5.0-rc
2 parents e527a77 + a10fff4 commit 06dfa47

108 files changed

Lines changed: 6969 additions & 2505 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/python-test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
fail-fast: true
1010
matrix:
1111
os: ["ubuntu-latest", "windows-latest", "macos-latest"]
12-
python-version: ["3.12"]
12+
python-version: ["3.12", "3.13"]
1313

1414
steps:
1515
- name: Checkout source

CHANGELOG.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Changelog
2+
3+
## [2.5.0] - Minor Release
4+
5+
### Added
6+
- **caterpillar.abc**
7+
- Add `_SupportsBits` protocol.
8+
- Add `_ContainsBits` protocol.
9+
- Add `_SupportsType` protocol.
10+
11+
- **caterpillar.shortcuts**
12+
- Add new shortcuts: `typeof`, `to_struct`, `hasstruct`, `getstruct`, and `sizeof`.
13+
14+
- **caterpillar.shared**
15+
- Add new constants from other modules: `ATTR_BYTEORDER`, `ATTR_TYPE`, `ATTR_BITS`, `ATTR_SIGNED`, `ATTR_TEMPLATE`.
16+
17+
- **caterpillar.context**
18+
- Add context attribute `_root` to point to the root context instance. Internal parent iteration now uses `self.get(...)`.
19+
20+
---
21+
22+
### Removed
23+
- **caterpillar.abc**
24+
- Remove `_Action` protocol; split into `_ActionLike = _SupportsActionUnpack | _SupportsActionPack`.
25+
- Remove `__type__` requirement from `_StructLike`.
26+
- **Breaking:** Remove `_EnumLike` and `_ContextPathStr`.
27+
28+
- **caterpillar.model**
29+
- Remove unused `getformat` function.
30+
31+
- **caterpillar.fields.common**
32+
- Remove unused `__fmt__` function in `Transformer`.
33+
34+
---
35+
36+
### Changed
37+
- **caterpillar.abc**
38+
- Rename `_Switch` protocol to `_SwitchLike`.
39+
- Move `STRUCT_FIELD` to `caterpillar.shared` as `ATTR_STRUCT`; move `hasstruct`, `getstruct`, and `typeof` to `caterpillar.shared`.
40+
41+
- **caterpillar.byteorder**
42+
- Move `BYTEORDER_FIELD` to `caterpillar.shared` as `ATTR_BYTEORDER`.
43+
44+
- **caterpillar.model**
45+
- Update `sizeof` to check if the object implements `_SupportsSize` protocol.
46+
- Renew `Bitfield` concept with enhanced syntax.
47+
48+
- **Documentation**
49+
- Update reference and library docs; improve section numbering.
50+
51+
---
52+
53+
### Fixed
54+
- **caterpillar.model**
55+
- Fix parsing of union objects with an unbound stream.
56+
- Fix field options in Sequences and Structs not being populated when creating fields.

MANIFEST.in

Lines changed: 0 additions & 9 deletions
This file was deleted.

README.md

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Caterpillar - 🐛
22

3-
[![python](https://img.shields.io/python/required-version-toml?tomlFilePath=https%3A%2F%2Fraw.githubusercontent.com%2FMatrixEditor%2Fcaterpillar%2Fmaster%2Fpyproject.toml&logo=python)](https://www.python.org/downloads/)
3+
[![python](https://img.shields.io/badge/Python-3.12+-blue?logo=python&logoColor=yellow)](https://www.python.org/downloads/)
4+
![![Latest Version](https://pypi.org/project/caterpillar-py/)](https://img.shields.io/github/v/release/MatrixEditor/caterpillar.svg?logo=github&label=Latest+Version)
45
[![Build and Deploy Docs](https://github.com/MatrixEditor/caterpillar/actions/workflows/python-sphinx.yml/badge.svg)](https://github.com/MatrixEditor/caterpillar/actions/workflows/python-sphinx.yml)
56
[![Run Tests](https://github.com/MatrixEditor/caterpillar/actions/workflows/python-test.yml/badge.svg)](https://github.com/MatrixEditor/caterpillar/actions/workflows/python-test.yml)
67
![GitHub issues](https://img.shields.io/github/issues/MatrixEditor/caterpillar?logo=github)
@@ -10,15 +11,8 @@
1011

1112
> [!WARNING]
1213
> This project is still in beta/testing phase. Expect bugs, naming changes and errors while using this
13-
> library. C API Reference is WIP, C extensions are supported since v2.1.0.
14-
15-
> [!NOTE]
16-
> Python 3.14 breaks `with` statements in class definitions since `__annotations__` are added at the end
17-
> of a class definition. Therefore, `Digest` and conditional statements **ARE NOT SUPPORTED** in using the `with` syntax Python 3.14+.
18-
> As of version `2.4.5` the `Digest` class has a counterpart (`DigestField`), which can be used to manually specify a digest without
19-
> the need of a `ẁith` statement.
20-
21-
14+
> library. C API Reference is WIP, C extensions are supported since v2.1.0. The latest stable release
15+
> is available at PyPI.
2216
2317
Caterpillar is a Python 3.12+ library to pack and unpack structurized binary data. It
2418
enhances the capabilities of [Python Struct](https://docs.python.org/3/library/struct.html)
@@ -27,7 +21,7 @@ options will be added in the future. Documentation is [here >](https://matrixedi
2721

2822
*Caterpillar* is able to:
2923

30-
* Pack and unpack data just from processing Python class definitions (including support for bitfields, c++-like templates and c-like unions!),
24+
* Pack and unpack data just from processing Python class definitions (including support for powerful bitfields, c++-like templates and c-like unions!),
3125
* apply a wide range of data types (with endianess and architecture configuration),
3226
* dynamically adapt structs based on their inheritance layout,
3327
* reduce the used memory space using `__slots__`,
@@ -36,36 +30,52 @@ options will be added in the future. Documentation is [here >](https://matrixedi
3630
* it helps you to create cleaner and more compact code.
3731
* You can even extend Caterpillar and write your parsing logic in C or C++!!
3832

33+
> [!NOTE]
34+
> Python 3.14 breaks `with` statements in class definitions since `__annotations__` are added at the end
35+
> of a class definition. Therefore, `Digest` and conditional statements **ARE NOT SUPPORTED** using the `with` syntax in Python 3.14+.
36+
> As of version `2.4.5` the `Digest` class has a counterpart (`DigestField`), which can be used to manually specify a digest without
37+
> the need of a `ẁith` statement.
38+
39+
3940
## Give me some code!
4041

4142
```python
42-
from caterpillar.py import *
43+
@bitfield(order=LittleEndian)
44+
class Header:
45+
version : 4 # 4bit integer
46+
valid : 1 # 1bit flag (boolean)
47+
ident : (8, CharFactory) # 8bit char
48+
# automatic alignment to 16bits
4349

4450
@struct(order=LittleEndian)
4551
class Format:
46-
magic: b"ITS MAGIC" # Supports string and byte constants directly
47-
a: uint8 # Primitive data types
48-
b: int32
49-
length: uint8 # String fields with computed lengths
50-
name: String(this.length) # -> you can also use Prefixed(uint8)
51-
52-
# wraps all following fields and creates a new attr
53-
# only for Python <= 3.13
54-
with Md5(name="hash", verify=True):
55-
# Sequences with prefixed, computed lengths
56-
names: CString[uint8::]
52+
magic : b"ITS MAGIC" # Supports string and byte constants directly
53+
header : Header
54+
a : uint8 # Primitive data types
55+
b : int32
56+
length : uint8 # String fields with computed lengths
57+
name : String(this.length) # -> you can also use Prefixed(uint8)
5758

59+
_hash_begin : DigestField.begin("hash", Md5_Algo) # custom actions, e.g. for hashes
60+
names : CString[uint8::] # Sequences with prefixed, computed lengths
61+
hash : Md5_Field("hash", verify=True) = None # automatic hash creation and verification + default value
5862

5963
# Instantiation (keyword-only arguments, magic is auto-inferred):
60-
obj = Format(a=1, b=2, length=3, name="foo", names=["a", "b"])
64+
obj = Format(
65+
header=Header(version=2, valid=True, ident="F"),
66+
a=1,
67+
b=2,
68+
length=3,
69+
name="foo",
70+
names=["a", "b"]
71+
)
6172
# Packing the object, reads as 'PACK obj FROM Format'
6273
# objects of struct classes can be packed right away
6374
blob = pack(obj, Format)
64-
# results in: b'ITS MAGIC\x01\x02\x00\x00\x00\x03foo\x02a\x00b\x00\xf55...
75+
# results in: b'ITS MAGIC0*\x01\x02\x00\x00\x00\x03foo\x02a\x00b\x00)\x9a...'
6576

6677
# Unpacking the binary data, reads as 'UNPACK Format FROM blob'
6778
obj2 = unpack(Format, blob)
68-
assert obj2.hash == obj.hash
6979
```
7080

7181
This library offers extensive functionality beyond basic struct handling. For further details
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
table.t-stbl {
2+
padding: 0em 1em 0.5em 1em;
3+
flex-direction: column;
4+
display: flex;
5+
}
6+
7+
.t-stbl td.t-hr {
8+
border-top: 1px solid var(--pst-color-surface);
9+
padding: 0
10+
}
11+
12+
.t-stbl span.t-decl-id {
13+
color: var(--pst-color-on-surface);
14+
font-style: italic;
15+
}
16+
17+
.t-stbl span.t-decl-opt {
18+
color: var(--pst-color-primary);
19+
font-style: italic;
20+
padding-left: 0;
21+
font-size: small;
22+
}
23+
24+
.t-stbl td.t-no {
25+
padding-left: 2em;
26+
}
27+
28+
table.t-par-begin {
29+
flex-direction: column;
30+
display: flex;
31+
}
32+
33+
tr.t-bar {
34+
vertical-align: top;
35+
}
36+
37+
tr.t-par>td {
38+
vertical-align: top;
39+
padding: 0 1em 0 0;
40+
}
41+
42+
tr.t-par>td:nth-child(1) {
43+
white-space: nowrap;
44+
text-align: right;
45+
font-weight: bold;
46+
font-family: var(--pst-font-family-monospace-system);
47+
}
48+
49+
tr.t-par>td:nth-child(3) {
50+
width: 100%
51+
}

docs/sphinx/source/conf.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"sphinx_design",
3030
"breathe",
3131
"c_annotations",
32-
"sphinx_copybutton"
32+
"sphinx_copybutton",
3333
]
3434

3535
templates_path = ["_templates"]
@@ -63,13 +63,14 @@
6363
# If true, `todo` and `todoList` produce output, else they produce nothing.
6464
todo_include_todos = False
6565

66-
refcount_file = '../../../src/capi.dat'
67-
autodoc_member_order = 'bysource'
66+
refcount_file = "../../../src/capi.dat"
67+
autodoc_member_order = "bysource"
6868

6969
# -- Options for HTML output -------------------------------------------------
7070
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output
7171
html_theme = "pydata_sphinx_theme"
7272
html_static_path = ["_static"]
73+
html_css_files = ["css/custom.css"]
7374
html_theme_options = {
7475
"show_nav_level": 4,
7576
"navbar_end": ["navbar-icon-links", "theme-switcher"],
@@ -78,10 +79,9 @@
7879
"logo": {
7980
"text": f"Caterpillar {version}",
8081
},
81-
"announcement": "https://raw.githubusercontent.com/MatrixEditor/caterpillar/master/docs/sphinx/source/_templates/announcement.html",
8282
}
8383
html_sidebars = {
84-
"installing/index": [],
84+
"installing/index": [],
8585
}
8686

8787
# -- Options for C++ Docs -----------------------------------------------------

docs/sphinx/source/development/changelog.rst

Lines changed: 106 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,109 @@
44
Changelog
55
*********
66

7-
*Entries will be added in the future.*
7+
*More entries will be added in the future.*
8+
9+
.. _changelog_2.5.0:
10+
11+
[2.5.0] - Minor Release
12+
=======================
13+
14+
This version introduces massive changes due to the addition of stub files. Most of the type hints in the Python
15+
file are ported into several stub files. Additionally, the bitfield concept was completely renewed to be more
16+
flexible and dynamic.
17+
18+
Added
19+
-----
20+
21+
*caterpillar.abc*
22+
^^^^^^^^^^^^^^^^^
23+
24+
- :class:`_SupportsBits` protocol
25+
- :class:`_ContainsBits` protocol
26+
- :class:`_SupportsType` protocol
27+
28+
*caterpillar.shortcuts*
29+
^^^^^^^^^^^^^^^^^^^^^^^
30+
31+
- New shortcuts: :func:`typeof`, :func:`to_struct`, :func:`hasstruct`, :func:`getstruct` and :func:`sizeof`
32+
33+
*caterpillar.shared*
34+
^^^^^^^^^^^^^^^^^^^^
35+
36+
- New constants from other modules: :attr:`ATTR_BYTEORDER`, :attr:`ATTR_TYPE`, :attr:`ATTR_BITS`, :attr:`ATTR_SIGNED`, :attr:`ATTR_TEMPLATE`
37+
38+
*caterpillar.context*
39+
^^^^^^^^^^^^^^^^^^^^^
40+
41+
- New context attribute: `_root` can be set to point to the root context instance. Internally, instead of a for-loop that iterates through parent context instances, a simple :code:`self.get(...)` call is made.
42+
43+
.. raw:: html
44+
45+
<hr>
46+
47+
Removed
48+
-------
49+
50+
*caterpillar.abc*
51+
^^^^^^^^^^^^^^^^^
52+
53+
- ``_Action`` protocol and create two separate Protocols that form::
54+
55+
_ActionLike = _SupportsActionUnpack | _SupportsActionPack
56+
57+
- ``__type__`` requirement from :class:`_StructLike`
58+
- **Breaking:** ``_EnumLike``, ``_ContextPathStr``
59+
60+
*caterpillar.model*
61+
^^^^^^^^^^^^^^^^^^^
62+
63+
- Unused ``getformat`` function
64+
65+
*caterpillar.fields.common*
66+
^^^^^^^^^^^^^^^^^^^^^^^^^^^
67+
68+
- Unused ``__fmt__`` function in :class:`Transformer`
69+
70+
.. raw:: html
71+
72+
<hr>
73+
74+
Changed
75+
-------
76+
77+
*caterpillar.abc*
78+
^^^^^^^^^^^^^^^^^
79+
80+
- Rename ``_Switch`` protocol to :attr:`_SwitchLike`
81+
- Move the following attributes and methods into *caterpillar.shared*: rename ``STRUCT_FIELD`` to :attr:`ATTR_STRUCT`, :func:`hasstruct`, :func:`getstruct` and :func:`typeof`
82+
83+
*caterpillar.byteorder*
84+
^^^^^^^^^^^^^^^^^^^^^^^
85+
86+
- Move ``BYTEORDER_FIELD`` to *caterpillar.shared* as :attr:`ATTR_BYTEORDER`
87+
88+
89+
*caterpillar.model*
90+
^^^^^^^^^^^^^^^^^^^
91+
92+
- :func:`sizeof` now checks if the provided object implements the :class:`_SupportsSize` protocol
93+
- New :class:`Bitfield` concept with enhanced syntax
94+
95+
96+
*Documentation*
97+
^^^^^^^^^^^^^^^
98+
99+
- Update reference and library docs as well as section numbering
100+
101+
.. raw:: html
102+
103+
<hr>
104+
105+
Fixed
106+
-----
107+
108+
*caterpillar.model*
109+
^^^^^^^^^^^^^^^^^^^
110+
111+
- when parsing union objects with an unbound stream object
112+
- field options defined in Sequences and Structs were not populated when creating fields.

docs/sphinx/source/development/index.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ Development
77
*TODO*
88

99
.. toctree::
10-
:numbered:
1110
:maxdepth: 2
1211

1312
roadmap.rst

0 commit comments

Comments
 (0)