Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(is-ok-in-reply): Adding is_ok attribute to Reply objects #113

Merged
merged 3 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
834 changes: 492 additions & 342 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ It relies on the [zenoh](https://github.com/eclipse-zenoh/zenoh/tree/master/zeno
Requirements:
* Python >= 3.7
* pip >= 19.3.1
* (Optional) A Python virtual environment (for instance [virtualenv](docs.python.org/3.10/tutorial/venv.html) or [miniconda](https://docs.conda.io/en/latest/miniconda.html))
* (Optional) A Python virtual environment (for instance [virtualenv](https://docs.python.org/3.10/tutorial/venv.html) or [miniconda](https://docs.conda.io/en/latest/miniconda.html))
* [Rust and Cargo](https://doc.rust-lang.org/cargo/getting-started/installation.html). If you already have the Rust toolchain installed, make sure it is up-to-date with:

```bash
Expand Down
2 changes: 1 addition & 1 deletion examples/z_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
session = zenoh.open(conf)

print("Sending Query '{}'...".format(selector))
replies = session.get(selector, zenoh.Queue(), target=target, value=args.value)
replies = session.get(selector, zenoh.Queue(), target=target, value=args.value, consolidation=zenoh.QueryConsolidation.NONE())
for reply in replies.receiver:
try:
print(">> Received ('{}': '{}')"
Expand Down
6 changes: 4 additions & 2 deletions examples/z_queryable.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,10 @@ def queryable_callback(query):
c = '\0'
while c != 'q':
c = sys.stdin.read(1)
if c == '':
if c != 'q':
print("getting")
session.get(key, print, consolidation=zenoh.QueryConsolidation.NONE())
time.sleep(1)

queryable.undeclare()
session.close()
session.close()
5 changes: 5 additions & 0 deletions src/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,11 @@ impl _SampleKind {
}
}
}
impl core::fmt::Debug for _SampleKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
core::fmt::Debug::fmt(&self.0, f)
}
}

#[pyclass(subclass)]
#[derive(Clone, PartialEq, Eq)]
Expand Down
9 changes: 9 additions & 0 deletions src/queryable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ impl _Query {
.res_sync()
.map_err(|e| e.to_pyerr())
}
pub fn reply_err(&self, value: _Value) -> PyResult<()> {
self.0
.reply(Err(value.into()))
.res_sync()
.map_err(|e| e.to_pyerr())
}
pub fn __str__(&self) -> String {
self.0.to_string()
}
}
impl From<Query> for _Query {
fn from(q: Query) -> Self {
Expand Down
48 changes: 45 additions & 3 deletions src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,27 @@ impl From<Py<PyBytes>> for Payload {
Payload::Python(buf)
}
}
impl core::fmt::Debug for Payload {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Zenoh(arg0) => {
for slice in arg0.slices() {
for byte in slice {
write!(f, "{byte:02x}")?
}
}
}
Self::Python(arg0) => {
for byte in arg0.as_bytes(unsafe { Python::assume_gil_acquired() }) {
write!(f, "{byte:02x}")?
}
}
};
Ok(())
}
}
#[pyclass(subclass)]
#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct _Value {
pub(crate) payload: Payload,
pub(crate) encoding: Encoding,
Expand Down Expand Up @@ -108,6 +127,9 @@ impl _Value {
pub fn with_encoding(&mut self, encoding: _Encoding) {
self.encoding = encoding.0;
}
pub fn __str__(&self) -> String {
format!("{self:?}")
}
}
impl From<Value> for _Value {
fn from(value: Value) -> Self {
Expand Down Expand Up @@ -135,7 +157,7 @@ impl PyAnyToValue for &PyAny {
}

#[pyclass(subclass)]
#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct _Sample {
key_expr: KeyExpr<'static>,
value: _Value,
Expand Down Expand Up @@ -163,6 +185,11 @@ impl From<Sample> for _Sample {
#[pyclass(subclass)]
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct _ZenohId(pub(crate) ZenohId);
impl core::fmt::Debug for _ZenohId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
core::fmt::Debug::fmt(&self.0, f)
}
}
#[pymethods]
impl _ZenohId {
#[new]
Expand Down Expand Up @@ -202,6 +229,11 @@ impl _Timestamp {
self.0.get_time().as_secs_f64()
}
}
impl core::fmt::Debug for _Timestamp {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
core::fmt::Debug::fmt(&self.0, f)
}
}

#[pymethods]
impl _Sample {
Expand Down Expand Up @@ -253,6 +285,9 @@ impl _Sample {
timestamp,
}
}
fn __str__(&self) -> String {
format!("{self:?}")
}
}

impl From<_Sample> for Sample {
Expand All @@ -271,7 +306,7 @@ impl From<_Sample> for Sample {
}

#[pyclass(subclass)]
#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct _Reply {
#[pyo3(get)]
pub replier_id: _ZenohId,
Expand All @@ -297,6 +332,13 @@ impl _Reply {
Ok(_) => Err(zenoh_core::zerror!("Called `Reply.err` on a non-err reply.").to_pyerr()),
}
}
#[getter]
pub fn is_ok(&self) -> bool {
self.reply.is_ok()
}
fn __str__(&self) -> String {
format!("{self:?}")
}
}
impl From<Reply> for _Reply {
fn from(reply: Reply) -> Self {
Expand Down
11 changes: 9 additions & 2 deletions zenoh/queryable.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

from .zenoh import _Query, _Queryable
from .keyexpr import KeyExpr, Selector
from .value import Sample, Value
from .value import Sample, Value, IntoValue, IntoSample

class Queryable:
"""
Expand Down Expand Up @@ -73,4 +73,11 @@ def reply(self, sample: Sample):
Allows you to reply to a query.
You may send any amount of replies to a single query, including 0.
"""
super().reply(sample)
super().reply(sample)
def reply_err(self, value: IntoValue):
"""
Allows you to reply to a query with an error.
You may send any amount of replies to a single query, including 0.
Sending error responses does not exclude sending other responses.
"""
super().reply_err(Value(value))
10 changes: 10 additions & 0 deletions zenoh/value.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,16 @@ def __new__(cls, inner: _Reply):
def replier_id(self) -> ZenohId:
"The reply's sender's id."
return ZenohId._upgrade_(super().replier_id)

@property
def is_ok(self) -> bool:
"""
Checks if the reply is `ok`.

Returns `True` if the reply is `ok`, `False` otherwise
"""
return super.is_ok()

@property
def ok(self) -> Sample:
"""
Expand Down
Loading