From 65fcb91bd0afe322bcd38ca24ec852841594edb3 Mon Sep 17 00:00:00 2001 From: ldericher Date: Mon, 10 Aug 2020 15:21:39 +0200 Subject: [PATCH] Parser singleton --- src/kiwi-config.py | 2 +- src/kiwi/core.py | 48 ++++++++++++++++++++---------------- src/kiwi/subcommands/init.py | 4 +-- src/kiwi/subcommands/logs.py | 2 +- src/kiwi/subcommands/show.py | 2 +- 5 files changed, 32 insertions(+), 26 deletions(-) diff --git a/src/kiwi-config.py b/src/kiwi-config.py index 3e2c589..61fa52a 100755 --- a/src/kiwi-config.py +++ b/src/kiwi-config.py @@ -30,7 +30,7 @@ def main(): for cmd in commands: cmd.setup() - args = kiwi.Parser.get_args() + args = kiwi.Parser().get_args() log_handler = logging.StreamHandler() logging.getLogger().addHandler(log_handler) diff --git a/src/kiwi/core.py b/src/kiwi/core.py index 8de4feb..86ccff6 100644 --- a/src/kiwi/core.py +++ b/src/kiwi/core.py @@ -9,34 +9,40 @@ KIWI_CONF_NAME = os.getenv('KIWI_CONF_NAME', "kiwi.yml") class Parser: - __parser = None - __subparsers = None - __args = None + class __Parser: + __parser = None + __subparsers = None + __args = None - @classmethod - def get_parser(cls): - if cls.__parser is None: - cls.__parser = argparse.ArgumentParser(description='kiwi-config') + def __init__(self): + self.__parser = argparse.ArgumentParser(description='kiwi-config') - cls.__parser.add_argument( + self.__parser.add_argument( '-v', '--verbosity', action='count', default=0 ) - return cls.__parser + self.__subparsers = self.__parser.add_subparsers() + self.__subparsers.required = True + self.__subparsers.dest = 'command' - @classmethod - def get_subparsers(cls): - if cls.__subparsers is None: - cls.__subparsers = cls.get_parser().add_subparsers() - cls.__subparsers.required = True - cls.__subparsers.dest = 'command' + def get_parser(self): + return self.__parser - return cls.__subparsers + def get_subparsers(self): + return self.__subparsers - @classmethod - def get_args(cls): - if cls.__args is None: - cls.__args = cls.get_parser().parse_args() + def get_args(self): + if self.__args is None: + self.__args = self.__parser.parse_args() - return cls.__args + return self.__args + + __instance = None + + def __init__(self): + if Parser.__instance is None: + Parser.__instance = Parser.__Parser() + + def __getattr__(self, item): + return getattr(self.__instance, item) diff --git a/src/kiwi/subcommands/init.py b/src/kiwi/subcommands/init.py index f95408e..00c6f9a 100644 --- a/src/kiwi/subcommands/init.py +++ b/src/kiwi/subcommands/init.py @@ -36,7 +36,7 @@ class InitCommand(SubCommand): @classmethod def setup(cls): - parser = Parser.get_subparsers().add_parser( + parser = Parser().get_subparsers().add_parser( cls.command, description="Create a new kiwi-config instance" ) @@ -51,7 +51,7 @@ class InitCommand(SubCommand): def run(cls): logging.info(f"Initializing kiwi-config instance in '{os.getcwd()}'") - if Parser.get_args().force and os.path.isfile(KIWI_CONF_NAME): + if Parser().get_args().force and os.path.isfile(KIWI_CONF_NAME): logging.warning(f"Overwriting existing '{KIWI_CONF_NAME}'!") config = DefaultConfig.get() else: diff --git a/src/kiwi/subcommands/logs.py b/src/kiwi/subcommands/logs.py index 5494a30..824d735 100644 --- a/src/kiwi/subcommands/logs.py +++ b/src/kiwi/subcommands/logs.py @@ -8,7 +8,7 @@ class LogsCommand(SubCommand): @classmethod def setup(cls): - parser = Parser.get_subparsers().add_parser( + _ = Parser().get_subparsers().add_parser( cls.command, description="Show logs of a project" ) diff --git a/src/kiwi/subcommands/show.py b/src/kiwi/subcommands/show.py index 0ae5eb9..30d582e 100644 --- a/src/kiwi/subcommands/show.py +++ b/src/kiwi/subcommands/show.py @@ -9,7 +9,7 @@ class ShowCommand(SubCommand): @classmethod def setup(cls): - parser = Parser.get_subparsers().add_parser( + _ = Parser().get_subparsers().add_parser( cls.command, description="Show effective kiwi.yml" )