Completed
Push — develop ( d85607...94bf94 )
by Jace
02:17
created

main()   A

Complexity

Conditions 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1.2963

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
dl 0
loc 6
rs 9.4285
c 3
b 0
f 0
ccs 2
cts 6
cp 0.3333
crap 1.2963
1 1
import os
2 1
import argparse
3 1
import logging
4 1
try:
5 1
    import configparser  # Python 3
6
except ImportError:
7
    import ConfigParser as configparser  # Python 2
8
9
10 1
CONFIG_FILENAMES = ['.verchew', '.verchewrc', 'verchew.ini', '.verchew.ini']
11
12
13 1
log = logging.getLogger(__name__)
14
15
16 1
def main():
17 1
    args = parse_arguments()
18
    configure_logging(args.verbose)
19
    path = find_config()
20
    config = parse_config(path)
21
    return config
22
23
24 1
def parse_arguments():
25 1
    parser = argparse.ArgumentParser()
26
    # TODO: add '--version' option
27
    # parser.add_argument('-V', '--version', action='version', version=VERSION)
28 1
    parser.add_argument('-v', '--verbose', action='count', default=0,
29
                        help="enable verbose logging")
30
31 1
    return parser.parse_args()
32
33
34 1
def configure_logging(count=0):
35
    if count == 0:
36
        level = logging.WARNING
37
    elif count == 1:
38
        level = logging.INFO
39
    else:
40
        level = logging.DEBUG
41
42
    logging.basicConfig(level=level, format="%(levelname)s: %(message)s")
43
44
45 1
def find_config(root=None, config_filenames=None):
46 1
    root = root or os.getcwd()
47 1
    config_filenames = config_filenames or CONFIG_FILENAMES
48
49 1
    path = None
50 1
    log.info("Looking for config file in: %s", root)
51 1
    log.debug("Filename options: %s", ", ".join(config_filenames))
52 1
    for filename in os.listdir(root):
53 1
        if filename in config_filenames:
54 1
            path = os.path.join(root, filename)
55 1
            log.info("Found config file: %s", path)
56 1
            return path
57
58 1
    msg = "No config file found in: {0}".format(root)
59 1
    raise RuntimeError(msg)
60
61
62 1
def parse_config(path):
63 1
    data = {}
64
65 1
    log.info("Parsing config file: %s", path)
66 1
    config = configparser.ConfigParser()
67 1
    config.read(path)
68
69 1
    for section in config.sections():
70 1
        data[section] = {}
71 1
        for name, value in config.items(section):
72 1
            data[section][name] = value
73
74 1
    return data
75
76
77
if __name__ == '__main__':  # pragma: no cover
78
    main()
79