1+ import argparse
2+ from pathlib import Path
3+ import json
14import subprocess
5+ import os
6+ from enum import auto , StrEnum
27
3- def run_subprocess (module_name ):
8+ from shutil import copyfile
9+
10+ SCRIPT_PATH = Path (os .path .abspath (__file__ ))
11+ CFG_PATH = SCRIPT_PATH .parent / "startup.json"
12+
13+ class Commands (StrEnum ):
14+ RUN = auto ()
15+ CFG = auto ()
16+
17+ def run_subprocess (module_name , opt_args = None , python = True ):
418 """
519 Runs a Python script as a subprocess.
620
721 :param module_name: Name of the module to run.
822 :type module_name: str
9- :param args: List of command-line arguments to pass to the script (optional).
10- :type args: list, optional
23+ :param opt_args: List of command-line arguments to pass to the script (optional).
24+ :type opt_args: list, optional
25+ :param python: Wether or not use python to execute the script
26+ :type python: bool, optional
1127 :return: None
1228 """
13- cmd = ["python" , module_name ]
29+ if python :
30+ cmd = ["python" , module_name ]
31+ else :
32+ cmd = [module_name ]
33+ if opt_args :
34+ cmd += opt_args
1435 result = subprocess .run (cmd , capture_output = True , text = True )
1536
1637 print (f"\n ===== { module_name } Output =====\n " )
@@ -20,20 +41,103 @@ def run_subprocess(module_name):
2041 print (f"\n ===== { module_name } Errors =====\n " )
2142 print (result .stderr )
2243
44+
45+ def load_json_config (json_path ):
46+ """Load argument settings from a JSON file."""
47+ with open (json_path , 'r' ) as f :
48+ return json .load (f )
49+
50+
51+ def json_to_arg_list (config ):
52+ """Convert JSON config to a list mimicking CLI arguments."""
53+ arg_list = []
54+ for key , value in config .items ():
55+ key_arg = f"--{ key } " # Convert to argparse format
56+ if isinstance (value , list ): # Handle list arguments
57+ arg_list .extend ([key_arg ] + [str (v ) for v in value ])
58+ elif isinstance (value , bool ): # Handle boolean flags
59+ if value :
60+ arg_list .append (key_arg )
61+ else : # Normal key-value pairs
62+ arg_list .extend ([key_arg , str (value )])
63+ return arg_list
64+
65+
66+ def build_main_argparser () -> argparse .ArgumentParser :
67+ main_parser = argparse .ArgumentParser (description = "Mermad runs." )
68+ subparsers = main_parser .add_subparsers (
69+ title = "Commands" ,
70+ description = "Available commands" ,
71+ help = "Description" ,
72+ dest = "command" ,
73+ required = True
74+ )
75+ subparsers .required = True
76+
77+ run_parser = subparsers .add_parser (
78+ Commands .RUN ,
79+ help = "Run mermad pipeline"
80+ )
81+
82+ run_parser .add_argument (
83+ "-c" , "--config" ,
84+ type = Path ,
85+ default = CFG_PATH ,
86+ help = "Path to the configuration file"
87+ )
88+
89+ cfg_parser = subparsers .add_parser (
90+ Commands .CFG ,
91+ help = "Output a configuration file"
92+ )
93+
94+ cfg_parser .add_argument (
95+ "out_location" ,
96+ type = Path ,
97+ help = "Path to the configuration file"
98+ )
99+
100+ return main_parser
101+
102+ def exec_cfg (args ):
103+ copyfile (CFG_PATH , args .out_location )
104+
105+ def exec_run (args ):
106+ cfg = load_json_config (args .config )
107+ kgwizard_args = [
108+ "transform" ,
109+ cfg ["json_dir" ],
110+ "--output_dir" , cfg ["json_dir" ] + "/results/" ,
111+ "--output_file" , cfg ["graph_dir" ] + f"/{ cfg ["kgwizard" ]["graph_name" ]} .graphml" ,
112+ ]
113+ kgwizard_args += json_to_arg_list (cfg ["kgwizard" ])
114+
115+
116+
117+ # print("\n### Running VisualHeist ###\n")
118+ # run_subprocess("scripts/run_visualheist.py")
119+
120+ # print("\n### Running DataRaider ###\n")
121+ # run_subprocess("scripts/run_dataraider.py")
122+
123+ print ("\n ### Running KGWizard ###\n " )
124+ run_subprocess ("kgwizard" , kgwizard_args , python = False )
125+
126+
23127def main ():
24128 """
25129 Runs VisualHeist, DataRaider and KGWizard sequentially.
26130
27131 :return: None
28132 """
29- print ("\n ### Running VisualHeist ###\n " )
30- run_subprocess ("scripts/run_visualheist.py" )
31-
32- print ("\n ### Running DataRaider ###\n " )
33- run_subprocess ("scripts/run_dataraider.py" )
133+ parser = build_main_argparser ()
134+ args = parser .parse_args ()
34135
35- # print("\n### Running KGWizard ###\n")
36- # run_subprocess("scripts/run_kgwizard.py")
136+ match args .command :
137+ case Commands .RUN :
138+ exec_run (args )
139+ case Commands .CFG :
140+ exec_cfg (args )
37141
38142if __name__ == "__main__" :
39143 main ()
0 commit comments