#!/usr/bin/python # -*- coding: utf-8 -*- # --------------------------------------------------------------------------------------------------------- # Author : Ulrich Carmesin # Source : gitea.ucarmesin.de # --------------------------------------------------------------------------------------------------------- import importlib, os import basic.program import basic.constants as B # ------------------------------------------------- def hasAttr(o, name): if (isinstance(o, dict)): if (name in o.keys()): return True elif (isinstance(o, list)): print("hasAttr list "+str(type(o))) elif hasattr(o, name): return True return False def getAttr(o, name): if (isinstance(o, dict)): if (name in o.keys()): return o[name] elif (isinstance(o, list)): print("getAttr c list "+str(type(o))) elif hasattr(o, name): return o.get(name) """ Toolmanager """ def getCompAttr(comp, topic, attr, table=""): out = "" print(topic+" "+attr+" "+str(comp)) if hasAttr(comp.conf[B.SUBJECT_CONN], topic) and hasAttr(comp.conf[B.SUBJECT_CONN][topic], attr): return getAttr(comp.conf[B.SUBJECT_CONN][topic], attr) if len(table) > 1 and hasAttr(comp.conf[B.SUBJECT_ARTS][topic], table) \ and hasAttr(comp.conf[B.SUBJECT_ARTS][topic][table], attr): return getAttr(comp.conf[B.SUBJECT_ARTS][topic][table], attr) if hasAttr(comp.conf[B.SUBJECT_ARTS], topic) and hasAttr(comp.conf[B.SUBJECT_ARTS][topic], attr): print("attr "+attr+" vorhanden") return getAttr(comp.conf[B.SUBJECT_ARTS][topic], attr) raise LookupError(topic+"."+attr+" is not set in comp " + comp.name) # class ToolManager: def getDbTool(comp): job = basic.program.Job.getInstance() verify = int(job.getDebugLevel("db_tool")) dbtype = getCompAttr(comp, B.TOPIC_NODE_DB, B.ATTR_TYPE, "") toolname = "db"+dbtype+"_tool" filepath = os.path.join(job.conf.confs[B.SUBJECT_PATH][B.ATTR_PATH_PROGRAM], "utils", toolname+".py") #comp.m.debug(verify, "toolname "+filepath) if not os.path.exists(filepath): raise FileNotFoundError("file for tool "+toolname+" does not exist "+filepath) cmodul = importlib.import_module("utils."+toolname) class_ = getattr(cmodul, "DbFcts") c = class_() c.setComp(comp) return c def getCliTool(comp): job = basic.program.Job.getInstance() verify = int(job.getDebugLevel("db_tool")) clitype = getCompAttr(comp, B.TOPIC_NODE_CLI, B.ATTR_TYPE, "") toolname = "cli"+clitype+"_tool" filepath = os.path.join(job.conf.confs[B.SUBJECT_PATH][B.ATTR_PATH_PROGRAM], "utils", toolname+".py") #comp.m.debug(verify, "toolname "+filepath) if not os.path.exists(filepath): raise FileNotFoundError("file for tool "+toolname+" does not exist "+filepath) cmodul = importlib.import_module("utils."+toolname) class_ = getattr(cmodul, "CliFcts") c = class_() c.setComp(comp) return c def getApiTool(comp): job = basic.program.Job.getInstance() verify = int(job.getDebugLevel("db_tool")) apitype = getCompAttr(comp, B.TOPIC_NODE_API, B.ATTR_TYPE, "") toolname = "api"+apitype+"_tool" filepath = os.path.join(job.conf.confs[B.SUBJECT_PATH][B.ATTR_PATH_PROGRAM], "utils", toolname+".py") #comp.m.debug(verify, "toolname "+filepath) if not os.path.exists(filepath): raise FileNotFoundError("file for tool "+toolname+" does not exist "+filepath) cmodul = importlib.import_module("utils."+toolname) class_ = getattr(cmodul, "ApiFcts") c = class_() c.setComp(comp) return c