kopia lustrzana https://gitlab.com/marnanel/chapeau
Config singleton class
rodzic
c098602b68
commit
c1e66efff2
|
@ -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
|
Ładowanie…
Reference in New Issue