Skip to content

Commit

Permalink
Add execution of python functions
Browse files Browse the repository at this point in the history
  • Loading branch information
dloez committed Jul 7, 2023
1 parent d97186f commit 9812ce6
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 4 deletions.
61 changes: 61 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition = "2021"

[dependencies]
clap = "4.3.11"
duct = "0.13.6"
glob = "0.3.1"
regex = "1.9.1"
strum = "0.25"
Expand Down
8 changes: 5 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use glob::glob;
use std::path::PathBuf;
use strum::IntoEnumIterator;
use strum_macros::EnumIter;
use wrapper::WrapperActions;

mod wrapper;
use wrapper::WrapperActions;

#[derive(EnumIter)]
enum SupportedExtension {
Expand Down Expand Up @@ -74,7 +74,7 @@ fn main() {
let matches = Command::new("Arcanist")
.version("0.1")
.author("David L. <davidlopez.hellin@outlook.com>")
.about("A 'Mage' like inspired function runner with multi-language support")
.about("A 'Mage' inspired function runner with multi-language support")
.arg(arg!([function_name] "Function to call").required(true))
.get_matches();

Expand All @@ -83,6 +83,8 @@ fn main() {
.get_one::<String>("function_name")
.expect("required");
for wrapper in wrappers {
println!("{:?}", wrapper.does_function_exists(function_name));
if wrapper.does_function_exists(function_name) {
wrapper.call_function(function_name);
};
}
}
14 changes: 14 additions & 0 deletions src/wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub enum WrapperKind {

trait WrapperKindActions {
fn does_function_exists(&self, spec_file_path: &PathBuf, function_name: &str) -> bool;
fn call_function(&self, spec_file_path: &PathBuf, function_name: &str);
}

impl WrapperKindActions for WrapperKind {
Expand All @@ -24,11 +25,19 @@ impl WrapperKindActions for WrapperKind {
WrapperKind::Python => python::does_function_exists(spec_file_path, function_name),
}
}

fn call_function(&self, spec_file_path: &PathBuf, function_name: &str) {
match self {
WrapperKind::Yaml => todo!(),
WrapperKind::Python => python::call_function(spec_file_path, function_name),
}
}
}

pub trait WrapperActions {
fn print_spec_file_path(&self);
fn does_function_exists(&self, function_name: &str) -> bool;
fn call_function(&self, function_name: &str);
}

impl WrapperActions for Wrapper {
Expand All @@ -40,4 +49,9 @@ impl WrapperActions for Wrapper {
self.wrapper_kind
.does_function_exists(&self.spec_file_path, function_name)
}

fn call_function(&self, function_name: &str) {
self.wrapper_kind
.call_function(&self.spec_file_path, function_name)
}
}
36 changes: 35 additions & 1 deletion src/wrapper/python.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use std::{fs, path::PathBuf};
use duct::cmd;
use std::fs;
use std::io::BufRead;
use std::io::BufReader;
use std::path::PathBuf;

use regex::Regex;

Expand All @@ -12,3 +16,33 @@ pub fn does_function_exists(spec_file_path: &PathBuf, function_name: &str) -> bo
let regex = Regex::new(&regex_pattern).expect("Failed to define regex");
regex.is_match(&file_content)
}

pub fn call_function(spec_file_path: &PathBuf, function_name: &str) {
let python_code = "\
import importlib.util; \
spec = importlib.util.spec_from_file_location('main', '{{spec_file_path}}'); \
module = importlib.util.module_from_spec(spec); \
spec.loader.exec_module(module); \
module.{{function_name}}() \
";
let python_code = python_code
.replace(
"{{spec_file_path}}",
spec_file_path
.to_str()
.expect("Could not get str from pathbuf"),
)
.replace("{{function_name}}", function_name);

let shell_cmd = cmd!("python", "-c", python_code);
let reader = shell_cmd
.stderr_to_stdout()
.stderr_capture()
.reader()
.expect("failed to redirect stderr to stdout");

let lines = BufReader::new(reader).lines();
for line in lines {
println!("{}", line.unwrap());
}
}

0 comments on commit 9812ce6

Please sign in to comment.