Skip to content

Commit 820f92a

Browse files
committed
allow multiple dots
1 parent 5674670 commit 820f92a

File tree

4 files changed

+42
-16
lines changed

4 files changed

+42
-16
lines changed

guest-js/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export async function registerFunction(
5252
*/
5353
export async function registerJs(pythonFunctionCall: string, jsFunctionName?: string) {
5454
if (jsFunctionName === undefined) {
55-
jsFunctionName = pythonFunctionCall.replace(".", "_");
55+
jsFunctionName = pythonFunctionCall.replaceAll(".", "_");
5656
}
5757
call[jsFunctionName] = function (...args: any[]) { return callFunction(pythonFunctionCall, args) };
5858
}

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,13 @@ pub fn init_and_register<R: Runtime>(python_functions: Vec<&'static str>) -> Tau
112112
app.manage(python);
113113

114114
let mut dir = get_resource_dir(app);
115-
let mut code = std::fs::read_to_string(&dir.join("main.py")).unwrap_or_default();
115+
let mut code = std::fs::read_to_string(dir.join("main.py")).unwrap_or_default();
116116
if code.is_empty() {
117117
println!(
118118
"Warning: 'src-tauri/main.py' seems not to be registered in 'tauri.conf.json'"
119119
);
120120
dir = get_current_dir();
121-
code = std::fs::read_to_string(&dir.join("main.py")).unwrap_or_default();
121+
code = std::fs::read_to_string(dir.join("main.py")).unwrap_or_default();
122122
}
123123
if code.is_empty() {
124124
println!("ERROR: Error reading 'src-tauri/main.py'");

src/py_lib.rs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,25 @@ pub fn register_function_str(
4949
}
5050
rustpython_vm::Interpreter::without_stdlib(Default::default()).enter(|vm| {
5151
let var_dot_split: Vec<&str> = function_name.split(".").collect();
52-
let func = GLOBALS.globals.get_item(var_dot_split[0], vm).unwrap_or_else(|_| {
52+
let func = GLOBALS
53+
.globals
54+
.get_item(var_dot_split[0], vm)
55+
.unwrap_or_else(|_| {
5356
panic!("Cannot find '{}' in globals", var_dot_split[0]);
5457
});
55-
if var_dot_split.len() > 1 {
56-
func.get_attr(&vm.ctx.new_str(var_dot_split[1]), vm).unwrap_or_else(|_| {
57-
panic!("Cannot find sub function '{}' in '{}'", var_dot_split[1], var_dot_split[0]);
58-
});
58+
if var_dot_split.len() > 2 {
59+
func.get_attr(&vm.ctx.new_str(var_dot_split[1]), vm)
60+
.unwrap()
61+
.get_attr(&vm.ctx.new_str(var_dot_split[2]), vm)
62+
.unwrap();
63+
} else if var_dot_split.len() > 1 {
64+
func.get_attr(&vm.ctx.new_str(var_dot_split[1]), vm)
65+
.unwrap_or_else(|_| {
66+
panic!(
67+
"Cannot find sub function '{}' in '{}'",
68+
var_dot_split[1], var_dot_split[0]
69+
);
70+
});
5971
}
6072

6173
if let Some(num_args) = number_of_args {
@@ -101,7 +113,10 @@ pub fn call_function(payload: RunRequest) -> crate::Result<String> {
101113
.collect();
102114
let var_dot_split: Vec<&str> = function_name.split(".").collect();
103115
let func = GLOBALS.globals.get_item(var_dot_split[0], vm)?;
104-
Ok(if var_dot_split.len() > 1 {
116+
Ok(if var_dot_split.len() > 2 {
117+
func.get_attr(&vm.ctx.new_str(var_dot_split[1]), vm)?
118+
.get_attr(&vm.ctx.new_str(var_dot_split[2]), vm)?
119+
} else if var_dot_split.len() > 1 {
105120
func.get_attr(&vm.ctx.new_str(var_dot_split[1]), vm)?
106121
} else {
107122
func
@@ -116,7 +131,10 @@ pub fn read_variable(payload: StringRequest) -> crate::Result<String> {
116131
rustpython_vm::Interpreter::without_stdlib(Default::default()).enter(|vm| {
117132
let var_dot_split: Vec<&str> = payload.value.split(".").collect();
118133
let var = GLOBALS.globals.get_item(var_dot_split[0], vm)?;
119-
Ok(if var_dot_split.len() > 1 {
134+
Ok(if var_dot_split.len() > 2 {
135+
var.get_attr(&vm.ctx.new_str(var_dot_split[1]), vm)?
136+
.get_attr(&vm.ctx.new_str(var_dot_split[2]), vm)?
137+
} else if var_dot_split.len() > 1 {
120138
var.get_attr(&vm.ctx.new_str(var_dot_split[1]), vm)?
121139
} else {
122140
var

src/py_lib_pyo3.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ use std::{collections::HashMap, ffi::CString, sync::Mutex};
88

99
use lazy_static::lazy_static;
1010
use pyo3::exceptions::PyBaseException;
11-
use pyo3::types::{PyAnyMethods, PyDictMethods, PyList, PyListMethods};
11+
use pyo3::types::{PyAnyMethods, PyDictMethods};
1212
use pyo3::PyErr;
13-
use pyo3::{marker, types::PyDict, Py, PyAny, PyResult};
13+
use pyo3::{marker, types::PyDict, Py, PyAny};
1414

1515
use crate::{models::*, Error};
1616

@@ -45,7 +45,11 @@ pub fn register_function_str(fn_name: String, number_of_args: Option<u8>) -> cra
4545
if app.is_none() {
4646
return Err(Error::String(format!("{} not found", &fn_name)));
4747
}
48-
let app = if fn_dot_split.len() > 1 {
48+
let app = if fn_dot_split.len() > 2 {
49+
app.unwrap()
50+
.getattr(fn_dot_split.get(1).unwrap())?
51+
.getattr(fn_dot_split.get(2).unwrap())?
52+
} else if fn_dot_split.len() > 1 {
4953
app.unwrap().getattr(fn_dot_split.get(1).unwrap())?
5054
} else {
5155
app.unwrap()
@@ -103,11 +107,15 @@ pub fn read_variable(payload: StringRequest) -> crate::Result<String> {
103107
let var_dot_split: Vec<&str> = payload.value.split(".").collect();
104108
let var = globals.get_item(var_dot_split[0])?;
105109
if let Some(var) = var {
106-
if var_dot_split.len() > 1 {
107-
Ok(var.getattr(var_dot_split.get(1).unwrap())?.to_string())
110+
Ok(if var_dot_split.len() > 2 {
111+
var.getattr(var_dot_split.get(1).unwrap())?
112+
.getattr(var_dot_split.get(2).unwrap())?
113+
} else if var_dot_split.len() > 1 {
114+
var.getattr(var_dot_split.get(1).unwrap())?
108115
} else {
109-
Ok(var.to_string())
116+
var
110117
}
118+
.to_string())
111119
} else {
112120
Err(Error::String(format!("{} not set", &payload.value)))
113121
}

0 commit comments

Comments
 (0)