From c1e66efff209493cdbaa2f7377f82759f52c4da7 Mon Sep 17 00:00:00 2001 From: Marnanel Thurman Date: Fri, 13 Oct 2023 02:18:56 +0100 Subject: [PATCH] Config singleton class --- kepi/config.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 kepi/config.py diff --git a/kepi/config.py b/kepi/config.py new file mode 100644 index 0000000..6299a38 --- /dev/null +++ b/kepi/config.py @@ -0,0 +1,50 @@ +import argparse + +class Config: + + settings = {} + subcommands = [] + argparser = None + + def __init__(self): + raise NotImplementedError() + + def users(self): + raise ValueError() + + def __getattr__(self, field): + if field in self.settings: + return self.settings[field] + else: + raise KeyError(field) + + def parse_args(self, argparser): + + assert self.subcommands is not None + + self.argparser = argparser + + self.subparsers = argparser.add_subparsers( + dest = 'command', + required = True, + ) + + for sub in self.subcommands: + sub(self.subparsers) + + self.subcommands = None + + args = self.argparser.parse_args() + + for field in dir(args): + if field.startswith('_'): + continue + self.settings[field] = getattr(args, field) + + # TODO here we will check the config file, and merge them in. + +config = Config.__new__(Config) + +def subcommand(fn): + config.subcommands.append(fn) + return None