Skip to main content

nautilus_databento/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
23pub mod arrow;
24pub mod enums;
25pub mod historical;
26pub mod loader;
27pub mod types;
28
29#[cfg(feature = "live")]
30pub mod factories;
31#[cfg(feature = "live")]
32pub mod live;
33
34#[cfg(feature = "live")]
35use nautilus_common::factories::{ClientConfig, DataClientFactory};
36use nautilus_core::python::{to_pyruntime_err, to_pyvalue_err};
37use nautilus_system::get_global_pyo3_registry;
38use pyo3::prelude::*;
39
40#[cfg(feature = "live")]
41use crate::factories::{DatabentoDataClientFactory, DatabentoLiveClientConfig};
42
43#[cfg(feature = "live")]
44#[expect(clippy::needless_pass_by_value)]
45fn extract_databento_data_factory(
46    py: Python<'_>,
47    factory: Py<PyAny>,
48) -> PyResult<Box<dyn DataClientFactory>> {
49    match factory.extract::<DatabentoDataClientFactory>(py) {
50        Ok(f) => Ok(Box::new(f)),
51        Err(e) => Err(to_pyvalue_err(format!(
52            "Failed to extract DatabentoDataClientFactory: {e}"
53        ))),
54    }
55}
56
57#[cfg(feature = "live")]
58#[expect(clippy::needless_pass_by_value)]
59fn extract_databento_data_config(
60    py: Python<'_>,
61    config: Py<PyAny>,
62) -> PyResult<Box<dyn ClientConfig>> {
63    match config.extract::<DatabentoLiveClientConfig>(py) {
64        Ok(c) => Ok(Box::new(c)),
65        Err(e) => Err(to_pyvalue_err(format!(
66            "Failed to extract DatabentoLiveClientConfig: {e}"
67        ))),
68    }
69}
70
71/// Databento Python module.
72///
73/// The module is exposed under different paths depending on the build configuration:
74/// - With `cython-compat` feature: `nautilus_trader.core.nautilus_pyo3.databento`
75/// - Without `cython-compat`: `nautilus_trader.databento` (via re-export)
76///
77/// # Errors
78///
79/// Returns a `PyErr` if registering any module components fails.
80#[pymodule]
81pub fn databento(_: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> {
82    m.add_class::<super::enums::DatabentoStatisticType>()?;
83    m.add_class::<super::enums::DatabentoStatisticUpdateAction>()?;
84    m.add_class::<super::types::DatabentoPublisher>()?;
85    m.add_class::<super::types::DatabentoStatistics>()?;
86    m.add_class::<super::types::DatabentoImbalance>()?;
87    m.add_class::<super::loader::DatabentoDataLoader>()?;
88    m.add_class::<historical::DatabentoHistoricalClient>()?;
89    m.add_function(wrap_pyfunction!(arrow::get_databento_arrow_schema_map, m)?)?;
90    m.add_function(wrap_pyfunction!(
91        arrow::py_databento_imbalance_to_arrow_record_batch_bytes,
92        m
93    )?)?;
94    m.add_function(wrap_pyfunction!(
95        arrow::py_databento_imbalance_from_arrow_record_batch_bytes,
96        m
97    )?)?;
98    m.add_function(wrap_pyfunction!(
99        arrow::py_databento_statistics_to_arrow_record_batch_bytes,
100        m
101    )?)?;
102    m.add_function(wrap_pyfunction!(
103        arrow::py_databento_statistics_from_arrow_record_batch_bytes,
104        m
105    )?)?;
106
107    #[cfg(feature = "live")]
108    m.add_class::<live::DatabentoLiveClient>()?;
109    #[cfg(feature = "live")]
110    m.add_class::<types::DatabentoSubscriptionAck>()?;
111    #[cfg(feature = "live")]
112    m.add_class::<DatabentoLiveClientConfig>()?;
113    #[cfg(feature = "live")]
114    m.add_class::<DatabentoDataClientFactory>()?;
115
116    #[cfg(feature = "live")]
117    {
118        let registry = get_global_pyo3_registry();
119
120        if let Err(e) = registry
121            .register_factory_extractor("DATABENTO".to_string(), extract_databento_data_factory)
122        {
123            return Err(to_pyruntime_err(format!(
124                "Failed to register Databento data factory extractor: {e}"
125            )));
126        }
127
128        if let Err(e) = registry.register_config_extractor(
129            "DatabentoLiveClientConfig".to_string(),
130            extract_databento_data_config,
131        ) {
132            return Err(to_pyruntime_err(format!(
133                "Failed to register Databento data config extractor: {e}"
134            )));
135        }
136
137        // Register alias so callers using the generic name also resolve
138        if let Err(e) = registry.register_config_extractor(
139            "DatabentoDataClientConfig".to_string(),
140            extract_databento_data_config,
141        ) {
142            return Err(to_pyruntime_err(format!(
143                "Failed to register Databento data config alias extractor: {e}"
144            )));
145        }
146    }
147
148    Ok(())
149}