1#![warn(rustc::all)]
46#![deny(unsafe_code)]
47#![deny(nonstandard_style)]
48#![deny(missing_debug_implementations)]
49#![deny(clippy::missing_errors_doc)]
50#![deny(clippy::missing_panics_doc)]
51#![deny(rustdoc::broken_intra_doc_links)]
52
53use std::path::Path;
54
55use pyo3::prelude::*;
56
57#[pymodule] #[cfg_attr(feature = "cython-compat", pyo3(name = "nautilus_pyo3"))]
64fn _libnautilus(py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> {
65 let sys = PyModule::import(py, "sys")?;
66 let modules = sys.getattr("modules")?;
67 let sys_modules: &Bound<'_, PyAny> = modules.downcast()?;
68
69 #[cfg(feature = "cython-compat")]
70 let module_name = "nautilus_trader.core.nautilus_pyo3";
71
72 #[cfg(not(feature = "cython-compat"))]
73 let module_name = "nautilus_trader._libnautilus";
74
75 sys_modules.set_item(module_name, m)?;
77
78 let n = "core";
79 let submodule = pyo3::wrap_pymodule!(nautilus_core::python::core);
80 m.add_wrapped(submodule)?;
81 sys_modules.set_item(format!("{module_name}.{n}"), m.getattr(n)?)?;
82 #[cfg(feature = "cython-compat")]
83 re_export_module_attributes(m, n)?;
84
85 let n = "common";
86 let submodule = pyo3::wrap_pymodule!(nautilus_common::python::common);
87 m.add_wrapped(submodule)?;
88 sys_modules.set_item(format!("{module_name}.{n}"), m.getattr(n)?)?;
89 #[cfg(feature = "cython-compat")]
90 re_export_module_attributes(m, n)?;
91
92 let n = "cryptography";
93 let submodule = pyo3::wrap_pymodule!(nautilus_cryptography::python::cryptography);
94 m.add_wrapped(submodule)?;
95 sys_modules.set_item(format!("{module_name}.{n}"), m.getattr(n)?)?;
96 #[cfg(feature = "cython-compat")]
97 re_export_module_attributes(m, n)?;
98
99 let n = "indicators";
100 let submodule = pyo3::wrap_pymodule!(nautilus_indicators::python::indicators);
101 m.add_wrapped(submodule)?;
102 sys_modules.set_item(format!("{module_name}.{n}"), m.getattr(n)?)?;
103 #[cfg(feature = "cython-compat")]
104 re_export_module_attributes(m, n)?;
105
106 let n = "infrastructure";
107 let submodule = pyo3::wrap_pymodule!(nautilus_infrastructure::python::infrastructure);
108 m.add_wrapped(submodule)?;
109 sys_modules.set_item(format!("{module_name}.{n}"), m.getattr(n)?)?;
110 #[cfg(feature = "cython-compat")]
111 re_export_module_attributes(m, n)?;
112
113 let n = "live";
114 let submodule = pyo3::wrap_pymodule!(nautilus_live::python::live);
115 m.add_wrapped(submodule)?;
116 sys_modules.set_item(format!("{module_name}.{n}"), m.getattr(n)?)?;
117 #[cfg(feature = "cython-compat")]
118 re_export_module_attributes(m, n)?;
119
120 let n = "model";
121 let submodule = pyo3::wrap_pymodule!(nautilus_model::python::model);
122 m.add_wrapped(submodule)?;
123 sys_modules.set_item(format!("{module_name}.{n}"), m.getattr(n)?)?;
124 #[cfg(feature = "cython-compat")]
125 re_export_module_attributes(m, n)?;
126
127 let n = "network";
128 let submodule = pyo3::wrap_pymodule!(nautilus_network::python::network);
129 m.add_wrapped(submodule)?;
130 sys_modules.set_item(format!("{module_name}.{n}"), m.getattr(n)?)?;
131 #[cfg(feature = "cython-compat")]
132 re_export_module_attributes(m, n)?;
133
134 let n = "persistence";
135 let submodule = pyo3::wrap_pymodule!(nautilus_persistence::python::persistence);
136 m.add_wrapped(submodule)?;
137 sys_modules.set_item(format!("{module_name}.{n}"), m.getattr(n)?)?;
138 #[cfg(feature = "cython-compat")]
139 re_export_module_attributes(m, n)?;
140
141 let n = "serialization";
142 let submodule = pyo3::wrap_pymodule!(nautilus_serialization::python::serialization);
143 m.add_wrapped(submodule)?;
144 sys_modules.set_item(format!("{module_name}.{n}"), m.getattr(n)?)?;
145 #[cfg(feature = "cython-compat")]
146 re_export_module_attributes(m, n)?;
147
148 let n = "testkit";
149 let submodule = pyo3::wrap_pymodule!(nautilus_testkit::python::testkit);
150 m.add_wrapped(submodule)?;
151 sys_modules.set_item(format!("{module_name}.{n}"), m.getattr(n)?)?;
152 #[cfg(feature = "cython-compat")]
153 re_export_module_attributes(m, n)?;
154
155 let n = "trading";
156 let submodule = pyo3::wrap_pymodule!(nautilus_trading::python::trading);
157 m.add_wrapped(submodule)?;
158 sys_modules.set_item(format!("{module_name}.{n}"), m.getattr(n)?)?;
159 #[cfg(feature = "cython-compat")]
160 re_export_module_attributes(m, n)?;
161
162 let n = "bitmex";
165 let submodule = pyo3::wrap_pymodule!(nautilus_bitmex::python::bitmex);
166 m.add_wrapped(submodule)?;
167 sys_modules.set_item(format!("{module_name}.{n}"), m.getattr(n)?)?;
168 #[cfg(feature = "cython-compat")]
169 re_export_module_attributes(m, n)?;
170
171 let n = "coinbase_intx";
172 let submodule = pyo3::wrap_pymodule!(nautilus_coinbase_intx::python::coinbase_intx);
173 m.add_wrapped(submodule)?;
174 sys_modules.set_item(format!("{module_name}.{n}"), m.getattr(n)?)?;
175 #[cfg(feature = "cython-compat")]
176 re_export_module_attributes(m, n)?;
177
178 let n = "databento";
179 let submodule = pyo3::wrap_pymodule!(nautilus_databento::python::databento);
180 m.add_wrapped(submodule)?;
181 sys_modules.set_item(format!("{module_name}.{n}"), m.getattr(n)?)?;
182 #[cfg(feature = "cython-compat")]
183 re_export_module_attributes(m, n)?;
184
185 let n = "hyperliquid";
186 let submodule = pyo3::wrap_pymodule!(nautilus_hyperliquid::python::hyperliquid);
187 m.add_wrapped(submodule)?;
188 sys_modules.set_item(format!("{module_name}.{n}"), m.getattr(n)?)?;
189 #[cfg(feature = "cython-compat")]
190 re_export_module_attributes(m, n)?;
191
192 let n = "okx";
193 let submodule = pyo3::wrap_pymodule!(nautilus_okx::python::okx);
194 m.add_wrapped(submodule)?;
195 sys_modules.set_item(format!("{module_name}.{n}"), m.getattr(n)?)?;
196 #[cfg(feature = "cython-compat")]
197 re_export_module_attributes(m, n)?;
198
199 let n = "tardis";
200 let submodule = pyo3::wrap_pymodule!(nautilus_tardis::python::tardis);
201 m.add_wrapped(submodule)?;
202 sys_modules.set_item(format!("{module_name}.{n}"), m.getattr(n)?)?;
203 #[cfg(feature = "cython-compat")]
204 re_export_module_attributes(m, n)?;
205
206 #[cfg(feature = "defi")]
207 {
208 let n = "blockchain";
209 let submodule = pyo3::wrap_pymodule!(nautilus_blockchain::python::blockchain);
210 m.add_wrapped(submodule)?;
211 sys_modules.set_item(format!("{module_name}.{n}"), m.getattr(n)?)?;
212 #[cfg(feature = "cython-compat")]
213 re_export_module_attributes(m, n)?;
214 }
215
216 Ok(())
217}
218
219#[cfg(feature = "cython-compat")]
220fn re_export_module_attributes(
221 parent_module: &Bound<'_, PyModule>,
222 submodule_name: &str,
223) -> PyResult<()> {
224 let submodule = parent_module.getattr(submodule_name)?;
225 for item_name in submodule.dir()? {
226 let item_name_str: &str = item_name.extract()?;
227 if let Ok(attr) = submodule.getattr(item_name_str) {
228 parent_module.add(item_name_str, attr)?;
229 }
230 }
231
232 Ok(())
233}
234
235pub fn stub_info() -> pyo3_stub_gen::Result<pyo3_stub_gen::StubInfo> {
253 let workspace_root = Path::new(env!("CARGO_MANIFEST_DIR"))
254 .parent()
255 .unwrap()
256 .parent()
257 .unwrap();
258 let pyproject_path = workspace_root.join("python").join("pyproject.toml");
259
260 pyo3_stub_gen::StubInfo::from_pyproject_toml(&pyproject_path)
261}