#!/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 tools.config_tool import re import basic.constants as B import tools.path_const as P import tools.date_tool import tools.value_tool import getpass from tools.path_const import PathConf TOOL_NAME = "path_tool" def getHome(): home = os.getcwd() if home[-4:] == "test" and home[-6:] != "datest": home = home[0:-5] if home[-10:] == "components": home = home[0:-11] if home[-6:] == "datest": prgdir = home[-6:] home = home[0:-7] elif home[-7:] == "program": prgdir = home[-7:] home = home[0:-8] return home def getBasisConfigPath(): home = os.getcwd() a = home.split(os.path.sep) for i in range(1, len(a)): path = os.path.sep.join(a[0:-i]) path = os.path.join(path, P.VAL_CONFIG, B.BASIS_FILE) for format in tools.config_tool.CONFIG_FORMAT: filepath = path+"."+format if os.path.isfile(filepath): return filepath if os.path.exists(filepath): return filepath raise Exception("no basis-configuration found") def getActualJsonPath(job): username = getpass.getuser() path = os.path.join(job.conf.confs[B.SUBJECT_PATH][B.ATTR_PATH_DEBUG], username+"Job.json") return path def compose_path(job, pathname, comp): """ this function composes a concrete path by the structured pathname - the key of pathname is declared in path_const and the structure is configurated in config/value.yml. :param pathname - plain keyword :param comp: :return: """ verify = job.getDebugLevel(TOOL_NAME) job.debug(verify, "composePath " + pathname) return tools.value_tool.compose_pattern(job, pathname, comp) def rejoinPath(a, b="", c="", d="", e="", f=""): """ this function concatenates the arguments to a path in the correct format for the operating-system :param a: :param b: optional :param c: optional :param d: optional :param e: optional :param f: optional :return: path """ work = a+"/"+b+"/"+c+"/"+d+"/"+e+"/"+f if a.find("://") > 1: protocol = True else: protocol = False work = re.sub(r'\\', '/', work) work = re.sub(r'\/', '/', work) work = re.sub(r'//', '/', work) while work[-1:] == "/": work = work[0:-1] l = work.split("/") out = "" for x in l: if len(x) < 1: continue if protocol: if len(out) < 1: out = x else: out = out+"/"+x else: out = os.path.join(out, x) if out[1:2] == ":" and out[2:3] != "\\": out = out[0:2]+"\\"+out[2:] elif protocol: if "://" not in out or out.index("://") > 8 or out.index("://") < 1: i = out.index(":/") out = out[0:i+1] + "/" + out[i+1:] pass if not protocol and out.count("\\") < 1 and out[0:1] != "/" and out[0:2] != "..": out = "/"+out return out def extractPattern(job, pathtyp, comp=None): """ this function extracts recoursively all parts of the pathstrucure as key and gets the values from the job-parameter and job-configuration :param pathtyp: the name of the path-structure :param comp: :return: dictionary of all part (key) with their valuess """ #job = basic.program.Job.getInstance() verify = job.getDebugLevel(TOOL_NAME) out = [] pt = PathConf.getInstance(job) pattern = pt.pattern[pathtyp] work = pattern while "{" in work: i = work.index("{") j = work.index("}") pre = work[0:i] pat = work[i+1:j] job.debug(verify, work + " von " + str(i) + "-" + str(j) + " pre " + pre + "pat " + pat) pit = tools.value_tool.get_key_value(job, pat, comp) tup = (pre, pat, pit) out.append(tup) work = work[j+1:] return out def extractPath(job, pathtyp, path): """ this function extracts parts of a concrete structered path and stores the parts as attributes into the actual job. So these attributes can read from the concrete path instead of the related parameter-arguments. It stores the values into the job-parameter :param pathtyp: the structure of the concrete path :param path: the concrete path - it should be the directory in the parameter of the job :return: """ patterlist = extractPattern(job, pathtyp) verbose = False work = path i = 0 if verbose: print("-- extractPatternList -- " + pathtyp + ":" + str(patterlist)) for p in patterlist: if len(p) < 1 : continue delim = p[0] key = p[1] val = p[2] if i >= len(patterlist) - 1: nextdelim = "" else: nextdelim = patterlist[i+1][0] if verbose: print("xPath delim " + delim + " " + str(len(delim)) + ", " + nextdelim + " work " + work) work = work[len(delim):] if verbose: print("xPath key " + key + " i " + str(i) + " work " + work) if val is not None: if verbose: print("val not none " + val) if val in work: if verbose: print("val ok") work = work.replace(val, "") elif "time" in key and "job.par" in key: prop = "" if i < len(patterlist) - 1: prop = work[0:work.index(nextdelim)] else: prop = work key = key[8:] if verbose: print("setprop " + key + " = " + prop) if hasattr(job.par, key): delattr(job.par, key) setattr(job.par, key, val) else: if verbose: print("val not not ok " + val + " zu " + key) elif "job.par" in key: prop = "" if i < len(patterlist) - 1: if verbose: print("job.par nextdelim " + nextdelim) prop = work[0:work.index(nextdelim)] else: prop = work key = key[8:] if verbose: print("setprop " + key + " = " + prop) if hasattr(job.par, key): delattr(job.par, key) setattr(job.par, key, prop) work = work.replace(prop, "") else: if verbose: print("val is none " + key) i = i +1