44 lines
1.4 KiB
Python
44 lines
1.4 KiB
Python
from Lib.Json import json_decode
|
|
from Lib.Yaml import yaml_decode
|
|
import os
|
|
|
|
class LoadConf(dict):
|
|
"""
|
|
Load the configuration file from the configuration directory.
|
|
"""
|
|
def __init__(self, name: str):
|
|
home = '%s%s%s' % (os.path.dirname(os.path.dirname(__file__)), os.sep, 'Conf')
|
|
try:
|
|
os.path.exists(home) or os.makedirs(home)
|
|
except FileExistsError:
|
|
pass
|
|
json_extension = '.json'
|
|
yaml_extension = '.yaml'
|
|
pa_json = '%s/%s%s' % (home, name, json_extension)
|
|
pa_yaml = '%s/%s%s' % (home, name, yaml_extension)
|
|
data = None
|
|
if os.path.exists(pa_json):
|
|
file = pa_json
|
|
data = json_decode(open(file, encoding='utf-8').read())
|
|
if os.path.exists(pa_yaml):
|
|
file = pa_yaml
|
|
data = yaml_decode(open(file, encoding='utf-8').read())
|
|
if data is None:
|
|
raise FileNotFoundError('Not found json or yaml configuration "%s" in %s.' % (name, home))
|
|
super().__init__(data)
|
|
|
|
def __setattr__(self, key, value):
|
|
raise AttributeError('Attribute is read only.')
|
|
|
|
def __getattr__(self, item):
|
|
try:
|
|
return super().__getitem__(item)
|
|
except KeyError:
|
|
return None
|
|
|
|
def __setitem__(self, key, value):
|
|
return self.__setattr__(key, value)
|
|
|
|
def __getitem__(self, item):
|
|
return self.__getattr__(item)
|