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