From 69dc1fd213c467b4c9e51d3a06548b6e51d1b5e9 Mon Sep 17 00:00:00 2001 From: ldericher Date: Thu, 6 Aug 2020 14:34:25 +0200 Subject: [PATCH] Runner for subcommands --- src/kiwi-config.py | 14 ++------ src/kiwi/__init__.py | 5 ++- src/kiwi/cmd_init.py | 40 ----------------------- src/kiwi/config.py | 51 +++++++++++++++++------------- src/kiwi/core.py | 16 ---------- src/kiwi/runner.py | 24 ++++++++++++++ src/kiwi/subcommands/__init__.py | 4 +++ src/kiwi/subcommands/init.py | 48 ++++++++++++++++++++++++++++ src/kiwi/subcommands/subcommand.py | 12 +++++++ 9 files changed, 122 insertions(+), 92 deletions(-) delete mode 100644 src/kiwi/cmd_init.py create mode 100644 src/kiwi/runner.py create mode 100644 src/kiwi/subcommands/__init__.py create mode 100644 src/kiwi/subcommands/init.py create mode 100644 src/kiwi/subcommands/subcommand.py diff --git a/src/kiwi-config.py b/src/kiwi-config.py index d5dea8c..25eb590 100755 --- a/src/kiwi-config.py +++ b/src/kiwi-config.py @@ -1,18 +1,10 @@ #!/usr/bin/env python3 - import kiwi -from kiwi.cmd_init import InitSubCommand + def main(): - isc = InitSubCommand() - isc.setup() - - args = kiwi.Parser.get_args() - print(args.command) - - isc.run() - - pass + kiwi.Runner.setup_all() + kiwi.Runner.run() if __name__ == "__main__": diff --git a/src/kiwi/__init__.py b/src/kiwi/__init__.py index 06b43a2..247ace4 100644 --- a/src/kiwi/__init__.py +++ b/src/kiwi/__init__.py @@ -1,4 +1,3 @@ -from .core import Parser -from .config import Config +from .runner import Runner -__all__ = ['Parser', 'Config'] +__all__ = ['Runner'] \ No newline at end of file diff --git a/src/kiwi/cmd_init.py b/src/kiwi/cmd_init.py deleted file mode 100644 index c3a1b31..0000000 --- a/src/kiwi/cmd_init.py +++ /dev/null @@ -1,40 +0,0 @@ -import logging -import os - -from .core import KIWI_CONF_NAME, Parser, SubCommand -from .config import Config - - -class InitSubCommand(SubCommand): - def __init__(self): - super(InitSubCommand, self).__init__('init') - - def setup(self): - init_parser = Parser.get_subparsers().add_parser(str(self), help="Create new kiwi-config instance") - # init_parser.add_argument('cmd', metavar='command', type=str, help='subcommand to execute') - - def __user_input(self, config, key, prompt): - # prompt user as per argument - result = input("{} [Current: {}] ".format(prompt, config[key])).strip() - - # store result if present - if result: - config[key] = result - - def run(self): - config = Config.default() - - if os.path.isfile(KIWI_CONF_NAME): - logging.warning("Overwriting existing '%s'!", KIWI_CONF_NAME) - - self.__user_input(config, 'version', "Choose kiwi-config version") - - self.__user_input(config, 'markers:project', "Enter marker string for project directories") - self.__user_input(config, 'markers:down', "Enter marker string for disabled projects") - - self.__user_input(config, 'network:name', "Enter name for local docker network") - self.__user_input(config, 'network:cidr', "Enter CIDR block for local docker network") - - self.__user_input(config, 'runtime:storage', "Enter main directory for local data") - - print(str(config)) diff --git a/src/kiwi/config.py b/src/kiwi/config.py index fbb3c2b..7f32b09 100644 --- a/src/kiwi/config.py +++ b/src/kiwi/config.py @@ -1,5 +1,6 @@ import logging import re +import os import yaml from .core import KIWI_ROOT, KIWI_CONF_NAME @@ -13,6 +14,26 @@ DEFAULT_KIWI_CONF_NAME = KIWI_ROOT + "/default.kiwi.yml" class Config: __yml_content = {} + def __key_resolve(self, key): + # "a:b:c" => path = ['a', 'b'], key = 'c' + path = key.split(':') + path, key = path[:-1], path[-1] + + # resolve path + container = self.__yml_content + for step in path: + container = container[step] + + return container, key + + def __getitem__(self, key): + container, key = self.__key_resolve(key) + return container[key] + + def __setitem__(self, key, value): + container, key = self.__key_resolve(key) + container[key] = value + def __str__(self): # dump yml content yml_string = yaml.dump(self.__yml_content, default_flow_style=False, sort_keys=False) @@ -28,26 +49,6 @@ class Config: return yml_string - def __key_resolve(self, key): - # "a:b:c" => path = ['a', 'b'], key = 'c' - path = key.split(':') - path, key = path[:-1], path[-1] - - # resolve path - container = self.__yml_content - for step in path: - container = container[step] - - return container, key - - def __setitem__(self, key, value): - container, key = self.__key_resolve(key) - container[key] = value - - def __getitem__(self, key): - container, key = self.__key_resolve(key) - return container[key] - def __update_from_file(self, filename): with open(filename, 'r') as stream: try: @@ -55,6 +56,10 @@ class Config: except yaml.YAMLError as exc: logging.error(exc) + def __save_to_file(self, filename): + with open(filename, 'w') as stream: + stream.write(str(self)) + @classmethod def default(cls): result = cls() @@ -68,9 +73,11 @@ class Config: @classmethod def load(cls): result = cls.default() - result.__update_from_file(KIWI_CONF_NAME) + + if os.path.isfile(KIWI_CONF_NAME): + result.__update_from_file(KIWI_CONF_NAME) return result def save(self): - pass + self.__save_to_file(KIWI_CONF_NAME) diff --git a/src/kiwi/core.py b/src/kiwi/core.py index f426548..4316fcc 100644 --- a/src/kiwi/core.py +++ b/src/kiwi/core.py @@ -38,19 +38,3 @@ class Parser: cls.__args = cls.get_instance().parse_args() return cls.__args - - -class SubCommand: - __cmd = None - - def __init__(self, cmd): - self.__cmd = cmd - - def __str__(self): - return self.__cmd - - def setup(self): - pass - - def run(self): - pass diff --git a/src/kiwi/runner.py b/src/kiwi/runner.py new file mode 100644 index 0000000..ecb9e22 --- /dev/null +++ b/src/kiwi/runner.py @@ -0,0 +1,24 @@ +from typing import List + +from .core import Parser +from .subcommands import * + + +class Runner: + __commands: List[SubCommand] = [ + InitCommand + ] + + @classmethod + def setup_all(cls): + for cmd in cls.__commands: + cmd.setup() + + @classmethod + def run(cls): + args = Parser.get_args() + + for cmd in cls.__commands: + if cmd.get_cmd() == args.command: + cmd.run() + return diff --git a/src/kiwi/subcommands/__init__.py b/src/kiwi/subcommands/__init__.py new file mode 100644 index 0000000..cdf8394 --- /dev/null +++ b/src/kiwi/subcommands/__init__.py @@ -0,0 +1,4 @@ +from .subcommand import SubCommand +from .init import InitCommand + +__all__ = ['InitCommand', 'SubCommand'] \ No newline at end of file diff --git a/src/kiwi/subcommands/init.py b/src/kiwi/subcommands/init.py new file mode 100644 index 0000000..3e84936 --- /dev/null +++ b/src/kiwi/subcommands/init.py @@ -0,0 +1,48 @@ +import logging +import os + +from kiwi.core import KIWI_CONF_NAME, Parser +from kiwi.config import Config + +from .subcommand import SubCommand + + +def user_input(config, key, prompt): + # prompt user as per argument + result = input("{} [Current: {}] ".format(prompt, config[key])).strip() + + # store result if present + if result: + config[key] = result + + +class InitCommand(SubCommand): + __parser = None + + @classmethod + def get_cmd(cls): + return 'init' + + @classmethod + def setup(cls): + cls.__parser = Parser.get_subparsers().add_parser(cls.get_cmd(), help="Create new kiwi-config instance") + # cls.__parser.add_argument('cmd', metavar='command', type=str, help='subcommand to execute') + + @classmethod + def run(cls): + config = Config.default() + + if os.path.isfile(KIWI_CONF_NAME): + logging.warning("Overwriting existing '%s'!", KIWI_CONF_NAME) + + user_input(config, 'version', "Choose kiwi-config version") + + user_input(config, 'markers:project', "Enter marker string for project directories") + user_input(config, 'markers:down', "Enter marker string for disabled projects") + + user_input(config, 'network:name', "Enter name for local docker network") + user_input(config, 'network:cidr', "Enter CIDR block for local docker network") + + user_input(config, 'runtime:storage', "Enter main directory for local data") + + config.save() diff --git a/src/kiwi/subcommands/subcommand.py b/src/kiwi/subcommands/subcommand.py new file mode 100644 index 0000000..21535e5 --- /dev/null +++ b/src/kiwi/subcommands/subcommand.py @@ -0,0 +1,12 @@ +class SubCommand: + @classmethod + def get_cmd(cls): + pass + + @classmethod + def setup(cls): + pass + + @classmethod + def run(cls): + pass