summaryrefslogtreecommitdiff
path: root/validation/validlib/config_utils.py
blob: 69ad2dea4d6a9074311d98b6b0ac2b5397356fb1 (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
# -*- coding: utf-8 -*-
import numpy
import os

def evaluate(file_path):
    file_handler = open(file_path, "r")
    file_content = file_handler.read()
    file_handler.close()
    return eval(file_content)

def run(option_parser, args, template_config_file_path):
    """Check the consistency of the arguments with the template configuration
    file and execute the command"""

    if len(args) != 2:
        option_parser._parser.error("Bad number of arguments")

    options, args = option_parser._parser.parse_args(args)
    cmd = args[0]
    if cmd not in option_parser._commands:
        option_parser._parser.error("Unknown command \"{0}\".".format(cmd))

    config_file_path = args[1]
    config = get_config(option_parser, template_config_file_path,
                        config_file_path)
    option_parser._commands[cmd](config, config_file_path)

def get_config(option_parser, template_config_file_path, config_file_path):
    """Concatenate the configurations contained in config_file and in
    its parent, recursively"""
    template_config = evaluate(template_config_file_path)
    config = {}
    parent = ("", config_file_path)
    counter = 0
    while parent[1] is not None:
        counter = counter + 1
        assert counter < 10, "Too many configuration files"
        parent = os.path.join(parent[0], parent[1])
        new_config = get_config_aux(option_parser, parent)
        new_config_parent = new_config["parent"]
        new_config.update(config)
        config.update(new_config)
        config["parent"] = new_config_parent
        parent = (os.path.dirname(parent), config["parent"])

    try:
        expected = sorted(template_config.keys())
        actual = sorted(config.keys())
        assert actual == expected, (actual, expected)
    except AssertionError as error:
        print error.args
        (actual, expected) = error.args
        message = config_file + " does not contain exactly the same " \
            "parameters as the template (" + \
            template_config_file_path + ").\n" + \
            config_file + \
            " contains the following parameters:\n" + \
            str(actual) + \
            "\n" + \
             template_config_file_path + \
             " contains the following parameters:\n" + \
             str(expected)
        option_parser._parser.error(message)
    except Exception as error:
        option_parser._parser.error("Unknown error.\n" + \
                               str(type(error)) + "\n" + \
                               str(error))
    return config

def get_config_aux(option_parser, config_file):
    print "Extracting configuration from", config_file
    try:
        config = evaluate(config_file)
        assert type(config) == dict
    except IOError:
        option_parser._parser.error(config_file + " cannot be read.")
    except SyntaxError:
        option_parser._parser.error(config_file + " cannot be evaluated.")
    except AssertionError:
        option_parser._parser.error(config_file + \
                                        " does not contain a dictionnary.")
    except Exception as error:
        print config_file
        option_parser._parser.error("Unknown error.\n" + \
                                        str(type(error)) + "\n" + \
                                        str(error))

    return config