Manejo de configuraciones

This commit is contained in:
2020-11-02 00:57:23 -03:00
parent 510df82520
commit 702f9e94ad

70
setup/config.py Normal file
View File

@ -0,0 +1,70 @@
import os
import json
class ConfigFile:
def __init__(self, name):
self.name = name
self._data = []
def load(self, folder):
filename = os.path.join(folder, self.name + '.json')
with open(filename, 'r') as f:
self._data = json.load(f)
def get(self, name):
if '.' not in name and name in self._data:
return self._data[name]
subs = "']['".join(name.split('.'))
substr = "self._data['" + subs + "']"
print(substr)
class Config:
def __init__(self, folder):
self.folder = folder
self._configs = {}
def load(self):
files = [f for f in os.listdir(self.folder) if os.path.isfile(os.path.join(self.folder, f)) and '.json' in f]
configs = {}
for f in files:
name = f.split('.')[0]
filename = os.path.join(self.folder, f)
with open(filename, 'r') as fh:
configs[name] = json.load(fh)
self._configs = configs
def get(self, name):
if '.' not in name and name in self._configs:
return self._configs[name]
subs = "']['".join(name.split('.'))
substr = "self._configs['" + subs + "']"
try:
sub = eval(substr)
return sub
except:
return None
def get_all(self, name_list):
output = {}
for name in name_list:
output[name] = self.get(name)
return output
def set(self, name, value):
if '.' not in name:
self._configs[name] = value
return
subs = "']['".join(name.split('.'))
substr = "self._configs['" + subs + "'] = {0}".format(value)
try:
eval(substr)
except:
pass
def load_config(folder):
config = Config(folder)
config.load()
return config