-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfig.py
70 lines (61 loc) · 2.13 KB
/
config.py
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
# Copyright 2024 The Lynx Authors. All rights reserved.
# Licensed under the Apache License Version 2.0 that can be found in the
# LICENSE file in the root directory of this source tree.
import os
from utils.merge_request import MergeRequest
import subprocess
import sys
from checkers.default_config import checker_default_config
class Config:
data = {}
@staticmethod
def init():
# merge checker default config
Config.data["checker-config"] = checker_default_config
Config.data["external_checker_path"] = None
# merge custom config
mr = MergeRequest()
root_dir = mr.GetRootDirectory()
config_path = os.path.join(root_dir, ".tools_shared")
if os.path.exists(config_path):
try:
import yaml
except ImportError:
subprocess.check_call(
[sys.executable, "-m", "pip", "install", "PyYAML~=6.0"]
)
import yaml
with open(config_path, encoding="utf-8") as config_f:
Config.data = Config.merge(
Config.data, yaml.load(config_f, Loader=yaml.FullLoader)
)
@staticmethod
def merge(target, source):
for key, value in source.items():
if key not in target:
raise KeyError(f"Key not found: {key}")
if isinstance(value, dict) and isinstance(target[key], dict):
target[key] = Config.merge(target[key], value)
else:
target[key] = value
return target
@staticmethod
def get(key):
return Config.data.get(key)
@staticmethod
def value(*args):
target_obj = Config.data
index = 1
for arg in args:
if arg in target_obj:
if index == len(args):
return target_obj[arg]
else:
index += 1
target_obj = target_obj[arg]
else:
return None
if __name__ == "__main__":
Config.init()
# print(Config.value("checker-config", "file-type-checker"))
print(Config.data)