From 720dfa7b0914d0ed7fd479926ad33dfd22956566 Mon Sep 17 00:00:00 2001 From: Martinez Date: Tue, 17 May 2016 09:52:26 +0200 Subject: [PATCH] over.app.Main now uses a features dict instead of individual flags, closes #5 --- over/app.py | 22 +++++++++++++++++----- test.py | 3 +-- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/over/app.py b/over/app.py index a904a89..bffb935 100644 --- a/over/app.py +++ b/over/app.py @@ -162,6 +162,12 @@ def expand_group(token, options): # -------------------------------------------------- class Main: + default_features = { + "use_cfg_file": False, + "auto_add_help": True, + "handle_exceptions": True + } + """ Application backbone. Provides: * A configuration system consisting of @@ -172,7 +178,7 @@ class Main: * A prettyprinting exception handler. """ - def __init__(self, name, version=None, license=None, use_cfg_file=False, auto_add_help=True, handle_exceptions=True): + def __init__(self, name, version=None, license=None, features={}): self.name = name self.version = version self.license = license @@ -181,14 +187,20 @@ class Main: self.options_by_abbr = OrderedDict() self.cfg = ConfigRouter(self.options) self.targets = [] - self.auto_add_help = auto_add_help self.invocation = cmd.format_invocation(sys.argv) + self.features = types.ndict() - if use_cfg_file: + for feature_name in self.default_features: + if feature_name in features: + self.features[feature_name] = features[feature_name] + else: + self.features[feature_name] = self.default_features[feature_name] + + if self.features.use_cfg_file: # TODO ensure it exists, update it with new values, etc. ... - if handle_exceptions: + if self.features.handle_exceptions: sys.excepthook = self.stack_tracer def __repr__(self): @@ -278,7 +290,7 @@ class Main: cmdline = cmdline or sys.argv[1:] # placing it here ensures --help is the last option in the list of options - if self.auto_add_help: + if self.features.auto_add_help: self.enable_help() last_read_option = None diff --git a/test.py b/test.py index 6b21855..5f4a700 100755 --- a/test.py +++ b/test.py @@ -28,7 +28,7 @@ def tri_callback(a, b, c): return "%s, %s and %s" %(a, b, c) if __name__ == "__main__": - main = over.app.Main("Over App Test", version.str, "LICENSE", use_cfg_file=False) + main = over.app.Main("Over App Test", version.str, "LICENSE") # name, description, callback, default=Option_sources.none, count=0, overwrite=True, abbr=None, in_cfg_file=True, show_in_help=True main.add_option("armed", "Description.", bool, True, abbr="A") main.add_option("verbose", "Description.", bool, True, abbr="v") @@ -42,5 +42,4 @@ if __name__ == "__main__": if option.name not in ["help", "-help"]: main.print("option %s<.> = %s" %(option.name, option.value)) - main.print(str(main.cfg.verbosee)) main.print("targets<.>: %s" %(main.targets))