Config singleton class

marnanel-wip
Marnanel Thurman 2023-10-13 02:18:56 +01:00
rodzic c098602b68
commit c1e66efff2
1 zmienionych plików z 50 dodań i 0 usunięć

50
kepi/config.py 100644
Wyświetl plik

@ -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