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
|
2020-08-15 00:06:55 +00:00
|
|
|
from . import subcommands
|
2020-08-11 12:03:00 +00:00
|
|
|
from .config import LoadedConfig
|
|
|
|
from .parser import Parser
|
2020-08-10 13:48:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Runner:
|
2020-08-13 08:48:01 +00:00
|
|
|
"""Singleton: Subcommands setup and run"""
|
|
|
|
|
2020-08-10 13:48:15 +00:00
|
|
|
class __Runner:
|
2020-08-13 08:48:01 +00:00
|
|
|
"""Singleton type"""
|
|
|
|
|
|
|
|
__parser = None
|
2020-08-10 13:48:15 +00:00
|
|
|
__commands = []
|
|
|
|
|
|
|
|
def __init__(self):
|
2020-08-13 08:48:01 +00:00
|
|
|
# setup all subcommands
|
2020-08-15 00:06:55 +00:00
|
|
|
for className in subcommands.__all__:
|
|
|
|
cmd = getattr(subcommands, className)
|
2020-08-10 13:48:15 +00:00
|
|
|
self.__commands.append(cmd())
|
|
|
|
|
2020-08-11 12:03:00 +00:00
|
|
|
def run(self):
|
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-10 13:48:15 +00:00
|
|
|
for cmd in self.__commands:
|
2020-08-11 12:03:00 +00:00
|
|
|
if str(cmd) == args.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:
|
|
|
|
cmd.run(LoadedConfig.get(), args)
|
|
|
|
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
print()
|
|
|
|
logging.warning(f"'{cmd}' aborted, inputs may have been discarded.")
|
|
|
|
|
2020-08-10 13:48:15 +00:00
|
|
|
return True
|
|
|
|
|
2020-08-13 08:48:01 +00:00
|
|
|
# command not found
|
|
|
|
logging.error(f"kiwi command '{args.command}' unknown")
|
2020-08-10 13:48:15 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
__instance = None
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
if Runner.__instance is None:
|
2020-08-13 08:48:01 +00:00
|
|
|
# create singleton
|
2020-08-10 13:48:15 +00:00
|
|
|
Runner.__instance = Runner.__Runner()
|
|
|
|
|
|
|
|
def __getattr__(self, item):
|
2020-08-13 08:48:01 +00:00
|
|
|
"""Inner singleton direct access"""
|
|
|
|
|
2020-08-10 13:48:15 +00:00
|
|
|
return getattr(self.__instance, item)
|