diff options
Diffstat (limited to 'gbp/scripts/config.py')
-rwxr-xr-x | gbp/scripts/config.py | 64 |
1 files changed, 53 insertions, 11 deletions
diff --git a/gbp/scripts/config.py b/gbp/scripts/config.py index 19966fe9..0ebca130 100755 --- a/gbp/scripts/config.py +++ b/gbp/scripts/config.py @@ -22,49 +22,88 @@ import sys import os, os.path from gbp.config import (GbpOptionParser, GbpOptionGroup) from gbp.errors import GbpError +from gbp.scripts.supercommand import import_command import gbp.log -def parse_args(argv): + +def build_parser(name): try: - parser = GbpOptionParser(command=os.path.basename(argv[0]), prefix='', + parser = GbpOptionParser(command=os.path.basename(name), prefix='', usage='%prog [options] - display configuration settings') except ConfigParser.ParsingError as err: gbp.log.err(err) - return None, None + return None parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False, help="verbose command execution") parser.add_config_file_option(option_name="color", dest="color", type='tristate') parser.add_config_file_option(option_name="color-scheme", dest="color_scheme") + return parser + + +def parse_args(argv): + parser = build_parser(argv[0]) + if not parser: + return None, None return parser.parse_args(argv) -def parse_config(command): +def parse_cmd_config(command): + """Make a command parse it's config files""" parser = GbpOptionParser(command) parser.parse_config_files() return parser -def print_single_value(query, printer): +def print_cmd_single_value(query, printer): + """Print a single configuration value of a command + + @param query: the cmd to print the value for + @param printer: the printer to output the value + """ try: cmd, option = query.split('.') except ValueError: return 2 - parser = parse_config(cmd) + parser = parse_cmd_config(cmd) value = parser.get_config_file_value(option) - printer(value) + printer("%s=%s" % (query, value)) return 0 if value else 1 -def single_value_printer(value): +def print_cmd_all_values(cmd, printer): + """ + Print all configuration values of a command + + @param cmd: the cmd to print the values for + @param printer: the printer to output the values + """ + if not cmd: + return 2 + try: + # Populae the parset to get a list of + # valid options + module = import_command(cmd) + parser = module.build_parser(cmd) + except (AttributeError, ImportError): + return 2 + + for option in parser.valid_options: + value = parser.get_config_file_value(option) + if value != '': + printer("%s.%s=%s" % (cmd, option, value)) + return 0 + + +def value_printer(value): if (value): print(value) def main(argv): - retval = 0 + retval = 1 (options, args) = parse_args(argv) gbp.log.setup(options.color, options.verbose, options.color_scheme) @@ -73,12 +112,15 @@ def main(argv): gbp.log.error("No command given") return 2 elif len(args) != 2: - gbp.log.error("Can only print a single value") + gbp.log.error("Can only take a single argument") return 2 else: query = args[1] - retval = print_single_value(query, single_value_printer) + if '.' in query: + retval = print_cmd_single_value(query, value_printer) + else: + retval = print_cmd_all_values(query, value_printer) return retval if __name__ == '__main__': |