Skip to main content

nautilus_finfeed/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
18pub mod config;
19pub mod factories;
20pub mod http;
21pub mod loaders;
22pub mod models;
23
24mod query;
25
26use nautilus_common::factories::{ClientConfig, DataClientFactory};
27use nautilus_core::python::{to_pyruntime_err, to_pyvalue_err};
28use nautilus_system::get_global_pyo3_registry;
29use pyo3::prelude::*;
30
31use crate::{
32    common::consts::{FINFEED, FINFEED_CLIENT_ID},
33    config::FinFeedDataClientConfig,
34    factories::FinFeedDataClientFactory,
35    instruments::FinFeedInstrumentSelection,
36};
37
38/// FinFeed Python module exposed as `nautilus_trader._libnautilus.finfeed`.
39///
40/// # Errors
41///
42/// Returns an error if adding module attributes or registering an extractor fails.
43#[pymodule]
44pub fn finfeed(m: &Bound<'_, PyModule>) -> PyResult<()> {
45    m.add(stringify!(FINFEED), FINFEED)?;
46    m.add(stringify!(FINFEED_CLIENT_ID), *FINFEED_CLIENT_ID)?;
47    m.add_class::<FinFeedDataClientConfig>()?;
48    m.add_class::<FinFeedDataClientFactory>()?;
49    m.add_class::<loaders::PyFinFeedDataLoader>()?;
50    m.add_class::<FinFeedInstrumentSelection>()?;
51    http::register(m)?;
52    models::register(m)?;
53    query::register(m)?;
54    let registry = get_global_pyo3_registry();
55
56    if let Err(e) =
57        registry.register_factory_extractor(FINFEED.to_string(), extract_finfeed_data_factory)
58    {
59        return Err(to_pyruntime_err(format!(
60            "Failed to register FinFeed data factory extractor: {e}"
61        )));
62    }
63
64    if let Err(e) = registry.register_config_extractor(
65        "FinFeedDataClientConfig".to_string(),
66        extract_finfeed_data_config,
67    ) {
68        return Err(to_pyruntime_err(format!(
69            "Failed to register FinFeed data config extractor: {e}"
70        )));
71    }
72
73    Ok(())
74}
75
76#[expect(clippy::needless_pass_by_value)]
77fn extract_finfeed_data_factory(
78    py: Python<'_>,
79    factory: Py<PyAny>,
80) -> PyResult<Box<dyn DataClientFactory>> {
81    match factory.extract::<FinFeedDataClientFactory>(py) {
82        Ok(f) => Ok(Box::new(f)),
83        Err(e) => Err(to_pyvalue_err(format!(
84            "Failed to extract FinFeedDataClientFactory: {e}"
85        ))),
86    }
87}
88
89#[expect(clippy::needless_pass_by_value)]
90fn extract_finfeed_data_config(
91    py: Python<'_>,
92    config: Py<PyAny>,
93) -> PyResult<Box<dyn ClientConfig>> {
94    match config.extract::<FinFeedDataClientConfig>(py) {
95        Ok(c) => Ok(Box::new(c)),
96        Err(e) => Err(to_pyvalue_err(format!(
97            "Failed to extract FinFeedDataClientConfig: {e}"
98        ))),
99    }
100}
101
102#[cfg(test)]
103mod tests {
104    use nautilus_model::identifiers::ClientId;
105    use pyo3::prelude::*;
106    use rstest::rstest;
107
108    #[rstest]
109    fn test_module_registration() {
110        Python::initialize();
111        Python::attach(|py| {
112            let module = pyo3::wrap_pymodule!(super::finfeed)(py);
113            let module = module.bind(py);
114
115            assert_eq!(module.name().unwrap(), "finfeed");
116            assert_eq!(
117                module
118                    .getattr("FINFEED")
119                    .unwrap()
120                    .extract::<String>()
121                    .unwrap(),
122                "FINFEED",
123            );
124            assert_eq!(
125                module
126                    .getattr("FINFEED_CLIENT_ID")
127                    .unwrap()
128                    .extract::<ClientId>()
129                    .unwrap(),
130                ClientId::from("FINFEED"),
131            );
132        });
133    }
134}