#!/usr/bin/python # -*- coding: utf-8 -*- # --------------------------------------------------------------------------------------------------------- # Author : Ulrich Carmesin # Source : gitea.ucarmesin.de # --------------------------------------------------------------------------------------------------------- """ In diesem Modul werden alle Funktionen zusammengefasst zur Generierung und Ermittlung von pathsn """ import os.path import sys import basic.program import tools.config_tool import re import basic.constants as B import tools.path_const as P import tools.date_tool import objects.catalog import getpass TOOL_NAME = "value_tool" DOM_JOB = "job" DOM_PAR = "par" DOM_COMP = "comp" DOM_CONF = "conf" DOM_ENV = "env" DOM_CTLG = "ctlg" class ValueConf: """ this class contains the structure-informations of the testrelevant directories """ __instance = None def __init__(self, job=None): #print('init pathConf') confs = tools.config_tool.getConfig(job, "tool", "value") self.pattern = confs["pattern"] #print(self.pattern) ValueConf.__instance = self @staticmethod def getInstance(job = None): #print("PathConf getInstance " + str(PathConf.__instance)) if (ValueConf.__instance is None): ValueConf(job) #print("PathConf getInstance " + str(PathConf.__instance)) return ValueConf.__instance def get_key_value(job, key, comp=None): """ this function gets the value for the key which relates to an attribute in the job or in the component :param key: :param comp: :return: """ #job = basic.program.Job.getInstance() try: verify = job.getDebugLevel(TOOL_NAME)-4 except: verify = False #pt = PathConf.getInstance(job) if verify: job.debug(verify, "getKeyValue " + key) if DOM_JOB == key[0:3]: a = key.split(".") if DOM_PAR == a[1]: val = job.getParameter(a[2]) return val elif DOM_CONF == a[1]: val = job.conf[B.SUBJECT_PATH][a[2]] if verify: job.debug(verify, val) return val elif len(a) == 2: if not hasattr(job, a[1]): job.m.setError("key, job has not attribute "+a[1]) return "" return getattr(job, a[1]) elif DOM_COMP in key: if comp is None: raise Exception(P.EXP_COMP_MISSING.format(key)) if tools.config_tool.hasAttr(comp.conf, key[5:]): return tools.config_tool.getAttr(comp.conf, key[5:]) if tools.config_tool.hasAttr(comp, key[5:]): return tools.config_tool.getAttr(comp, key[5:]) return "" elif DOM_ENV in key: if key[4:] in comp.conf["conn"]: return comp.conf["conn"][key[4:]] pass elif DOM_CTLG in key: a = key.split(".") catalog = objects.catalog.Catalog.getInstance() if len(a) == 3: return catalog.getValue(job, a[1], a[2]) if len(a) == 4: return catalog.getValue(job, a[1], a[2], a[3]) pass elif "time" in key and hasattr(job, "start"): return getattr(job, "start") else: vc = ValueConf.getInstance(job) if key in vc.pattern: return vc.pattern[key] pc = P.PathConf.getInstance(job) if key in pc.pattern: return pc.pattern[key] job.m.setError("key is not defined " + key) return "xx-"+key+"-xx" def compose_pattern(job, pattern, comp): if "{" in pattern and "}" in pattern: return compose_string(job, pattern,comp) vc = ValueConf.getInstance(job) if pattern in vc.pattern: return compose_string(job, "{" + pattern + "}", comp) pc = P.PathConf.getInstance(job) if pattern in pc.pattern: return compose_string(job, "{" + pattern + "}", comp) return None def compose_string(job, pattern, comp): """ the function composes the pattern to the standardarized path with the attributes which are stored in the job and the component - the key of pathname is declared in path_const and the structure is configurated in config/value.yml. :param pattern: - keyword surroundet with {} :param comp: :return: path """ try: verify = job.getDebugLevel(TOOL_NAME) except: verify = False verbose = not False max=5 l = re.findall('\{.*?\}', pattern) #job.debug(verify, l) if verbose: print("l: " + str(l)) for pat in l: if verbose: print(str(max) + ": " + pattern + ": " + pat) pit = get_key_value(job, pat[1:-1], comp) if verbose: print(str(pit) + ": " + pattern + ": " + pat) #job.debug(verify, str(max) + ": " + pattern + ": " + pat + ": " + pit) pattern = pattern.replace(pat, pit) while ("{" in pattern): max = max-1 #job.debug(verify, str(max) + ": " + pattern + ": " + pat + ": " + pit) pattern = compose_string(job, pattern, comp) #job.debug(verify, str(max) + ": " + pattern + ": " + pat + ": " + pit) if (max < 3) : break return pattern