@@ -8,6 +8,18 @@ use pyo3::prelude::*;
8
8
use pyo3:: types:: { PyDict , PyFunction , PyList , PyString , PyTuple } ;
9
9
use std:: path:: Path ;
10
10
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
+
11
23
fn python_load_config ( mut cx : FunctionContext ) -> JsResult < JsPromise > {
12
24
let file_content_arg = cx. argument :: < JsString > ( 0 ) ?. value ( & mut cx) ;
13
25
let options_arg = cx. argument :: < JsObject > ( 1 ) ?;
@@ -21,14 +33,7 @@ fn python_load_config(mut cx: FunctionContext) -> JsResult<JsPromise> {
21
33
py_runtime_init ( & mut cx, channel. clone ( ) ) ?;
22
34
23
35
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) ?;
32
37
33
38
let cube_code = include_str ! ( concat!(
34
39
env!( "CARGO_MANIFEST_DIR" ) ,
@@ -71,14 +76,7 @@ fn python_load_model(mut cx: FunctionContext) -> JsResult<JsPromise> {
71
76
py_runtime_init ( & mut cx, channel. clone ( ) ) ?;
72
77
73
78
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) ?;
82
80
83
81
let cube_code = include_str ! ( concat!(
84
82
env!( "CARGO_MANIFEST_DIR" ) ,
0 commit comments