| 1 |
//! PyO3 bindings for `dromedary::log::LogTransport`. |
| 2 |
|
| 3 |
use crate::{Transport, TransportDecorator}; |
| 4 |
use dromedary::log::{LogSink, LogTransport}; |
| 5 |
use dromedary::pyo3::PyTransport; |
| 6 |
use pyo3::exceptions::PyValueError; |
| 7 |
use pyo3::prelude::*; |
| 8 |
use std::sync::Arc; |
| 9 |
|
| 10 |
const PREFIX: &str = "log+"; |
| 11 |
|
| 12 |
fn resolve_inner(py: Python, url: &str, decorated: Option<Py<PyAny>>) -> PyResult<Py<PyAny>> { |
| 13 |
if let Some(d) = decorated { |
| 14 |
return Ok(d); |
| 15 |
} |
| 16 |
if !url.starts_with(PREFIX) { |
| 17 |
return Err(PyValueError::new_err(format!( |
| 18 |
"url {:?} doesn't start with decorator prefix {:?}", |
| 19 |
url, PREFIX |
| 20 |
))); |
| 21 |
} |
| 22 |
let rest = &url[PREFIX.len()..]; |
| 23 |
let dromedary = py.import("dromedary")?; |
| 24 |
let urlutils = py.import("dromedary.urlutils")?; |
| 25 |
let is_url: bool = urlutils.call_method1("is_url", (rest,))?.extract()?; |
| 26 |
let func = if is_url { |
| 27 |
dromedary.getattr("get_transport_from_url")? |
| 28 |
} else { |
| 29 |
dromedary.getattr("get_transport_from_path")? |
| 30 |
}; |
| 31 |
Ok(func.call1((rest,))?.unbind()) |
| 32 |
} |
| 33 |
|
| 34 |
/// Build a sink that forwards formatted messages to the named Python logger |
| 35 |
/// at DEBUG level. Wrapped in `Arc` so the sink can be cheaply cloned — each |
| 36 |
/// LogTransport instance keeps its own handle but they share the underlying |
| 37 |
/// Python object. |
| 38 |
fn python_debug_sink(py: Python, logger_name: &str) -> PyResult<LogSink> { |
| 39 |
let logging = py.import("logging")?; |
| 40 |
let logger = logging.call_method1("getLogger", (logger_name,))?.unbind(); |
| 41 |
let logger = Arc::new(logger); |
| 42 |
Ok(Arc::new(move |msg: &str| { |
| 43 |
Python::attach(|py| { |
| 44 |
// Ignore errors — logging failures should not propagate and break |
| 45 |
// the transport method. If the logger is gone we silently drop |
| 46 |
// the message, matching Python's best-effort logging semantics. |
| 47 |
let _ = logger.bind(py).call_method1("debug", (msg,)); |
| 48 |
}); |
| 49 |
})) |
| 50 |
} |
| 51 |
|
| 52 |
fn wrap_inner(decorated: &Py<PyAny>, py: Python) -> PyResult<Transport> { |
| 53 |
let py_inner: Box<dyn dromedary::Transport + Send + Sync> = |
| 54 |
Box::new(PyTransport::from(decorated.clone_ref(py))); |
| 55 |
let sink = python_debug_sink(py, "dromedary.log")?; |
| 56 |
Ok(Transport(Box::new(LogTransport::new(py_inner, sink)))) |
| 57 |
} |
| 58 |
|
| 59 |
#[pyclass(extends=TransportDecorator, subclass)] |
| 60 |
pub(crate) struct TransportLogDecorator; |
| 61 |
|
| 62 |
#[pymethods] |
| 63 |
impl TransportLogDecorator { |
| 64 |
#[new] |
| 65 |
#[pyo3(signature = (url, _decorated=None, _from_transport=None))] |
| 66 |
fn new( |
| 67 |
py: Python, |
| 68 |
url: &str, |
| 69 |
_decorated: Option<Py<PyAny>>, |
| 70 |
_from_transport: Option<Py<PyAny>>, |
| 71 |
) -> PyResult<PyClassInitializer<Self>> { |
| 72 |
let _ = _from_transport; |
| 73 |
let decorated = resolve_inner(py, url, _decorated)?; |
| 74 |
let wrapped = wrap_inner(&decorated, py)?; |
| 75 |
Ok(PyClassInitializer::from(wrapped) |
| 76 |
.add_subclass(TransportDecorator { |
| 77 |
decorated, |
| 78 |
prefix: PREFIX, |
| 79 |
}) |
| 80 |
.add_subclass(TransportLogDecorator)) |
| 81 |
} |
| 82 |
|
| 83 |
#[classmethod] |
| 84 |
fn _get_url_prefix(_cls: &Bound<'_, pyo3::types::PyType>) -> &'static str { |
| 85 |
PREFIX |
| 86 |
} |
| 87 |
|
| 88 |
#[pyo3(signature = (offset=None))] |
| 89 |
fn clone<'a>( |
| 90 |
slf: PyRef<'a, Self>, |
| 91 |
py: Python<'a>, |
| 92 |
offset: Option<Py<PyAny>>, |
| 93 |
) -> PyResult<Bound<'a, TransportLogDecorator>> { |
| 94 |
let decorator: &TransportDecorator = slf.as_super(); |
| 95 |
let decorated = decorator.decorated.clone_ref(py); |
| 96 |
let decorated_clone = match offset { |
| 97 |
Some(o) => decorated.call_method1(py, "clone", (o,))?, |
| 98 |
None => decorated.call_method0(py, "clone")?, |
| 99 |
}; |
| 100 |
let wrapped = wrap_inner(&decorated_clone, py)?; |
| 101 |
let init = PyClassInitializer::from(wrapped) |
| 102 |
.add_subclass(TransportDecorator { |
| 103 |
decorated: decorated_clone, |
| 104 |
prefix: PREFIX, |
| 105 |
}) |
| 106 |
.add_subclass(TransportLogDecorator); |
| 107 |
Bound::new(py, init) |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
pub(crate) fn register(_py: Python, m: &Bound<PyModule>) -> PyResult<()> { |
| 112 |
m.add_class::<TransportLogDecorator>()?; |
| 113 |
Ok(()) |
| 114 |
} |