Skip to content

Commit

Permalink
tapo-py: Update to pyo3 0.23
Browse files Browse the repository at this point in the history
  • Loading branch information
mihai-dinculescu committed Dec 6, 2024
1 parent 2915079 commit 25fbee3
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 38 deletions.
24 changes: 12 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ members = ["tapo", "tapo-py"]
[workspace.dependencies]
anyhow = "1.0"
chrono = { version = "0.4.34", default-features = false }
pyo3 = { version = "0.22" }
pyo3 = { version = "0.23" }
serde = { version = "1.0" }
serde_json = { version = "1.0" }
tokio = { version = "1.42", default-features = false }
2 changes: 1 addition & 1 deletion tapo-py/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pyo3 = { workspace = true, features = [
"extension-module",
"py-clone",
] }
pyo3-log = { version = "0.11" }
pyo3-log = { version = "0.12" }
serde = { workspace = true }
serde_json = { workspace = true }
tokio = { workspace = true, default-features = false, features = [
Expand Down
16 changes: 8 additions & 8 deletions tapo-py/src/api/hub_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,30 +75,30 @@ impl PyHubHandler {
)?;

let results = Python::with_gil(|py| {
let results = PyList::empty_bound(py);
let results = PyList::empty(py);

for child in children {
match child {
ChildDeviceHubResult::KE100(device) => {
results.append(device.into_py(py))?;
results.append(device.into_pyobject(py)?)?;
}
ChildDeviceHubResult::S200B(device) => {
results.append(device.into_py(py))?;
results.append(device.into_pyobject(py)?)?;
}
ChildDeviceHubResult::T100(device) => {
results.append(device.into_py(py))?;
results.append(device.into_pyobject(py)?)?;
}
ChildDeviceHubResult::T110(device) => {
results.append(device.into_py(py))?;
results.append(device.into_pyobject(py)?)?;
}
ChildDeviceHubResult::T300(device) => {
results.append(device.into_py(py))?;
results.append(device.into_pyobject(py)?)?;
}
ChildDeviceHubResult::T310(device) => {
results.append(device.into_py(py))?;
results.append(device.into_pyobject(py)?)?;
}
ChildDeviceHubResult::T315(device) => {
results.append(device.into_py(py))?;
results.append(device.into_pyobject(py)?)?;
}
_ => {
results.append(py.None())?;
Expand Down
4 changes: 2 additions & 2 deletions tapo-py/src/api/power_strip_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ impl PyPowerStripHandler {
)?;

let results = Python::with_gil(|py| {
let results = PyList::empty_bound(py);
let results = PyList::empty(py);

for child in children {
results.append(child.into_py(py))?;
results.append(child.into_pyobject(py)?)?;
}

Ok(results.into())
Expand Down
6 changes: 3 additions & 3 deletions tapo-py/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ fn tapo_py(py: Python, module: &Bound<'_, PyModule>) -> PyResult<()> {
.install()
.expect("Failed to install the logger");

let requests = PyModule::new_bound(py, "tapo.requests")?;
let responses = PyModule::new_bound(py, "tapo.responses")?;
let requests = PyModule::new(py, "tapo.requests")?;
let responses = PyModule::new(py, "tapo.responses")?;

register_handlers(module)?;
register_requests(&requests)?;
Expand All @@ -55,7 +55,7 @@ fn tapo_py(py: Python, module: &Bound<'_, PyModule>) -> PyResult<()> {
module.add_submodule(&requests)?;
module.add_submodule(&responses)?;

let sys = py.import_bound("sys")?;
let sys = py.import("sys")?;
let modules = sys.getattr("modules")?;
modules.set_item("tapo.requests", requests)?;
modules.set_item("tapo.responses", responses)?;
Expand Down
22 changes: 11 additions & 11 deletions tapo/src/python.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
//! Python utilities.
use pyo3::types::{PyDict, PyDictMethods, PyList, PyListMethods};
use pyo3::{Py, PyResult, Python, ToPyObject};
use pyo3::{IntoPyObjectExt, Py, PyResult, Python};
use serde_json::Value;

/// Converts a serde object to a Python dictionary.
pub fn serde_object_to_py_dict(py: Python, value: &Value) -> PyResult<Py<PyDict>> {
let dict = PyDict::new_bound(py);
let dict = PyDict::new(py);

if let Some(object) = value.as_object() {
for (key, value) in object {
Expand All @@ -18,28 +18,28 @@ pub fn serde_object_to_py_dict(py: Python, value: &Value) -> PyResult<Py<PyDict>
Ok(dict.into())
}

fn map_value(py: Python, value: &Value) -> PyResult<impl ToPyObject> {
fn map_value<'py>(py: Python<'py>, value: &'py Value) -> PyResult<impl IntoPyObjectExt<'py>> {
let mapped_value = match value {
Value::Object(_) => serde_object_to_py_dict(py, value)?.to_object(py),
Value::Object(_) => serde_object_to_py_dict(py, value)?.into_py_any(py)?,
Value::Array(value) => {
let array = PyList::empty_bound(py);
let array = PyList::empty(py);

for item in value {
let mapped_item = map_value(py, item)?;
array.append(mapped_item)?;
}

array.to_object(py)
array.into_py_any(py)?
}
Value::String(value) => ToPyObject::to_object(value, py),
Value::Bool(value) => ToPyObject::to_object(value, py),
Value::String(value) => IntoPyObjectExt::into_py_any(value, py)?,
Value::Bool(value) => IntoPyObjectExt::into_py_any(value, py)?,
Value::Number(value) => {
if let Some(ref value) = value.as_i64() {
ToPyObject::to_object(value, py)
IntoPyObjectExt::into_py_any(value, py)?
} else if let Some(ref value) = value.as_u64() {
ToPyObject::to_object(value, py)
IntoPyObjectExt::into_py_any(value, py)?
} else if let Some(ref value) = value.as_f64() {
ToPyObject::to_object(value, py)
IntoPyObjectExt::into_py_any(value, py)?
} else {
todo!()
}
Expand Down

0 comments on commit 25fbee3

Please sign in to comment.