Skip to content

Commit 700cd02

Browse files
committed
add python api
1 parent ccbb3b9 commit 700cd02

File tree

8 files changed

+208
-1
lines changed

8 files changed

+208
-1
lines changed

.github/workflows/test.yml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,43 @@ jobs:
3434
- name: Run cargo nextest
3535
run: |
3636
cargo nextest run --profile ci --all-features
37+
38+
test-maturin-linux:
39+
runs-on: ubuntu-latest
40+
41+
steps:
42+
- name: Check out repository code
43+
uses: actions/checkout@v4
44+
with:
45+
fetch-depth: 2
46+
47+
- name: Set up Python
48+
uses: actions/setup-python@v5
49+
with:
50+
python-version: '3.10'
51+
52+
- name: Set up Rust
53+
run: rustup show
54+
55+
- name: Install Maturin
56+
run: pip install maturin
57+
58+
- name: Create and activate virtualenv
59+
working-directory: "ziplinter-python"
60+
run: |
61+
python -m venv .venv
62+
source .venv/bin/activate
63+
pip install -U pip maturin
64+
pip freeze
65+
66+
- name: Build the Python module
67+
working-directory: "ziplinter-python"
68+
run: |
69+
source .venv/bin/activate
70+
maturin develop
71+
72+
- name: Run test script
73+
working-directory: "ziplinter-python"
74+
run: |
75+
source .venv/bin/activate
76+
python test.py

Cargo.lock

Lines changed: 110 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[workspace]
22
resolver = "2"
3-
members = ["rc-zip-cli", "rc-zip", "rc-zip-sync", "rc-zip-tokio", "ziplinter"]
3+
members = ["rc-zip-cli", "rc-zip", "rc-zip-sync", "rc-zip-tokio", "ziplinter", "ziplinter-python"]
44
exclude = ["fuzz"]
55

66
# The profile that 'cargo dist' will build with

ziplinter-python/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.venv

ziplinter-python/Cargo.toml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[package]
2+
name = "ziplinter-python"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
[lib]
7+
name = "ziplinter"
8+
crate-type = ["cdylib"]
9+
10+
[dependencies]
11+
serde_json = "1.0.137"
12+
pythonize = "0.24.0"
13+
ziplinter = { version = "0.1.0", path = "../ziplinter" }
14+
15+
[dependencies.pyo3]
16+
version = "0.24.0"
17+
# "abi3-py38" tells pyo3 (and maturin) to build using the stable ABI with minimum Python version 3.8
18+
features = ["abi3-py38"]

ziplinter-python/pyproject.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[build-system]
2+
requires = ["maturin>=1.0,<2.0"]
3+
build-backend = "maturin"
4+
5+
[tool.maturin]
6+
# "extension-module" tells pyo3 we want to build an extension module (skips linking against libpython.so)
7+
features = ["pyo3/extension-module"]

ziplinter-python/src/lib.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use pyo3::prelude::*;
2+
use pythonize::pythonize;
3+
4+
#[pyfunction]
5+
fn parse_file(py: Python, path: String) -> PyResult<pyo3::Bound<'_, PyAny>> {
6+
let file = std::fs::File::open(&path)?;
7+
let value = ::ziplinter::parse_file(&file);
8+
9+
Ok(pythonize(py, &value)?)
10+
}
11+
12+
#[pyfunction]
13+
fn parse_bytes(py: Python, bytes: Vec<u8>) -> PyResult<pyo3::Bound<'_, PyAny>> {
14+
let value = ::ziplinter::parse_bytes(&bytes);
15+
Ok(pythonize(py, &value)?)
16+
}
17+
18+
#[pymodule]
19+
fn ziplinter(m: &Bound<'_, PyModule>) -> PyResult<()> {
20+
m.add_function(wrap_pyfunction!(parse_file, m)?)?;
21+
m.add_function(wrap_pyfunction!(parse_bytes, m)?)?;
22+
23+
Ok(())
24+
}

ziplinter-python/test.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import ziplinter
2+
3+
def main():
4+
print("{}", ziplinter.parse_file("../testdata/unix.zip"))
5+
6+
if __name__ == '__main__':
7+
main()

0 commit comments

Comments
 (0)