Skip to content

Commit b70ee4c

Browse files
committed
rustpython running on ios
1 parent 9fef457 commit b70ee4c

4 files changed

Lines changed: 32 additions & 37 deletions

File tree

src/desktop.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,23 @@
44
// git clone https://github.com/marcomq/tauri-plugin-python
55

66
use serde::de::DeserializeOwned;
7-
use tauri::{plugin::PluginApi, AppHandle, Runtime};
7+
use tauri::{ plugin::PluginApi, AppHandle, Runtime};
88

99
use crate::py_lib;
1010

1111
/// Access to the python plugin APIs.
1212
pub struct Python<R: Runtime>(AppHandle<R>);
1313

14+
fn read_main_py<'a>() -> String {
15+
let py_file_path = std::env::current_dir().unwrap().join("src-python").join("main.py");
16+
std::fs::read_to_string(py_file_path).unwrap_or_default()
17+
// include_str!(concat!(env!("PWD"), "/src-tauri/src-python/main.py"))
18+
}
19+
1420
pub fn init<R: Runtime, C: DeserializeOwned>(
1521
app: &AppHandle<R>,
1622
_api: PluginApi<R, C>,
1723
) -> crate::Result<Python<R>> {
18-
py_lib::init_python().unwrap();
24+
py_lib::init_python(read_main_py()).unwrap();
1925
Ok(Python(app.clone()))
2026
}

src/mobile.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55

66
use serde::de::DeserializeOwned;
77
use tauri::{
8+
path::BaseDirectory,
89
plugin::{PluginApi, PluginHandle},
9-
AppHandle, Runtime,
10+
AppHandle, Runtime, Manager
1011
};
1112

1213
use crate::models::*;
@@ -17,14 +18,18 @@ tauri::ios_plugin_binding!(init_plugin_python);
1718

1819
// initializes the Kotlin or Swift plugin classes
1920
pub fn init<R: Runtime, C: DeserializeOwned>(
20-
_app: &AppHandle<R>,
21+
app: &AppHandle<R>,
2122
api: PluginApi<R, C>,
2223
) -> crate::Result<Python<R>> {
2324
#[cfg(target_os = "android")]
2425
let handle = api.register_android_plugin("com.plugin.python.application", "ExamplePlugin")?;
2526
#[cfg(target_os = "ios")]
2627
let handle = api.register_ios_plugin(init_plugin_python)?;
27-
py_lib::init_python()?;
28+
let py_file_path = app
29+
.path()
30+
.resolve("src-python/main.py", BaseDirectory::Resource)?;
31+
let code = std::fs::read_to_string(py_file_path).expect("Error reading main.py");
32+
py_lib::init_python(code)?;
2833
Ok(Python(handle))
2934
}
3035

src/py_lib.rs

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,7 @@ lazy_static! {
2525
static ref GLOBALS: rustpython_vm::scope::Scope = create_globals();
2626
}
2727

28-
fn get_py_path() -> PathBuf {
29-
env::current_dir().unwrap().join("src-python")
30-
}
31-
32-
fn read_main_py<'a>() -> String {
33-
let py_file_path = get_py_path().join("main.py");
34-
std::fs::read_to_string(py_file_path).unwrap()
35-
// include_str!(concat!(env!("PWD"), "/src-tauri/src-python/main.py"))
36-
}
37-
38-
pub fn init_python() -> PyResult<()> {
39-
let code = read_main_py();
28+
pub fn init_python(code: String) -> crate::Result<()> {
4029
rustpython_vm::Interpreter::without_stdlib(Default::default()).enter(|vm| {
4130
let code_obj = vm
4231
.compile(
@@ -50,7 +39,7 @@ pub fn init_python() -> PyResult<()> {
5039
Ok(())
5140
}
5241

53-
pub fn run_python(payload: StringRequest) -> PyResult<()> {
42+
pub fn run_python(payload: StringRequest) -> crate::Result<()> {
5443
rustpython_vm::Interpreter::without_stdlib(Default::default()).enter(|vm| {
5544
let code_obj = vm
5645
.compile(
@@ -63,36 +52,42 @@ pub fn run_python(payload: StringRequest) -> PyResult<()> {
6352
})?;
6453
Ok(())
6554
}
66-
pub fn register_function(payload: RegisterRequest) -> PyResult<()> {
55+
pub fn register_function(payload: RegisterRequest) -> crate::Result<()> {
6756
register_function_str(payload.python_function_call, payload.number_of_args)
6857
}
6958

70-
pub fn register_function_str(fn_name: String, number_of_args: Option<u8>) -> PyResult<()> {
59+
pub fn register_function_str(fn_name: String, number_of_args: Option<u8>) -> crate::Result<()> {
7160
rustpython_vm::Interpreter::without_stdlib(Default::default()).enter(|vm| {
7261
GLOBALS.globals.get_item(&fn_name, vm).unwrap();
7362
FUNCTION_MAP.lock().unwrap().insert(fn_name);
7463
Ok(())
7564
})
7665
}
77-
pub fn call_function(payload: RunRequest) -> PyResult<String> {
78-
// TODO,
66+
pub fn call_function(payload: RunRequest) -> crate::Result<String> {
67+
let function_name = payload.function_name;
68+
if FUNCTION_MAP.lock().unwrap().get(&function_name).is_none() {
69+
Err(std::io::Error::new(
70+
std::io::ErrorKind::Other,
71+
format!("Function {function_name} has not been registered yet"),
72+
))?;
73+
}
7974
rustpython_vm::Interpreter::without_stdlib(Default::default()).enter(|vm| {
8075
let posargs: Vec<_> = payload
8176
.args
8277
.into_iter()
83-
.map(|x| py_serde::deserialize(vm, x).unwrap())
78+
.map(|value| py_serde::deserialize(vm, value).unwrap())
8479
.collect();
8580
let res = GLOBALS
8681
.globals
87-
.get_item(&payload.function_name, vm)?
82+
.get_item(&function_name, vm)?
8883
.call(posargs, vm)?
8984
.str(vm)?
9085
.to_string();
9186
Ok(res)
9287
})
9388
}
9489

95-
pub fn read_variable(payload: StringRequest) -> PyResult<String> {
90+
pub fn read_variable(payload: StringRequest) -> crate::Result<String> {
9691
rustpython_vm::Interpreter::without_stdlib(Default::default()).enter(|vm| {
9792
let res = GLOBALS
9893
.globals

src/py_lib_pyo3.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,7 @@ lazy_static! {
2323
Mutex::new(marker::Python::with_gil(|py| { PyDict::new(py).into() }));
2424
}
2525

26-
fn get_py_path() -> PathBuf {
27-
env::current_dir().unwrap().join("src-python")
28-
}
29-
30-
fn read_main_py<'a>() -> String {
31-
let py_file_path = get_py_path().join("main.py");
32-
std::fs::read_to_string(py_file_path).unwrap_or_default()
33-
// include_str!(concat!(env!("PWD"), "/src-tauri/src-python/main.py"))
34-
}
35-
36-
pub fn init_python() -> PyResult<()> {
37-
let code = read_main_py();
26+
pub fn init_python(code: String) -> crate::Result<()> {
3827
let c_code = CString::new(code).expect("error creating cstring from code");
3928
marker::Python::with_gil(|py| -> PyResult<()> {
4029
let syspath = py

0 commit comments

Comments
 (0)