File: user_config.py

package info (click to toggle)
python-stestr 4.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 868 kB
  • sloc: python: 6,348; makefile: 37; sh: 12
file content (114 lines) | stat: -rw-r--r-- 3,836 bytes parent folder | download | duplicates (2)
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
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import os
import sys

import voluptuous as vp
import yaml


def get_user_config(path=None):
    if not path:
        home_dir = os.path.expanduser("~")
        path = os.path.join(home_dir, ".stestr.yaml")
        if not os.path.isfile(path):
            path = os.path.join(os.path.join(home_dir, ".config"), "stestr.yaml")
            if not os.path.isfile(path):
                path = None
        if not path:
            return None
    else:
        if not os.path.isfile(path):
            msg = "The specified stestr user config is not a valid path"
            sys.exit(msg)

    return UserConfig(path)


class UserConfig:
    def __init__(self, path):
        self.schema = vp.Schema(
            {
                vp.Optional("run"): {
                    vp.Optional("concurrency"): int,
                    vp.Optional("random"): bool,
                    vp.Optional("no-subunit-trace"): bool,
                    vp.Optional("color"): bool,
                    vp.Optional("abbreviate"): bool,
                    vp.Optional("slowest"): bool,
                    vp.Optional("suppress-attachments"): bool,
                    vp.Optional("all-attachments"): bool,
                },
                vp.Optional("failing"): {
                    vp.Optional("list"): bool,
                },
                vp.Optional("last"): {
                    vp.Optional("no-subunit-trace"): bool,
                    vp.Optional("color"): bool,
                    vp.Optional("suppress-attachments"): bool,
                    vp.Optional("all-attachments"): bool,
                },
                vp.Optional("load"): {
                    vp.Optional("force-init"): bool,
                    vp.Optional("subunit-trace"): bool,
                    vp.Optional("color"): bool,
                    vp.Optional("abbreviate"): bool,
                    vp.Optional("suppress-attachments"): bool,
                    vp.Optional("all-attachments"): bool,
                },
                vp.Optional("history-list"): {
                    vp.Optional("show-metadata"): bool,
                },
                vp.Optional("history-show"): {
                    vp.Optional("no-subunit-trace"): bool,
                    vp.Optional("color"): bool,
                    vp.Optional("suppress-attachments"): bool,
                    vp.Optional("all-attachments"): bool,
                },
            }
        )
        with open(path) as fd:
            self.config = yaml.safe_load(fd.read())
        if self.config is None:
            self.config = {}
        try:
            self.schema(self.config)
        except vp.MultipleInvalid as e:
            msg = "Provided user config file {} is invalid " "because:\n{}".format(
                path, str(e)
            )
            sys.exit(msg)

    @property
    def run(self):
        return self.config.get("run")

    @property
    def failing(self):
        return self.config.get("failing")

    @property
    def last(self):
        return self.config.get("last")

    @property
    def load(self):
        return self.config.get("load")

    @property
    def history_list(self):
        return self.config.get("history-list")

    @property
    def history_show(self):
        return self.config.get("history-show")