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