Commit 971b6cde authored by whooie's avatar whooie
Browse files

make _perform_actions available for other commands and add better handling of...

make _perform_actions available for other commands and add better handling of command-line args for modularity
parent 82ea1796
......@@ -37,39 +37,47 @@ class Controller:
print(helptext)
sys.exit(exit)
def cmd_run(self, *args):
run_actions = self._get_diritems("run_")
def _perform_actions(self, prefix: str, args):
actions = self._get_diritems(prefix)
print(
f"[control] Found {len(run_actions.keys())} actions for RUN:\n\t"
+ ", ".join(name for name in run_actions.keys())
f"[control] Found {len(actions.keys())} actions"
+ (
":\n\t"
+ ", ".join(name for name in actions.keys())
) if len(actions.keys()) > 0 else ""
)
try:
self.precmd(*args)
except NotImplementedError:
print("[control] no pre-command defined")
threads = [
(name, threading.Thread(target=run_fn, args=args))
for name, run_fn in run_actions.items()
(name, threading.Thread(target=fn, args=args))
for name, fn in actions.items()
]
for name, thread in threads:
print(f"[control] `{name}` started")
print(f"[control] action `{name}` started")
thread.start()
while len(threads) > 0:
for k, (name, thread) in enumerate(threads):
for name, thread in threads:
if not thread.is_alive():
thread.join()
print(f"[control] `{name}` finished")
threads.pop(k)
print(f"[control] action `{name}` finished")
threads.remove((name, thread))
time.sleep(1)
try:
self.postcmd(*args)
except NotImplementedError:
print("[control] no post-command defined")
def RUN(self):
def cmd_run(self, *args):
self._perform_actions("run_", args)
def RUN(self, argv=None):
argv = sys.argv[1:] if argv is None else argv
command_handler = self._get_diritems("cmd_")
if len(sys.argv) <= 1 or sys.argv[1] not in command_handler.keys():
if len(argv) < 1 or argv[0] not in command_handler.keys():
self.cmd_help(exit=1)
else:
command_handler[sys.argv[1]](*sys.argv[2:])
self.cmd = argv[0]
command_handler[argv[0]](*argv[1:])
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment