forked from microsoft/azure-devops-python-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
64 lines (50 loc) · 1.88 KB
/
config.py
File metadata and controls
64 lines (50 loc) · 1.88 KB
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
import json
import os
import pathlib
import sys
from utils import emit
DEFAULT_CONFIG_FILE_NAME = "azure-devops-runner-config.json"
OLD_DEFAULT_CONFIG_FILE_NAME = "vsts-runner-config.json"
CONFIG_KEYS = [
'url',
'pat',
]
class Config():
def __init__(self, filename=None):
if not filename:
runner_path = (pathlib.Path(os.getcwd()) / pathlib.Path(sys.argv[0])).resolve()
filename = runner_path.parents[0] / pathlib.Path(DEFAULT_CONFIG_FILE_NAME)
self._filename = filename
try:
with open(filename) as config_fp:
self._config = json.load(config_fp)
except FileNotFoundError:
emit("warning: no config file found.")
emit("The default filename has changed. You may need to rename")
emit(OLD_DEFAULT_CONFIG_FILE_NAME)
emit("to")
emit(DEFAULT_CONFIG_FILE_NAME)
self._config = {}
except json.JSONDecodeError:
emit("possible bug: config file exists but isn't parseable")
self._config = {}
def __getitem__(self, name):
self._check_if_name_valid(name)
return self._config.get(name, None)
def __setitem__(self, name, value):
self._check_if_name_valid(name)
self._config[name] = value
def __delitem__(self, name):
self._check_if_name_valid(name)
self._config.pop(name, None)
def __len__(self):
return len(CONFIG_KEYS)
def __iter__(self):
for key in CONFIG_KEYS:
yield key
def save(self):
with open(self._filename, 'w') as config_fp:
json.dump(self._config, config_fp, sort_keys=True, indent=4)
def _check_if_name_valid(self, name):
if name not in CONFIG_KEYS:
raise KeyError("{0} is not a valid config key".format(name))