@@ -8,6 +8,18 @@ use pyo3::prelude::*;
88use pyo3:: types:: { PyDict , PyFunction , PyList , PyString , PyTuple } ;
99use 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+
1123fn 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