1use ndata::dataobject::DataObject;
2use crate::code::CodeException;
3use crate::DataStore;
4
5#[cfg(not(feature="python_runtime"))]
6use ndata::dataarray::DataArray;
7#[cfg(not(feature="python_runtime"))]
8use crate::flowlang::system::system_call::system_call;
9
10#[cfg(feature="python_runtime")]
11use crate::pyenv::*;
12
13#[derive(Debug)]
14pub struct PyCmd {
15 #[cfg(not(feature="python_runtime"))]
16 path:String,
17 #[cfg(feature="python_runtime")]
18 lib:String,
19 #[cfg(feature="python_runtime")]
20 id:String,
21}
22
23impl PyCmd{
24 pub fn new(lib:&str, id:&str) -> PyCmd{
25 PyCmd{
26 #[cfg(not(feature="python_runtime"))]
27 path: PyCmd::get_path(lib,id),
28 #[cfg(feature="python_runtime")]
29 lib:lib.to_string(),
30 #[cfg(feature="python_runtime")]
31 id:id.to_string(),
32 }
33 }
34 pub fn execute(&self, args:DataObject) -> Result<DataObject, CodeException> {
35 #[cfg(not(feature="python_runtime"))]
36 {
37 let mut a = DataArray::new();
38 a.push_string("python");
39 a.push_string(&self.path);
40 a.push_string(&args.to_string());
41 let o = system_call(a);
42 let err = o.get_string("err");
43 if &err == "" {
44 let out = o.get_string("out");
45 let mut jo = DataObject::new();
46 jo.put_string("msg", &out);
47 return Ok(jo);
48 }
49 else {
50 let mut jo = DataObject::new();
51 jo.put_string("status", "err");
52 jo.put_string("msg", &err);
53 return Ok(jo);
54 }
55 }
56 #[cfg(feature="python_runtime")]
57 Ok(dopy(&self.lib, &self.id, args))
58 }
59
60 pub fn get_path(lib:&str, id:&str) -> String {
61 let store = DataStore::new();
62 let data = store.get_data(lib, id);
63 let data = data.get_object("data");
64 let data = data.get_string("python");
65 let data = store.get_data(lib, &data);
66 let data = data.get_object("data");
67 let ctl = data.get_string("ctl");
68 let cmd = data.get_string("cmd");
69 let root = store.get_lib_root(&lib);
70 let filename = cmd.clone()+".py";
71 let path = root.join("src").join(&lib).join(&ctl).join(&filename);
72 path.display().to_string()
73 }
74}