kiwi-scp/src/kiwi/runner.py

62 lines
1.6 KiB
Python
Raw Normal View History

2020-08-12 15:46:50 +00:00
# system
2020-08-11 12:03:00 +00:00
import logging
2020-08-12 15:46:50 +00:00
# local
from . import subcommands
2020-08-11 12:03:00 +00:00
from .config import LoadedConfig
from .parser import Parser
class Runner:
2020-08-13 08:48:01 +00:00
"""Singleton: Subcommands setup and run"""
class __Runner:
2020-08-13 08:48:01 +00:00
"""Singleton type"""
__parser = None
__commands = []
def __init__(self):
2020-08-13 08:48:01 +00:00
# setup all subcommands
for className in subcommands.__all__:
cmd = getattr(subcommands, className)
self.__commands.append(cmd())
2020-08-17 08:55:08 +00:00
def run(self, command=None):
2020-08-13 08:48:01 +00:00
"""run the desired subcommand"""
2020-08-11 12:03:00 +00:00
args = Parser().get_args()
2020-08-17 08:55:08 +00:00
if command is None:
command = args.command
for cmd in self.__commands:
2020-08-17 08:55:08 +00:00
if str(cmd) == command:
2020-08-13 08:48:01 +00:00
# command found
2020-08-11 12:03:00 +00:00
logging.debug(f"Running '{cmd}' with args: {args}")
2020-08-13 12:26:49 +00:00
try:
2020-08-17 08:57:45 +00:00
cmd.run(self, LoadedConfig.get(), args)
2020-08-13 12:26:49 +00:00
except KeyboardInterrupt:
print()
logging.warning(f"'{cmd}' aborted, inputs may have been discarded.")
return True
2020-08-13 08:48:01 +00:00
# command not found
2020-08-17 08:55:08 +00:00
logging.error(f"kiwi command '{command}' unknown")
return False
__instance = None
def __init__(self):
if Runner.__instance is None:
2020-08-13 08:48:01 +00:00
# create singleton
Runner.__instance = Runner.__Runner()
def __getattr__(self, item):
2020-08-13 08:48:01 +00:00
"""Inner singleton direct access"""
return getattr(self.__instance, item)