From a798f29b73ad7779820bd9a81e14cb4fc54457f0 Mon Sep 17 00:00:00 2001 From: Emanuele Date: Sun, 10 Nov 2019 12:49:49 +0100 Subject: [PATCH] Prima versione gestore configurazioni --- bananaSPLIT/libbabanasplit/libconfload.py | 53 ++++++++++++++++++++--- 1 file changed, 47 insertions(+), 6 deletions(-) diff --git a/bananaSPLIT/libbabanasplit/libconfload.py b/bananaSPLIT/libbabanasplit/libconfload.py index 49a7dcd..aeab422 100644 --- a/bananaSPLIT/libbabanasplit/libconfload.py +++ b/bananaSPLIT/libbabanasplit/libconfload.py @@ -3,16 +3,22 @@ Created on 2 nov 2019 @author: Emanuele Trabattoni ''' -from bananaSPLIT.libbabanasplit.libfancylogger import fancyLogger -import json,sys, os +from libbabanasplit.libfancylogger import fancyLogger +import json,os,glob,copy + class bananaCONF(object): ''' Carica e Salva file di configurazione per bananaSPLITTER ''' def __init__(self, workdir=None, logger=None): - self.log=logger + self.log = logger + self.fileList = None + self.workdir = None + self.inUse = None + self.settingsList = dict() if workdir is not None: + self.workdir = workdir os.chdir(workdir) self.log.debug("Cerco le configurazioni in: [{}]".format(os.getcwd())) else: @@ -20,22 +26,57 @@ class bananaCONF(object): pass def open(self): + self.log.info("Carico i file di configurazione") + self.fileList = glob.glob(self.workdir+"\\*.json") + if len(self.fileList) > 0: + for f in self.fileList: + try: + tf = json.load(open(f)) + fName = f.split("\\")[-1] + self.settingsList[fName] = copy.deepcopy(tf) + self.log.info("Caricato correttamente: {}".format(fName)) + f.close() + except json.JSONDecodeError as e: + self.log.error("Impossibile leggere la configurazione:{}\n \ + Controlla il file a riga: {} e colonna:{}" .format(e.doc, e.lineno, e.colno)) + except IOError as ee: + self.log.error("Impossibile aprire il file: {}".format(ee)) + except Exception as eee: + self.log.critical("Eccezione inaspettata: {}".format(eee)) + else: + self.log.error("Non ho trovato alcun file di configurazione!") pass def reload(self): + self.settingsList = None + self.fileList = None + self.inUse = None + self.open() + pass + + def use(self, toUse): + self.inUse = toUse pass def save(self): + self.log.info("Salvo la configurazione: {}".format(self.inUse)) + try: + f=open() + json.dump(self.settingsList[self.inUse], f) + f.close() + except IOError as eee: + self.log.error("Impossibile salvare il file: {}".format(self.inUse)) pass - def get(self): + def get(self, k): pass - def set(self): + def set(self, k, v): pass if __name__ == "__main__": logg = fancyLogger() - confloader = bananaCONF(workdir=os.getcwd(), logger=logg) + confloader = bananaCONF(workdir="../", logger=logg) + confloader.open() \ No newline at end of file