summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/harness/wptrunner/config.py
blob: c0e44e1eb9512fcdd900dc30eca22925ffeeb8ad (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
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import ConfigParser
import os
import sys
from collections import OrderedDict

here = os.path.split(__file__)[0]

class ConfigDict(dict):
    def __init__(self, base_path, *args, **kwargs):
        self.base_path = base_path
        dict.__init__(self, *args, **kwargs)

    def get_path(self, key, default=None):
        if key not in self:
            return default
        path = self[key]
        os.path.expanduser(path)
        return os.path.abspath(os.path.join(self.base_path, path))

def read(config_path):
    config_path = os.path.abspath(config_path)
    config_root = os.path.split(config_path)[0]
    parser = ConfigParser.SafeConfigParser()
    success = parser.read(config_path)
    assert config_path in success, success

    subns = {"pwd": os.path.abspath(os.path.curdir)}

    rv = OrderedDict()
    for section in parser.sections():
        rv[section] = ConfigDict(config_root)
        for key in parser.options(section):
            rv[section][key] = parser.get(section, key, False, subns)

    return rv

def path(argv=None):
    if argv is None:
        argv = []
    path = None

    for i, arg in enumerate(argv):
        if arg == "--config":
            if i + 1 < len(argv):
                path = argv[i + 1]
        elif arg.startswith("--config="):
            path = arg.split("=", 1)[1]
        if path is not None:
            break

    if path is None:
        if os.path.exists("wptrunner.ini"):
            path = os.path.abspath("wptrunner.ini")
        else:
            path = os.path.join(here, "..", "wptrunner.default.ini")

    return os.path.abspath(path)

def load():
    return read(path(sys.argv))