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 commands;
31pub mod custom;
32pub mod enums;
33pub mod factory;
34pub mod fifo;
35pub mod greeks;
36pub mod indicators;
37pub mod listener;
38pub mod logging;
39pub mod msgbus;
40pub mod order_factory;
41pub mod response;
42pub mod runtime;
43pub mod signal;
44pub mod system;
45pub mod timer;
46pub mod wrappers;
47pub mod xrate;
48
49use nautilus_core::python::to_pyvalue_err;
50use pyo3::{PyErr, prelude::*};
51
52use crate::config::ConfigError;
53
54/// Converts a config validation failure to a Python `ValueError`.
55#[must_use]
56#[allow(
57    clippy::needless_pass_by_value,
58    reason = "Result::map_err passes owned errors to conversion functions"
59)]
60pub fn config_error_to_pyvalue_err(e: ConfigError) -> PyErr {
61    to_pyvalue_err(e)
62}
63
64/// Exposed through `nautilus_trader.common`.
65///
66/// # Errors
67///
68/// Returns an error if registering any module components fails.
69#[rustfmt::skip]
70#[pymodule]
71pub fn common(_: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> {
72    m.add_class::<crate::custom::CustomData>()?;
73    m.add_class::<crate::python::response::PyDataResponse>()?;
74    m.add_class::<crate::signal::Signal>()?;
75    m.add_class::<crate::runner::SystemChannel>()?;
76    m.add_class::<crate::messages::system::QueueCondition>()?;
77    m.add_class::<crate::messages::system::QueueState>()?;
78    m.add_class::<crate::messages::system::QueueStateChanged>()?;
79    m.add_class::<crate::messages::system::SocketState>()?;
80    m.add_class::<crate::messages::system::SocketStateChanged>()?;
81    m.add_class::<crate::messages::system::ReconnectSocket>()?;
82    m.add_class::<crate::timer::TimeEvent>()?;
83    m.add_class::<crate::cache::CacheConfig>()?;
84    m.add_class::<crate::python::actor::PyDataActor>()?;
85    m.add_class::<crate::python::cache::PyCache>()?;
86    m.add_class::<crate::python::fifo::PyFifoCache>()?;
87    m.add_class::<crate::python::clock::PyClock>()?;
88    m.add_class::<crate::python::order_factory::PyOrderFactory>()?;
89    m.add_class::<crate::python::greeks::PyGreeksCalculator>()?;
90    m.add_class::<crate::python::logging::PyLogger>()?;
91    m.add_class::<crate::actor::data_actor::DataActorConfig>()?;
92    m.add_class::<crate::actor::data_actor::ImportableActorConfig>()?;
93    m.add_class::<crate::msgbus::BusMessage>()?;
94    m.add_class::<crate::msgbus::config::MessageBusConfig>()?;
95    m.add_class::<crate::python::msgbus::PyMessageBus>()?;
96    m.add_class::<crate::enums::ComponentState>()?;
97    m.add_class::<crate::enums::ComponentTrigger>()?;
98    m.add_class::<crate::enums::Environment>()?;
99    m.add_class::<crate::enums::LogColor>()?;
100    m.add_class::<crate::enums::LogLevel>()?;
101    m.add_class::<crate::enums::LogFormat>()?;
102    m.add_class::<crate::enums::SerializationEncoding>()?;
103    m.add_class::<crate::logging::logger::LoggerConfig>()?;
104    m.add_class::<crate::logging::logger::LogGuard>()?;
105    m.add_class::<crate::logging::writer::FileWriterConfig>()?;
106    m.add_function(wrap_pyfunction!(logging::py_init_logging, m)?)?;
107    m.add_function(wrap_pyfunction!(logging::py_logger_flush, m)?)?;
108    m.add_function(wrap_pyfunction!(logging::py_logging_sync_to_disk, m)?)?;
109    m.add_function(wrap_pyfunction!(logging::py_logger_log, m)?)?;
110    m.add_function(wrap_pyfunction!(logging::py_log_header, m)?)?;
111    m.add_function(wrap_pyfunction!(logging::py_log_sysinfo, m)?)?;
112    m.add_function(wrap_pyfunction!(logging::py_logging_clock_set_static_mode, m)?)?;
113    m.add_function(wrap_pyfunction!(logging::py_logging_clock_set_realtime_mode, m)?)?;
114    m.add_function(wrap_pyfunction!(logging::py_logging_clock_set_static_time, m)?)?;
115    #[cfg(feature = "tracing-bridge")]
116    m.add_function(wrap_pyfunction!(logging::py_tracing_is_initialized, m)?)?;
117    #[cfg(feature = "tracing-bridge")]
118    m.add_function(wrap_pyfunction!(logging::py_init_tracing, m)?)?;
119    m.add_function(wrap_pyfunction!(xrate::py_get_exchange_rate, m)?)?;
120
121    #[cfg(feature = "live")]
122    m.add_class::<crate::live::listener::MessageBusListener>()?;
123
124    Ok(())
125}
126
127#[cfg(test)]
128mod tests {
129    use std::sync::Once;
130
131    use pyo3::{Python, exceptions::PyValueError};
132    use rstest::rstest;
133
134    use super::*;
135
136    fn ensure_python_initialized() {
137        static INIT: Once = Once::new();
138        INIT.call_once(|| {
139            Python::initialize();
140        });
141    }
142
143    #[rstest]
144    fn test_config_error_to_pyvalue_err_preserves_display_text() {
145        ensure_python_initialized();
146
147        let error = ConfigError::invalid_format("rate_limit", "expected 'limit/HH:MM:SS'");
148
149        Python::attach(|py| {
150            let py_err = config_error_to_pyvalue_err(error);
151
152            assert!(py_err.is_instance_of::<PyValueError>(py));
153            assert_eq!(
154                py_err.value(py).to_string(),
155                "invalid rate_limit: expected 'limit/HH:MM:SS'"
156            );
157        });
158    }
159}