#!/usr/bin/python # -*- coding: utf-8 -*- # --------------------------------------------------------------------------------------------------------- # Author : Ulrich Carmesin # Source : gitea.ucarmesin.de # --------------------------------------------------------------------------------------------------------- # 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 basic.program import basic.message import components.component import importlib import copy import basic.constants as B comps = {} PARAM_NOSUBNODE = ["artifact", "components", "instance"] def getComponents(mainfct): job = basic.program.Job.getInstance() verify = -2 + job.getDebugLevel("job_tool") job.debug(verify, "getComponents " + mainfct) out = [] for c in comps: job.debug(verify, "getComponents " + c + ": " + str(comps[c].conf)) print("getComponents " + c + ": " + str(comps[c].conf)) if mainfct in comps[c].conf["function"]: out.append(c) return out class ComponentManager: __instance = None """ initializes the Manager with all necessary components """ def __init__(self): job = basic.program.Job.getInstance() job.m.logDebug("applicationscomponente -- " + str(type(job.par))) self.components = {} print ("init ComponentHandling "+str(self)) def initComponents(self): # sets components the first time # afterwards set components from parameter-file job = basic.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 = basic.program.Job.getInstance() job.m.logDebug("applicationscomponente -- " + str(type(job.par))) def getComponent(self, compobjname): job = basic.program.Job.getInstance() verify = -2 + job.getDebugLevel("job_tool") job.debug(verify, "getComponents " + compobjname) return comps[compobjname] def getComponents(self, mainfct): job = basic.program.Job.getInstance() verify = -2 + job.getDebugLevel("job_tool") job.debug(verify, "getComponents " + mainfct) out = [] for c in comps: job.debug(verify, "getComponents " + c + ": " + str(comps[c].conf)) print("getComponents " + c + ": " + str(comps[c].conf)) if mainfct in comps[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 suffix: :return: """ job = basic.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[B.SUBJECT_CONN] = conns[0] c.m = basic.message.Message(basic.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)) if name in readedPar["comps"]: for k in readedPar["comps"][name].keys(): c.conf[k] = readedPar["comps"][name][k] comps[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[B.SUBJECT_CONN] = conns[0] c.m = basic.message.Message(basic.message.LIMIT_DEBUG, "logTime", name) c.init() print("createComponent 4 b " + componentName) print(vars(c)) if readedPar is not None: if name in readedPar["comps"]: for k in readedPar["comps"][name].keys(): c.conf[k] = readedPar["comps"][name][k] else: print("comp fehlt "+name) comps[name] = c self.createComponent(c, i, suffix) i = i + 1 print("createComponent 9 " + componentName) print(comps) def createComponent(self, comp, nr, suffix): job = basic.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 = basic.program.Job.getInstance() verify = -2 + job.getDebugLevel("job_tool") job.debug(verify, "getComponents ") out = {} for c in comps: out[comps[c].name] = {} print("getCompDict " + comps[c].name) for k in comps[c].conf.keys(): print("getCompDict " + k) if isParameterSubnode(k): # "artifact" in k or "components" in k or "instance" in k: print("getCompDict -b- " + k) out[comps[c].name][k] = copy.deepcopy(comps[c].conf[k]) return out def isParameterSubnode(key): for k in PARAM_NOSUBNODE: if key in k: return False return True def getPlainCompname(name): if "_0" in name: return name[0:-3] return name