Skip to content

Commit

Permalink
wip: pyo3 stub generation
Browse files Browse the repository at this point in the history
  • Loading branch information
cocool97 committed Jan 29, 2025
1 parent dc909ce commit 0eceb3e
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 1 deletion.
8 changes: 7 additions & 1 deletion pyadb_client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@ readme = "README.md"

[lib]
name = "pyadb_client"
crate-type = ["cdylib"]
crate-type = ["cdylib", "rlib"]

[[bin]]
name = "stub_gen"
doc = false

[dependencies]
anyhow = { version = "1.0.94" }
adb_client = { version = "2.0.6" }
pyo3 = { version = "0.23.4", features = ["extension-module", "anyhow", "abi3-py37"] }
pyo3-stub-gen = "0.7.0"
pyo3-stub-gen-derive = "0.7.0"
3 changes: 3 additions & 0 deletions pyadb_client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ pip install ".[build]"
# Build development package
maturin develop

# Build stub file (.pyi)
cargo run --bin stub_gen

# Build release Python package
maturin build --release
```
3 changes: 3 additions & 0 deletions pyadb_client/src/adb_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ use std::net::SocketAddrV4;
use adb_client::ADBServer;
use anyhow::Result;
use pyo3::{pyclass, pymethods, PyResult};
use pyo3_stub_gen_derive::{gen_stub_pyclass, gen_stub_pymethods};

use crate::{PyADBServerDevice, PyDeviceShort};

#[gen_stub_pyclass]
#[pyclass]
pub struct PyADBServer(ADBServer);

#[gen_stub_pymethods]
#[pymethods]
impl PyADBServer {
#[new]
Expand Down
3 changes: 3 additions & 0 deletions pyadb_client/src/adb_server_device.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use adb_client::{ADBDeviceExt, ADBServerDevice};
use anyhow::Result;
use pyo3::{pyclass, pymethods};
use pyo3_stub_gen_derive::{gen_stub_pyclass, gen_stub_pymethods};
use std::{fs::File, path::PathBuf};

#[gen_stub_pyclass]
#[pyclass]
pub struct PyADBServerDevice(pub ADBServerDevice);

#[gen_stub_pymethods]
#[pymethods]
impl PyADBServerDevice {
#[getter]
Expand Down
6 changes: 6 additions & 0 deletions pyadb_client/src/adb_usb_device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@ use std::{fs::File, path::PathBuf};
use adb_client::{ADBDeviceExt, ADBUSBDevice};
use anyhow::Result;
use pyo3::{pyclass, pymethods};
use pyo3_stub_gen_derive::{gen_stub_pyclass, gen_stub_pymethods};

#[gen_stub_pyclass]
#[pyclass]
/// Represent a device directly reachable over USB.
pub struct PyADBUSBDevice(ADBUSBDevice);

#[gen_stub_pymethods]
#[pymethods]
impl PyADBUSBDevice {
#[staticmethod]
/// Autodetect a device reachable over USB.
/// This method raises an error if multiple devices or none are connected.
pub fn autodetect() -> Result<Self> {
Ok(ADBUSBDevice::autodetect()?.into())
}
Expand Down
9 changes: 9 additions & 0 deletions pyadb_client/src/bin/stub_gen.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use pyo3_stub_gen::{Result, StubInfo};

fn main() -> Result<()> {
// let stub = pyadb_client::stub_info().expect("eee");
let stub = StubInfo::from_pyproject_toml("pyproject.toml").expect("eee");
dbg!(&stub);
stub.generate().expect("cannot generate");
Ok(())
}
3 changes: 3 additions & 0 deletions pyadb_client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ pub use adb_usb_device::*;
pub use models::*;

use pyo3::prelude::*;
use pyo3_stub_gen::define_stub_info_gatherer;

define_stub_info_gatherer!(stub_info);

#[pymodule]
fn pyadb_client(m: &Bound<'_, PyModule>) -> PyResult<()> {
Expand Down
3 changes: 3 additions & 0 deletions pyadb_client/src/models/devices.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use adb_client::DeviceShort;
use pyo3::{pyclass, pymethods};
use pyo3_stub_gen_derive::{gen_stub_pyclass, gen_stub_pymethods};

// Check https://docs.rs/rigetti-pyo3/latest/rigetti_pyo3 to automatically build this code

#[gen_stub_pyclass]
#[pyclass]
pub struct PyDeviceShort(DeviceShort);

#[gen_stub_pymethods]
#[pymethods]
impl PyDeviceShort {
#[getter]
Expand Down

0 comments on commit 0eceb3e

Please sign in to comment.