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