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