# managing the components # ----------------------------------------------------------------------------- """ component has to be created in relation of the application (basis.yml). Each componente could be created mostly once, but not everytime: * the same instance of a component is used in different contexts * there could be exist more instances * there could be alternatives of an instance Each kind of instance has its component-class and for each use should be an object be created. Each crated component-onject are documented in the parameter-file. """ import utils.config_tool import utils.conn_tool import ulrich.program import ulrich.message import components.component import importlib import copy components = {} PARAM_NOSUBNODE = [ "artifact", "components", "instance" ] class ComponentManager: __instance = None """ initializes the Manager with all necessary components """ def __init__(self): job = ulrich.program.Job.getInstance() job.m.logDebug("applicationscomponente -- " + str(type(job.par)) ) self.components = {} def initComponents(self): # sets components the first time # afterwards set components from parameter-file job = ulrich.program.Job.getInstance() anw = job.par.application job.m.logDebug("applicationscomponente -- " + str(type(job.par)) ) if not job.conf.confs["applicationen"].get(anw): job.m.setFatal("application " + job.par.application + " is not configured") return for k in job.conf.confs["applicationen"].get(anw): job.m.logDebug("applicationscomponente -- " + k + ":" ) self.createComponents(k, 0, "") def setComponents(self): # set components from parameter-file job = ulrich.program.Job.getInstance() job.m.logDebug("applicationscomponente -- " + str(type(job.par)) ) def getComponent(self, compobjname): job = ulrich.program.Job.getInstance() verify=-2+job.getDebugLevel("job_tool") job.debug(verify, "getComponents " + compobjname) return components[compobjname] def getComponents(self, mainfct): job = ulrich.program.Job.getInstance() verify=-2+job.getDebugLevel("job_tool") job.debug(verify, "getComponents " + mainfct) out = [] for c in components: job.debug(verify, "getComponents " + c + ": " + str(components[c].conf)) print("getComponents " + c + ": " + str(components[c].conf)) if mainfct in components[c].conf["function"]: out.append(c) return out @staticmethod def getInstance(): if (ComponentManager.__instance is not None): return ComponentManager.__instance else: raise Exception("Klasse noch nicht initialisiert") def createComponents(self, componentName, nr, suffix): """ in order to create a component it must be loaded * knogwedge of the application - which components should be created * technical-knowledge of the instanciated component, especially the connection, user, password * business-knowledge of the component, especially of their interfaces resp. artifacts :param componentName: Name of the component :param nr: :param add: :return: """ job = ulrich.program.Job.getInstance() verify=job.getDebugLevel("job_tool") componentName = componentName.lower() print("createComponent " + componentName) confs = utils.config_tool.getConfig("comp", componentName) conns = utils.conn_tool.getConnections(componentName) print("createComponent -91- " + componentName + " : " + str(confs)) if nr > 0 and int(confs["conf"]["instance"]["count"]) > 1: job.m.setError("for multiple callers are multiple calls not implemented ") if nr > 0 and len(conns) == 0: job.m.setError("for multiple calls has only one call configured") print(confs) print("createComponent 1 " + componentName) print(getComponentPath(componentName)) print("createComponent 2 " + componentName) cmodul = importlib.import_module(getComponentPath(componentName)) class_ = getattr(cmodul, getComponentClass(componentName)) readedPar = job.loadParameter() if len(conns) == 1: print("createComponent 3 a " + componentName) if nr > 0 and confs["conf"]["instance"]["single"] == "n": name = componentName + "_0" + str(nr) else: name = componentName c = class_() c.name = name c.conf = confs["conf"] c.conf["conn"] = conns[0] c.m = ulrich.message.Message(ulrich.message.LIMIT_DEBUG, "logTime", name) c.init() print("createComponent 4 a " + componentName) print(vars(c)) print(vars(c.m)) if readedPar is not None: print("createComponent 5 a " + name + " : "+ str(readedPar)) for k in readedPar["components"][name].keys(): c.conf[k] = readedPar["components"][name][k] components[name] = c self.createComponent(c, nr, suffix) else: i = 1 print("createComponent 3 b " + componentName) for cn in conns: name = componentName+ "_0"+str(i) c = class_() c.name = name c.conf = confs["conf"] c.conf["conn"] = conns[0] c.m = ulrich.message.Message(ulrich.message.LIMIT_DEBUG, "logTime", name) c.init() print("createComponent 4 b " + componentName) print(vars(c)) if readedPar is not None: for k in readedPar["components"][name].keys(): c.conf[k] = readedPar["components"][name][k] components[name] = c self.createComponent(c, i, suffix) i = i + 1 print("createComponent 9 " + componentName) print(components) def createComponent(self, comp, nr, suffix): job = ulrich.program.Job.getInstance() verify = -2 + job.getDebugLevel("job_tool") job.debug(verify, "getComponents " + str(comp.conf["components"])) for c in comp.conf["components"].keys(): if c == "none": continue self.createComponents(c, nr, suffix) def getComponentFolder(comp): return comp.lower() def getComponentModul(comp): return comp[0:1].upper() + comp[1:].lower() def getComponentClass(comp): return comp[0:1].upper() + comp[1:].lower() def getComponentPath(comp): return "components." + getComponentFolder(comp) + "." + getComponentModul(comp) def getComponentDict(): job = ulrich.program.Job.getInstance() verify=-2+job.getDebugLevel("job_tool") job.debug(verify, "getComponents ") out = {} for c in components: out[components[c].name] = {} print("getCompDict " + components[c].name) for k in components[c].confs.keys(): print("getCompDict " + k) if isParameterSubnode(k): # "artifact" in k or "components" in k or "instance" in k: print("getCompDict -b- " + k) out[components[c].name][k] = copy.deepcopy(components[c].confs[k]) return out def isParameterSubnode(key): for k in PARAM_NOSUBNODE: if key in k: return False return True