Skip to content

Commit 3c79f20

Browse files
committed
extract sys path to fn
1 parent d4d4615 commit 3c79f20

File tree

1 file changed

+14
-16
lines changed
  • packages/cubejs-backend-native/src/python

1 file changed

+14
-16
lines changed

packages/cubejs-backend-native/src/python/entry.rs

+14-16
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@ use pyo3::prelude::*;
88
use pyo3::types::{PyDict, PyFunction, PyList, PyString, PyTuple};
99
use std::path::Path;
1010

11+
fn extend_sys_path(py: Python, file_name: &String) -> PyResult<()> {
12+
let sys_path = py.import("sys")?.getattr("path")?.downcast::<PyList>()?;
13+
14+
let config_dir = Path::new(&file_name)
15+
.parent()
16+
.unwrap_or_else(|| Path::new("."));
17+
let config_dir_str = config_dir.to_str().unwrap_or(".");
18+
19+
sys_path.insert(0, PyString::new(py, config_dir_str))?;
20+
Ok(())
21+
}
22+
1123
fn python_load_config(mut cx: FunctionContext) -> JsResult<JsPromise> {
1224
let file_content_arg = cx.argument::<JsString>(0)?.value(&mut cx);
1325
let options_arg = cx.argument::<JsObject>(1)?;
@@ -21,14 +33,7 @@ fn python_load_config(mut cx: FunctionContext) -> JsResult<JsPromise> {
2133
py_runtime_init(&mut cx, channel.clone())?;
2234

2335
let conf_res = Python::with_gil(|py| -> PyResult<CubeConfigPy> {
24-
let sys_path = py.import("sys")?.getattr("path")?.downcast::<PyList>()?;
25-
26-
let config_dir = Path::new(&options_file_name)
27-
.parent()
28-
.unwrap_or_else(|| Path::new("."));
29-
let config_dir_str = config_dir.to_str().unwrap_or(".");
30-
31-
sys_path.insert(0, PyString::new(py, config_dir_str))?;
36+
extend_sys_path(py, &options_file_name)?;
3237

3338
let cube_code = include_str!(concat!(
3439
env!("CARGO_MANIFEST_DIR"),
@@ -71,14 +76,7 @@ fn python_load_model(mut cx: FunctionContext) -> JsResult<JsPromise> {
7176
py_runtime_init(&mut cx, channel.clone())?;
7277

7378
let conf_res = Python::with_gil(|py| -> PyResult<CubePythonModel> {
74-
let sys_path = py.import("sys")?.getattr("path")?.downcast::<PyList>()?;
75-
76-
let config_dir = Path::new(&model_file_name)
77-
.parent()
78-
.unwrap_or_else(|| Path::new("."));
79-
let config_dir_str = config_dir.to_str().unwrap_or(".");
80-
81-
sys_path.insert(0, PyString::new(py, config_dir_str))?;
79+
extend_sys_path(py, &model_file_name)?;
8280

8381
let cube_code = include_str!(concat!(
8482
env!("CARGO_MANIFEST_DIR"),

0 commit comments

Comments
 (0)