diff --git a/src/main.rs b/src/main.rs index 8045f46..5c0b9a1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,6 @@ mod events; mod messages; mod python; mod macros; -mod python_comms; use std::env; use std::process; @@ -42,7 +41,7 @@ async fn main() { && !args.contains(&"--rs".to_string()) { println!("----- PYTHON ONLY MODE -----"); - let _ = python::start(); + let _ = python::start(args); process::exit(0); } else if args.contains(&"--rs".to_string()) @@ -54,15 +53,17 @@ async fn main() { } let rt = Runtime::new().unwrap(); + let rust_args = args.clone(); + let python_args = args.clone(); let rust = thread::spawn(move || { rt.block_on(async { - start(args).await; + start(rust_args).await; }); }); let python = thread::spawn(|| { - let _ = python::start(); + let _ = python::start(python_args); }); rust.join().unwrap(); @@ -71,6 +72,8 @@ async fn main() { async fn start(args: Vec) { + rs_println!("ARGS: {:?}", &args[1..]); + let data = gen_data(args); let mut bot = gen_bot(data).await; diff --git a/src/python.rs b/src/python.rs index 15ebd8f..f55dc4a 100644 --- a/src/python.rs +++ b/src/python.rs @@ -8,22 +8,18 @@ use pyo3::prelude::*; use pyo3::types::PyList; -pub fn start() -> PyResult<()> { +pub fn start(args: Vec) -> PyResult<()> { rs_println!("Running Python program..."); let path = concat!(env!("CARGO_MANIFEST_DIR"), "\\src\\python"); let code = get_code(&(path.to_owned() + "\\main.py")); - let app_path = CString::new(code).unwrap(); + let app_path = CString::new(format!("args = {:?}\n{}", args, code)).unwrap(); pyo3::prepare_freethreaded_python(); let from_python = Python::with_gil(|py| -> PyResult> { - let syspath = py - .import("sys")? - .getattr("path")? - .downcast_into::()?; - + let syspath = py.import("sys")?.getattr("path")?.downcast_into::()?; syspath.insert(0, path)?; let empty = CString::new("").unwrap(); @@ -38,6 +34,7 @@ pub fn start() -> PyResult<()> { return Ok(()); } + fn get_code(path: &str) -> String { return fs::read_to_string(path) .expect("Failed to read Python file.") diff --git a/src/python/bot.py b/src/python/bot.py index c6ea8a0..30b8b33 100644 --- a/src/python/bot.py +++ b/src/python/bot.py @@ -2,9 +2,11 @@ from io import TextIOWrapper from praw import models import praw import os + from macros import * class Bot: + args: list[str] = ["NO_RUST", "--dev", "--py"] password: str = os.environ.get("ASSISTANT_R_PASS") secret: str = os.environ.get("ASSISTANT_R_TOKEN") @@ -22,4 +24,7 @@ class Bot: ) sr: models.Subreddit = r.subreddit("bytedicetesting") #r.subreddit("boykisser") data_f: TextIOWrapper = None - data: dict = {} \ No newline at end of file + data: dict = {} + + def set_args(self, args: list[str]): + self.args = args \ No newline at end of file diff --git a/src/python/main.py b/src/python/main.py index 87a0539..ea44e4d 100644 --- a/src/python/main.py +++ b/src/python/main.py @@ -1,14 +1,19 @@ import sys +from macros import * import bot as botPy import data -from macros import * def main(): sys.stdout.reconfigure(encoding="utf-8") bot = botPy.Bot() + try: bot.set_args(args) + except NameError: + py_print("No command args found from Rust. Don't worry though, we have backup in place.") + + py_print("ARGS:", str(bot.args[1:])) py_print("Reading data...") data.read_data(bot) diff --git a/src/python/websocket.py b/src/python/websocket.py new file mode 100644 index 0000000..e69de29 diff --git a/src/python_comms.rs b/src/python_comms.rs deleted file mode 100644 index f26a70a..0000000 --- a/src/python_comms.rs +++ /dev/null @@ -1,7 +0,0 @@ -use serde_json::Value; - -fn fetch_data() { - // tell python to update JSON - // read JSON - return; -} \ No newline at end of file diff --git a/src/websocket.rs b/src/websocket.rs new file mode 100644 index 0000000..e69de29