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 .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"""
|
|
|
|
|
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-19 14:10:56 +00:00
|
|
|
def run(self, command=None, args=None):
|
2020-08-13 08:48:01 +00:00
|
|
|
"""run the desired subcommand"""
|
|
|
|
|
2020-08-19 14:10:56 +00:00
|
|
|
if args is None:
|
|
|
|
args = Parser().get_args()
|
2020-08-11 12:03:00 +00:00
|
|
|
|
2020-08-17 08:55:08 +00:00
|
|
|
if command is None:
|
|
|
|
command = args.command
|
|
|
|
|
2020-08-10 13:48:15 +00:00
|
|
|
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-19 14:10:56 +00:00
|
|
|
result = cmd.run(self, args)
|
2020-08-13 12:26:49 +00:00
|
|
|
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
print()
|
|
|
|
logging.warning(f"'{cmd}' aborted, inputs may have been discarded.")
|
2020-08-17 13:00:05 +00:00
|
|
|
result = False
|
2020-08-13 12:26:49 +00:00
|
|
|
|
2020-08-17 13:00:05 +00:00
|
|
|
return result
|
2020-08-10 13:48:15 +00:00
|
|
|
|
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")
|
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)
|