Browse Source

PoC in Node

tags/v0.3.5
EricLBuehler 1 year ago
parent
commit
5995ac28de
7 changed files with 22 additions and 12 deletions
  1. +1
    -1
      Cargo.lock
  2. +1
    -0
      Cargo.toml
  3. +1
    -0
      apis/python/node/Cargo.toml
  4. +8
    -0
      apis/python/node/src/lib.rs
  5. +0
    -1
      derive/Cargo.toml
  6. +9
    -5
      derive/src/lib.rs
  7. +2
    -5
      derive/tests/functional.rs

+ 1
- 1
Cargo.lock View File

@@ -2144,7 +2144,6 @@ name = "derive"
version = "0.1.0"
dependencies = [
"proc-macro2",
"pyo3",
"quote",
"quote_into",
"syn 1.0.109",
@@ -2457,6 +2456,7 @@ name = "dora-node-api-python"
version = "0.3.4"
dependencies = [
"arrow",
"derive",
"dora-node-api",
"dora-operator-api-python",
"dora-ros2-bridge-python",


+ 1
- 0
Cargo.toml View File

@@ -67,6 +67,7 @@ dora-coordinator = { version = "0.3.4", path = "binaries/coordinator" }
dora-ros2-bridge = { path = "libraries/extensions/ros2-bridge" }
dora-ros2-bridge-msg-gen = { path = "libraries/extensions/ros2-bridge/msg-gen" }
dora-ros2-bridge-python = { path = "libraries/extensions/ros2-bridge/python" }
derive = { path = "derive" }
arrow = { version = "52" }
arrow-schema = { version = "52" }
arrow-data = { version = "52" }


+ 1
- 0
apis/python/node/Cargo.toml View File

@@ -25,6 +25,7 @@ arrow = { workspace = true, features = ["pyarrow"] }
pythonize = { workspace = true }
futures = "0.3.28"
dora-ros2-bridge-python = { workspace = true }
derive = { workspace = true }

[lib]
name = "dora"


+ 8
- 0
apis/python/node/src/lib.rs View File

@@ -24,6 +24,7 @@ use pyo3::types::{PyBytes, PyDict};
/// ```
///
#[pyclass]
#[derive(derive::DirHelper)]
pub struct Node {
events: Events,
node: DoraNode,
@@ -197,6 +198,13 @@ impl Node {

Ok(())
}

// TODO: We should only list fields which are at least readableby Python users, right?
/// Get a list of the fields of this Node.
pub fn __dir__(&self) -> Vec<String> {
self.fields()
}
}

enum Events {


+ 0
- 1
derive/Cargo.toml View File

@@ -8,7 +8,6 @@ syn = "1.0"
quote = "1.0"
proc-macro2 = "1.0"
quote_into = "0.2.0"
pyo3 = { workspace = true, features = ["eyre", "abi3-py37"] }

[lib]
proc-macro = true

+ 9
- 5
derive/src/lib.rs View File

@@ -1,9 +1,15 @@
//! Derive macros for dora

extern crate proc_macro;
use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, Data, DeriveInput, Fields};

/// Add a Python `__dir__`` method to the struct.
/// Add a `fields` method to the struct.
///
/// Because we cannot have multiple `#[pymethods]` impls, this macro
/// produces a function called `fields` which should be called from the
/// PyO3 impl.
#[proc_macro_derive(DirHelper)]
pub fn dir_helper_derive(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
@@ -31,9 +37,8 @@ pub fn dir_helper_derive(input: TokenStream) -> TokenStream {
}
}];);
quote! {
#[pyo3::prelude::pymethods]
impl #name {
pub fn __dir__(&self) -> Vec<String> {
pub fn fields(&self) -> Vec<String> {
let mut names = Vec::new();
#assigner
names
@@ -44,9 +49,8 @@ pub fn dir_helper_derive(input: TokenStream) -> TokenStream {
Fields::Unit => {
// If the struct has no fields
quote! {
#[pyo3::prelude::pymethods]
impl #name {
pub fn __dir__(&self) -> Vec<String> {
pub fn fields(&self) -> Vec<String> {
Vec::new()
}
}


+ 2
- 5
derive/tests/functional.rs View File

@@ -1,8 +1,6 @@
use derive::DirHelper;
use pyo3::pyclass;

#[derive(DirHelper)]
#[pyclass]
#[allow(dead_code)]
struct WithFields {
hello: (),
@@ -19,7 +17,7 @@ fn test_with_fields() {
my: "".to_string(),
name: 0.0,
}
.__dir__();
.fields();
assert_eq!(
vec![
"hello".to_string(),
@@ -32,12 +30,11 @@ fn test_with_fields() {
}

#[derive(DirHelper)]
#[pyclass]
#[allow(dead_code)]
struct UnitNoFields;

#[test]
fn test_no_fields() {
let fields: Vec<String> = UnitNoFields.__dir__();
let fields: Vec<String> = UnitNoFields.fields();
assert_eq!(Vec::<String>::new(), fields);
}

Loading…
Cancel
Save