64 lines
1.5 KiB
Python
64 lines
1.5 KiB
Python
import os
|
|
import json
|
|
import re
|
|
|
|
def dict_merge(dct, merge_dct):
|
|
""" Recursive dict merge. Inspired by :meth:``dict.update()``, instead of
|
|
updating only top-level keys, dict_merge recurses down into dicts nested
|
|
to an arbitrary depth, updating keys. The ``merge_dct`` is merged into
|
|
``dct``.
|
|
:param dct: dict onto which the merge is executed
|
|
:param merge_dct: dct merged into dct
|
|
:return: None
|
|
"""
|
|
for k, v in merge_dct.items():
|
|
if (k in dct and isinstance(dct[k], dict) and isinstance(merge_dct[k], dict)): #noqa
|
|
dict_merge(dct[k], merge_dct[k])
|
|
else:
|
|
dct[k] = merge_dct[k]
|
|
return dct
|
|
|
|
def read_config():
|
|
path = os.path.normpath(os.getcwd())
|
|
res = {}
|
|
file = None
|
|
while file == None:
|
|
try:
|
|
config_path = os.path.join(path, 'config', 'default.json')
|
|
file = open(config_path, 'r')
|
|
break
|
|
except:
|
|
before = path
|
|
path = os.path.normpath(os.path.join(path, '..'))
|
|
if before == path:
|
|
break
|
|
|
|
if file is None:
|
|
return res
|
|
|
|
res['path'] = path
|
|
|
|
data = json.load(file)
|
|
file.close()
|
|
dict_merge(res, data)
|
|
|
|
try:
|
|
config_path = os.path.join(path, 'config', 'local.json')
|
|
file = open(config_path, 'r')
|
|
except:
|
|
file = None
|
|
|
|
if file is None:
|
|
return res
|
|
|
|
data = json.load(file)
|
|
file.close()
|
|
dict_merge(res, data)
|
|
|
|
return res
|
|
|
|
def merge_config_path(config_path, path):
|
|
if path[0] == '/':
|
|
return os.path.normpath(path)
|
|
return os.path.normpath(os.path.join(config_path, path))
|
|
|