Skip to main content

nautilus_common/python/
mod.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16//! Python bindings from [PyO3](https://pyo3.rs).
17
18#![expect(
19    clippy::missing_errors_doc,
20    reason = "errors documented on underlying Rust methods"
21)]
22#![allow(
23    clippy::unused_self,
24    reason = "PyO3 stub methods take &self for Python API parity even when the body is empty"
25)]
26
27pub mod actor;
28pub mod cache;
29pub mod clock;
30pub mod custom;
31pub mod enums;
32pub mod fifo;
33pub mod greeks;
34pub mod indicators;
35pub mod listener;
36pub mod logging;
37pub mod msgbus;
38pub mod order_factory;
39pub mod runtime;
40pub mod signal;
41pub mod timer;
42pub mod xrate;
43
44use nautilus_core::python::to_pyvalue_err;
45use pyo3::{PyErr, prelude::*};
46
47use crate::config::ConfigError;
48
49/// Converts a config validation failure to a Python `ValueError`.
50#[must_use]
51#[allow(
52    clippy::needless_pass_by_value,
53    reason = "Result::map_err passes owned errors to conversion functions"
54)]
55pub fn config_error_to_pyvalue_err(e: ConfigError) -> PyErr {
56    to_pyvalue_err(e)
57}
58
59/// Loaded as `nautilus_pyo3.common`.
60///
61/// # Errors
62///
63/// Returns a `PyErr` if registering any module components fails.
64#[rustfmt::skip]
65#[pymodule]
66pub fn common(_: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> {
67    m.add_class::<crate::custom::CustomData>()?;
68    m.add_class::<crate::signal::Signal>()?;
69    m.add_class::<crate::timer::TimeEvent>()?;
70    m.add_class::<crate::cache::CacheConfig>()?;
71    m.add_class::<crate::python::actor::PyDataActor>()?;
72    m.add_class::<crate::python::cache::PyCache>()?;
73    m.add_class::<crate::python::fifo::PyFifoCache>()?;
74    m.add_class::<crate::python::clock::PyClock>()?;
75    m.add_class::<crate::python::order_factory::PyOrderFactory>()?;
76    m.add_class::<crate::python::greeks::PyGreeksCalculator>()?;
77    m.add_class::<crate::python::logging::PyLogger>()?;
78    m.add_class::<crate::actor::data_actor::DataActorConfig>()?;
79    m.add_class::<crate::actor::data_actor::ImportableActorConfig>()?;
80    m.add_class::<crate::msgbus::BusMessage>()?;
81    m.add_class::<crate::msgbus::config::MessageBusConfig>()?;
82    m.add_class::<crate::python::msgbus::PyMessageBus>()?;
83    m.add_class::<crate::enums::ComponentState>()?;
84    m.add_class::<crate::enums::ComponentTrigger>()?;
85    m.add_class::<crate::enums::Environment>()?;
86    m.add_class::<crate::enums::LogColor>()?;
87    m.add_class::<crate::enums::LogLevel>()?;
88    m.add_class::<crate::enums::LogFormat>()?;
89    m.add_class::<crate::logging::logger::LoggerConfig>()?;
90    m.add_class::<crate::logging::logger::LogGuard>()?;
91    m.add_class::<crate::logging::writer::FileWriterConfig>()?;
92    m.add_function(wrap_pyfunction!(logging::py_init_logging, m)?)?;
93    m.add_function(wrap_pyfunction!(logging::py_logger_flush, m)?)?;
94    m.add_function(wrap_pyfunction!(logging::py_logging_sync_to_disk, m)?)?;
95    m.add_function(wrap_pyfunction!(logging::py_logger_log, m)?)?;
96    m.add_function(wrap_pyfunction!(logging::py_log_header, m)?)?;
97    m.add_function(wrap_pyfunction!(logging::py_log_sysinfo, m)?)?;
98    m.add_function(wrap_pyfunction!(logging::py_logging_clock_set_static_mode, m)?)?;
99    m.add_function(wrap_pyfunction!(logging::py_logging_clock_set_realtime_mode, m)?)?;
100    m.add_function(wrap_pyfunction!(logging::py_logging_clock_set_static_time, m)?)?;
101    #[cfg(feature = "tracing-bridge")]
102    m.add_function(wrap_pyfunction!(logging::py_tracing_is_initialized, m)?)?;
103    #[cfg(feature = "tracing-bridge")]
104    m.add_function(wrap_pyfunction!(logging::py_init_tracing, m)?)?;
105    m.add_function(wrap_pyfunction!(xrate::py_get_exchange_rate, m)?)?;
106
107    #[cfg(feature = "live")]
108    m.add_class::<crate::live::listener::MessageBusListener>()?;
109
110    Ok(())
111}
112
113#[cfg(test)]
114mod tests {
115    use std::sync::Once;
116
117    use pyo3::{Python, exceptions::PyValueError};
118    use rstest::rstest;
119
120    use super::*;
121
122    fn ensure_python_initialized() {
123        static INIT: Once = Once::new();
124        INIT.call_once(|| {
125            Python::initialize();
126        });
127    }
128
129    #[rstest]
130    fn test_config_error_to_pyvalue_err_preserves_display_text() {
131        ensure_python_initialized();
132
133        let error = ConfigError::invalid_format("rate_limit", "expected 'limit/HH:MM:SS'");
134
135        Python::attach(|py| {
136            let py_err = config_error_to_pyvalue_err(error);
137
138            assert!(py_err.is_instance_of::<PyValueError>(py));
139            assert_eq!(
140                py_err.value(py).to_string(),
141                "invalid rate_limit: expected 'limit/HH:MM:SS'"
142            );
143        });
144    }
145}