Skip to content

Commit 4716933

Browse files
Frandodignifiedquirecclauss
authored
docs: improve python docs and setup
* docs: improve python docs * Update README.python.md Co-authored-by: Christian Clauss <[email protected]> --------- Co-authored-by: Friedel Ziegelmayer <[email protected]> Co-authored-by: Christian Clauss <[email protected]>
1 parent 1be9652 commit 4716933

File tree

3 files changed

+72
-21
lines changed

3 files changed

+72
-21
lines changed

DEVELOPERS.md

Lines changed: 64 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
# Developers
22

3-
- This uses https://mozilla.github.io/uniffi-rs/ for building the interface
3+
`iroh-ffi` uses [`uniffi-rs`](https://mozilla.github.io/uniffi-rs/) for building the different language bindings.
4+
5+
## General
6+
7+
### Translating the iroh API into iroh-ffi bindings
48

5-
## translating the iroh API into iroh ffi bindings
69
Use these general guidelines when translating the rust API featured in the rust
710
`iroh` library with the API we detail in this crate:
811
- `PathBuf` -> `String`
@@ -15,27 +18,78 @@ Use these general guidelines when translating the rust API featured in the rust
1518
- Anything that can be represented as a string, should have a `to_string` and `from_string` method, eg `NamespaceId`, `DocTicket`
1619
- Enums that have enum variants which contain data should look at the `SocketAddr` or `LiveEvent` enums for the expected translation.
1720

18-
## Testing
21+
### Testing
1922

2023
Please include tests when you add new pieces of the API to the ffi bindings
2124

25+
## Languages
26+
27+
### Kotlin
28+
29+
- See `README.kotlin.md` for setup
30+
- Run tests using `./test_kotlin.sh`
31+
2232
### Python
2333

24-
#### Requirements
34+
#### Development setup
2535

2636
- Install [`maturin`](https://www.maturin.rs/installation) for python development and packaging.
37+
- Create and activate a virtual environment
2738
- Install `uniffi-bindgen` with `pip`
28-
- `maturin develop` will build your package
29-
- maturin expects you to use `virtualenv` to manage your virtual environment
39+
- `maturin build` will build a wheel in `targets/wheels`
40+
- `maturin develop` will build the wheel and install into the current virtual env. It expects you to use `virtualenv` to manage your virtual environment.
41+
42+
See below for example commands that do all this.
3043

31-
#### pytest
44+
#### Running the example
3245

33-
We use [`pytest`](https://docs.pytest.org/en/7.1.x/contents.html) to test the python api.
46+
This repo includes a very small, but working example for using the python API, at [`python/main.py`](python/main.py).
3447

35-
Run the tests by using `python -m pytest` in order to correctly include all of the iroh bindings.
48+
To run it with the latest version of iroh published on pypi:
3649

50+
```sh
51+
# Install iroh with pip
52+
pip install iroh
53+
# Run the example
54+
python3 python/main.py --help
55+
```
56+
57+
To run with a locally-built wheel:
3758

38-
#### Building wheels
59+
```sh
60+
# Create and activate a virtual env
61+
virtualenv .
62+
source ./bin/activate
63+
# Install dependencies
64+
pip install uniffi-bindgen
65+
# Build wheel
66+
maturin develop
67+
# Run the example
68+
python3 python/main.py --help
69+
70+
```
71+
72+
#### Testing
73+
74+
We use [`pytest`](https://docs.pytest.org/en/7.1.x/contents.html) to test the python API.
75+
76+
Execute the following commands to prepare and run all tests:
77+
```sh
78+
# Create and activate a virtual env
79+
virtualenv .
80+
source ./bin/activate
81+
# Install dependencies
82+
pip install uniffi-bindgen pytest pytest-asyncio
83+
# Build wheel
84+
maturin develop
85+
# Run tests
86+
python -m pytest
87+
```
88+
89+
When developing the python API, create a test file for each rust module that you create, and test all pieces of the API in that module in the python test file. The file should be named `[MODULENAME]_test.py`.
90+
For example, the `iroh::net` ffi bindings crate should have a corresponding `net_test.py` file.
91+
92+
#### Building portable wheels
3993

4094
Invoking `maturin build` will build a wheel in `target/wheels`. This
4195
will likely only work on your specific platform. To build a portable
@@ -58,11 +112,3 @@ Uniffi translates the rust to python in a systematic way. The biggest discrepanc
58112
- unit enums will have the same names:
59113
`SocketAddrType::V4` in rust will be `SocketAddrType.V4` in python
60114
- methods that return `Result` in rust will potentially throw exceptions on error in python
61-
62-
#### test file
63-
Create a test file for each rust module that you create, and test all pieces of the API in that module in the python test file. The file should be named "[MODULENAME]\_test.py". For example, the `iroh::net` ffi bindings crate should have a corresponding "net\_test.py" file.
64-
65-
### Kotlin
66-
67-
- See `README.kotlin.md` for setup
68-
- Run tests using `./test_kotlin.sh`

README.python.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
# Iroh Python
22

3-
This is the [Iroh](https://github.com/n0-computer/iroh) python api!
3+
This is the [Iroh](https://github.com/n0-computer/iroh) Python API!
44

5-
The api was generated using [uniffi-rs](https://github.com/mozilla/uniffi-rs).
5+
The API was generated using [uniffi-rs](https://github.com/mozilla/uniffi-rs).
66

77
All iroh classes, methods, functions, and enums contain docstrings.
88

9-
The current best way to understand how the iroh python api can be used is to check out our [tests](https://github.com/n0-computer/iroh-ffi/tree/main/python).
9+
The current best way to understand how the iroh python API can be used is to check out our
10+
[example](https://github.com/n0-computer/iroh-ffi/blob/main/python/main.py) or [tests](https://github.com/n0-computer/iroh-ffi/tree/main/python).
1011

1112
We currently ship binary wheels on pypi for:
1213
- amd64 win
1314
- x86_64 manylinux2014
1415
- aarch64 manylinux2014
1516
- arm64 macosx
1617
- x86_64 macosx
18+
19+
Notes on how the wheels are built and tested are in our [FFI development docs](https://github.com/n0-computer/iroh-ffi/blob/main/DEVELOPERS.md#python).

pytest.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[pytest]
2+
addopts = --ignore=./lib/

0 commit comments

Comments
 (0)