summaryrefslogtreecommitdiff
path: root/gbp/scripts/config.py
blob: c914e7dac201502690f98cb9886136f8078860ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# vim: set fileencoding=utf-8 :
#
# (C) 2014 Guido Guenther <agx@sigxcpu.org>
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
"""Query and display config file values"""

import configparser
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 build_parser(name):
    try:
        parser = GbpOptionParser(command=os.path.basename(name), prefix='',
                             usage='%prog [options] command[.optionname] - display configuration settings')
    except configparser.ParsingError as err:
        gbp.log.err(err)
        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_cmd_config(command):
    """Make a command parse it's config files"""
    parser = GbpOptionParser(command)
    parser.parse_config_files()
    return parser


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_cmd_config(cmd)
    value = parser.get_config_file_value(option)
    printer("%s=%s" % (query, value))
    return 0 if value else 1


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 = 1

    (options, args) = parse_args(argv)
    gbp.log.setup(options.color, options.verbose, options.color_scheme)

    if not args:
        gbp.log.error("No command given")
        return 2
    elif len(args) != 2:
        gbp.log.error("Can only take a command or command.optionname, check --help")
        return 2
    else:
        query = args[1]

    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__':
    sys.exit(main(sys.argv))

# vim:et:ts=4:sw=4:et:sts=4:ai:set list listchars=tab\:»·,trail\:·: