diff --git a/bin/is b/bin/is index 6744d8d32011398304f88bef0bf3eb65f4cb3742..0577b3216eb5307a96ae92def81922daf19be956 100755 --- a/bin/is +++ b/bin/is @@ -459,11 +459,16 @@ def arg_parser_init(): parser = argparse.ArgumentParser() parser.add_argument("-V", "--version", action="version", version=installsystems.version) - # exclusive group on debug/quiet + # exclusive group on verbosity g = parser.add_mutually_exclusive_group() - g.add_argument("-d", "--debug", action="store_true", + g.add_argument("-v", "--verbosity", default=1, + type=int, choices=[0,1,2], + help="define verbosity level (0: quiet, 1:normal, 2:debug)") + g.add_argument("-d", "--debug", dest="verbosity", + action="store_const", const=2, help="active debug mode") - g.add_argument("-q", "--quiet", action="store_true", + g.add_argument("-q", "--quiet", dest="verbosity", + action="store_const", const=0, help="active quiet mode") # common options parser.add_argument("-c", "--config", default="installsystems", @@ -711,21 +716,19 @@ def main(): arg_parser = arg_parser_init() # first partial parsing, to get early debug and config path options = arg_parser.parse_known_args()[0] - # set early command line debug and quiet mode - installsystems.debug = options.debug - installsystems.quiet = options.quiet + # set early command line verbosity and color + installsystems.verbosity = options.verbosity installsystems.printer.NOCOLOR = options.no_color # load main config file options config_parser = MainConfigFile(options.config, "installsystems") options = config_parser.parse() # second partial parsing, command line option overwrite config file options = arg_parser.parse_known_args(namespace=options)[0] - # set debug and quiet mode - installsystems.debug = options.debug - installsystems.quiet = options.quiet + # set verbosity and color + installsystems.verbosity = options.verbosity installsystems.printer.NOCOLOR = options.no_color # no warning if we are not in debug mode - if not installsystems.debug: + if installsystems.verbosity < 2: warnings.filterwarnings("ignore") # except for install command we parse all args! # install command is responsible of parsing diff --git a/completion/bash/is b/completion/bash/is index 3a7f6e6832b9d32ea39d7dfae73db68c973860c0..ac8fe27a014455c9eb312b8043557a351a1e1c39 100644 --- a/completion/bash/is +++ b/completion/bash/is @@ -3,12 +3,12 @@ # list local repositories _local_repo() { - COMPREPLY=("${COMPREPLY[@]}" $(compgen -W "$(is --no-color --no-sync repo --local)" -- "$cur")) + COMPREPLY=("${COMPREPLY[@]}" $(compgen -W "$(is --quiet --no-color --no-sync repo --local)" -- "$cur")) } # list all defined repositories _repo() { - COMPREPLY=("${COMPREPLY[@]}" $(compgen -W "$(is --no-color --no-sync repo)" -- "$cur")) + COMPREPLY=("${COMPREPLY[@]}" $(compgen -W "$(is --quiet --no-color --no-sync repo)" -- "$cur")) } # list all images available in any online repositories @@ -44,6 +44,7 @@ _is() { 'search' 'version' 'diff' 'prepare_chroot' 'unprepare_chroot') opts=('-h' '--help' '-V' '--version' + '-v' '--verbosity' '-d' '--debug' '-q' '--quiet' '-R' '--repo-config' diff --git a/installsystems/__init__.py b/installsystems/__init__.py index 0a5601563ed878cba9bf6df33f29405f731c0528..caa330f65cf3a48fb79ad09bc819c5d929b008f6 100644 --- a/installsystems/__init__.py +++ b/installsystems/__init__.py @@ -8,7 +8,6 @@ InstallSystems module canonical_name="installsystems" version = "5" -debug = False -quiet = False +verbosity = 1 # 0: quiet, 1: normal, 2: debug __all__ = [] diff --git a/installsystems/config.py b/installsystems/config.py index 7f60869b2158981db467181e1c9f4656ae51491c..268f5b21e21ecb9043e9f06d7d6f9817ae2236d7 100644 --- a/installsystems/config.py +++ b/installsystems/config.py @@ -53,8 +53,7 @@ class MainConfigFile(ConfigFile): ''' valid_options = { - "debug": bool, - "quiet": bool, + "verbosity": [0,1,2], "no_cache": bool, "no_color": bool, "timeout": int, @@ -99,16 +98,33 @@ class MainConfigFile(ConfigFile): if option not in self.valid_options.keys(): warn("Invalid option %s in %s, skipped" % (option, self.path)) continue + # we expect a string like if not isinstance(option, basestring): raise TypeError("Invalid config parser option %s type" % option) # smartly cast option's value if self.valid_options[option] is bool: value = value.strip().lower() not in ("false", "no", "0", "") + # in case of valid option is a list, we take the type of the first + # argument of the list to convert value into it + # as a consequence, all element of a list must be of the same type! + # empty list are forbidden ! + elif isinstance(self.valid_options[option], list): + ctype = type(self.valid_options[option][0]) + try: + value = ctype(value) + except ValueError: + warn("Invalid option %s type (must be %s), skipped" % + (option, ctype)) + continue + if value not in self.valid_options[option]: + warn("Invalid value %s in option %s (must be in %s), skipped" % + (value, option, self.valid_options[option])) + continue else: try: value = self.valid_options[option](value) except ValueError: - warn("Invalid option %s type. Must be %s" % + warn("Invalid option %s type (must be %s), skipped" % (option, self.valid_options[option])) continue setattr(namespace, option, value) diff --git a/installsystems/printer.py b/installsystems/printer.py index b1077983ee44f3673c2d6dfca2304c7a25aa4784..85a8bd923d0ba3c545e5d436b70658e58347c89f 100644 --- a/installsystems/printer.py +++ b/installsystems/printer.py @@ -72,14 +72,14 @@ def err(message, fd=sys.stderr, endl=os.linesep): def fatal(message, quit=True, fd=sys.stderr, endl=os.linesep): out("#light##red#Fatal:#reset# #red#%s#reset#" % message, fd, endl) - if sys.exc_info()[0] is not None and installsystems.debug: + if sys.exc_info()[0] is not None and installsystems.verbosity > 1: raise if quit: os._exit(21) def error(message, quit=True, fd=sys.stderr, endl=os.linesep): out("#light##red#Error:#reset# #red#%s#reset#" % message, fd, endl) - if sys.exc_info()[0] is not None and installsystems.debug: + if sys.exc_info()[0] is not None and installsystems.verbosity > 1: raise if quit: exit(42) @@ -88,11 +88,11 @@ def warn(message, fd=sys.stderr, endl=os.linesep): out("#light##yellow#Warning:#reset# #yellow#%s#reset#" % message, fd, endl) def info(message, fd=sys.stderr, endl=os.linesep): - if not installsystems.quiet: + if installsystems.verbosity > 0: out("#light#Info%s:#reset# %s" % message, fd, endl) def debug(message, fd=sys.stderr, endl=os.linesep): - if installsystems.debug: + if installsystems.verbosity > 1: out("#light##black#%s#reset#" % message, fd, endl) def arrowlevel(inc=None, level=None): @@ -105,7 +105,7 @@ def arrowlevel(inc=None, level=None): return old_level def arrow(message, inclevel=None, level=None, fd=sys.stdout, endl=os.linesep): - if installsystems.quiet: + if installsystems.verbosity == 0: return # define new level old_level = arrowlevel(inc=inclevel, level=level) diff --git a/installsystems/tools.py b/installsystems/tools.py index f69588c84cbd29c54e44a48dc254afde7338cd1f..8d5a7d77f3081136656930b130b75d134d303acc 100644 --- a/installsystems/tools.py +++ b/installsystems/tools.py @@ -219,7 +219,7 @@ class PipeFile(object): ''' Set this property to true enable progress bar ''' - if installsystems.quiet is True: + if installsystems.verbosity == 0: return if val == True and not hasattr(self, "_progressbar_started"): self._progressbar_started = True diff --git a/samples/installsystems.conf b/samples/installsystems.conf index c8b5f2430a87ed667279b9d563d2bae7d7a40af5..cfc1bc32d25a8959cfed9621c2d26a01c9b6f04a 100644 --- a/samples/installsystems.conf +++ b/samples/installsystems.conf @@ -2,11 +2,8 @@ [installsystems] -# Set debug mode -#debug = True - -# Set quiet mode -#quiet = True +# Set verbosity (0: quiet, 1: normal, 2: debug) +#verbosity = 2 # define a custom cache directory #cache = /tmp/sex