MCPcopy Create free account
hub / github.com/MotrixLab/AiOS / Config

Class Config

util/config.py:42–350  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

40
41
42class Config(object):
43 @staticmethod
44 def _validate_py_syntax(filename):
45 with open(filename) as f:
46 content = f.read()
47 try:
48 ast.parse(content)
49 except SyntaxError:
50 raise SyntaxError('There are syntax errors in config '
51 f'file {filename}')
52
53 @staticmethod
54 def _file2dict(filename):
55 filename = osp.abspath(osp.expanduser(filename))
56 check_file_exist(filename)
57 if filename.lower().endswith('.py'):
58 with tempfile.TemporaryDirectory() as temp_config_dir:
59 temp_config_file = tempfile.NamedTemporaryFile(
60 dir=temp_config_dir, suffix='.py')
61 temp_config_name = osp.basename(temp_config_file.name)
62 shutil.copyfile(filename,
63 osp.join(temp_config_dir, temp_config_name))
64 temp_module_name = osp.splitext(temp_config_name)[0]
65 sys.path.insert(0, temp_config_dir)
66 Config._validate_py_syntax(filename)
67 mod = import_module(temp_module_name)
68 sys.path.pop(0)
69 cfg_dict = {
70 name: value
71 for name, value in mod.__dict__.items()
72 if not name.startswith('__')
73 }
74 del sys.modules[temp_module_name]
75 temp_config_file.close()
76 else:
77 raise IOError('Only py/yml/yaml/json type are supported now!')
78
79 cfg_text = filename + '\n'
80 with open(filename, 'r') as f:
81 cfg_text += f.read()
82
83 if BASE_KEY in cfg_dict:
84 cfg_dir = osp.dirname(filename)
85 base_filename = cfg_dict.pop(BASE_KEY)
86 base_filename = base_filename if isinstance(
87 base_filename, list) else [base_filename]
88
89 cfg_dict_list = list()
90 cfg_text_list = list()
91 for f in base_filename:
92 _cfg_dict, _cfg_text = Config._file2dict(osp.join(cfg_dir, f))
93 cfg_dict_list.append(_cfg_dict)
94 cfg_text_list.append(_cfg_text)
95
96 base_cfg_dict = dict()
97 for c in cfg_dict_list:
98 if len(base_cfg_dict.keys() & c.keys()) > 0:
99 raise KeyError('Duplicate key is not allowed among bases')

Callers 4

fromfileMethod · 0.70
copyMethod · 0.70
deepcopyMethod · 0.70
config.pyFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected