From f2537d7604c06b47d6cad2ec2057e564fe3ae3f4 Mon Sep 17 00:00:00 2001 From: Ulrich Carmesin Date: Sun, 28 Aug 2022 15:34:06 +0200 Subject: [PATCH] refactoring and beautifying --- basic/compexec.py | 22 +++++++++++++--------- basic/message.py | 13 +++++++++++++ basic/text_const.py | 7 +++++++ basic/toolHandling.py | 18 ++++++++++-------- test/test_24gen.py | 13 +++++++++++++ test/test_31db.py | 2 +- utils/data_const.py | 1 + utils/file_abstract.py | 20 ++++++-------------- utils/filejson_tool.py | 1 + utils/filexml_tool.py | 5 +++++ utils/gen_tool.py | 3 --- utils/i18n_tool.py | 11 +++++++++-- 12 files changed, 79 insertions(+), 37 deletions(-) diff --git a/basic/compexec.py b/basic/compexec.py index d590fed..f218d2f 100644 --- a/basic/compexec.py +++ b/basic/compexec.py @@ -49,6 +49,8 @@ import utils.match_tool import utils.match_const as M import utils.tdata_tool import basic.constants as B +import basic.text_const as T +import utils.data_const as D class Testexecuter(): @@ -112,7 +114,7 @@ class Testexecuter(): print (t) if utils.db_abstract.isCompTable(self, tdata, t): self.m.logInfo("insert content "+ self.name) - dbi = basic.toolHandling.getDbTool(self, job) + dbi = basic.toolHandling.getDbTool(job, self) dbi.insertTables(tdata, job) break self.m.setMsg("data loaded for " + self.name + " is OK") @@ -139,7 +141,7 @@ class Testexecuter(): self.m.debug(verify, "--- " + str(inspect.currentframe().f_code.co_name) + "() started at " + datetime.now().strftime("%Y%m%d_%H%M%S") + " for " + str(self.name).upper()) if B.TOPIC_NODE_DB in self.conf[B.SUBJECT_ARTS]: self.m.logInfo("select db-content "+ self.name) - dbi = basic.toolHandling.getDbTool(self, job) + dbi = basic.toolHandling.getDbTool(job, self) data = dbi.selectTables(subdir, job) print("ppp") #data = {} @@ -252,16 +254,18 @@ class Testexecuter(): :param tdata: :return: """ - if step.fct in self.conf["teststeps"]: - for s in self.conf["teststeps"][step.fct]: - stepconf = self.conf["teststeps"][step.fct][s] - if stepconf["tool"] == "file": + if not step.fct in self.conf[B.DATA_NODE_STEPS]: + raise Exception(self.m.getMessageText(T.EXP_KEY_DOESNT_EXIST, [step.fct, self.name])) + if step.fct in self.conf[B.DATA_NODE_STEPS]: + for stepconf in self.conf[B.DATA_NODE_STEPS][step.fct]: + if stepconf[B.SUBJECT_TOOL] == B.TOPIC_NODE_FILE: + tool = basic.toolHandling.getFileTool(job, self, stepconf[B.ATTR_ARTS_TYPE]) print("file-tool") - elif stepconf["tool"] == "api": + elif stepconf[B.SUBJECT_TOOL] == B.TOPIC_NODE_API: print("api-tool") - elif stepconf["tool"] == "cli": + elif stepconf[B.SUBJECT_TOOL] == B.TOPIC_NODE_CLI: print("cli-tool") - elif stepconf["tool"] == "db": + elif stepconf[B.SUBJECT_TOOL] == B.TOPIC_NODE_DB: print("db-tool") else: print("nichts da") diff --git a/basic/message.py b/basic/message.py index b0e75d6..f4bef55 100644 --- a/basic/message.py +++ b/basic/message.py @@ -21,6 +21,8 @@ import os import math from datetime import datetime import utils.path_tool +import utils.i18n_tool +import basic.text_const as T LIMIT_FATAL = 0 LIMIT_ERROR = 4 @@ -55,6 +57,7 @@ class Message: """ def __init__(self, job, level, logTime, componente): # (self, componente, out, level): + self.job = job self.componente = componente # dezantrales Logsystem verify = LIMIT_DEBUG self.initErrorTyp() @@ -191,6 +194,16 @@ class Message: self.setRc(RC_MSG, text) self.logInfo(text) + def getMessageText(self, text, args): + out = "" + constName = "" + for i in range(0, len(T.LIST_EXP_TEXT)): + if text == T.LIST_EXP_TEXT[i]: + constName = T.LIST_EXP_CONST[i] + txt = utils.i18n_tool.I18n.getInstance().getMessage(self.job, constName, args) + out = txt.format(args) + return out + def logFatal(self, text): self.log(LIMIT_FATAL, "FATAL: " + text) self.debug(LIMIT_FATAL, "FATAL: " + text) diff --git a/basic/text_const.py b/basic/text_const.py index e69de29..79d93c7 100644 --- a/basic/text_const.py +++ b/basic/text_const.py @@ -0,0 +1,7 @@ +# ----------------- + +EXP_KEY_MISSING = "key is missing {}" +EXP_KEY_DOESNT_EXIST = "key {} doesnt exist in domain {}" +LIST_EXP_TEXT = [EXP_KEY_MISSING, EXP_KEY_DOESNT_EXIST] +LIST_EXP_CONST = ["EXP_KEY_MISSING", "EXP_KEY_DOESNT_EXIST"] + diff --git a/basic/toolHandling.py b/basic/toolHandling.py index fb11758..f930b26 100644 --- a/basic/toolHandling.py +++ b/basic/toolHandling.py @@ -50,18 +50,18 @@ def getCompAttr(comp, topic, attr, table=""): def getTool(technicType, comp, job): if technicType == B.TOPIC_NODE_DB: - return getDbTool(comp, job) + return getDbTool(job, comp) if technicType == B.TOPIC_NODE_CLI: - return getCliTool(comp, job) + return getCliTool(job, comp) if technicType == B.TOPIC_NODE_API: - return getApiTool(comp, job) + return getApiTool(job, comp) if technicType == B.TOPIC_NODE_FILE: # TODO im Allgemeinen keine konrete Implementierung aufrufen, # denn zu einer Komponente koennen unterschiedliche Dateien vorkommen return getFileTool(job, comp, "") # class ToolManager: -def getDbTool(comp, job): +def getDbTool(job, comp, dbtype=""): verify = int(job.getDebugLevel("db_tool")) dbtype = getCompAttr(comp, B.TOPIC_NODE_DB, B.ATTR_TYPE, "") toolname = "db"+dbtype+"_tool" @@ -75,7 +75,7 @@ def getDbTool(comp, job): c.setComp(comp) return c -def getCliTool(comp, job): +def getCliTool(job, comp): verify = int(job.getDebugLevel("db_tool")) clitype = getCompAttr(comp, B.TOPIC_NODE_CLI, B.ATTR_TYPE, "") toolname = "cli"+clitype+"_tool" @@ -89,7 +89,7 @@ def getCliTool(comp, job): c.setComp(comp) return c -def getApiTool(comp, job): +def getApiTool(job, comp): verify = int(job.getDebugLevel("db_tool")) apitype = getCompAttr(comp, B.TOPIC_NODE_API, B.ATTR_TYPE, "") toolname = "api"+apitype+"_tool" @@ -104,9 +104,11 @@ def getApiTool(comp, job): return c def getFileTool(job, comp, filenode=""): - verify = int(job.getDebugLevel("db_tool")) - if len(filenode) > 3 and filenode[-1:] != ".": + verify = int(job.getDebugLevel("file_tool")) + if len(filenode) > 3 and "." in filenode and filenode[-1:] != ".": filetype = utils.config_tool.getAttribute(comp, filenode, B.ATTR_ARTS_TYPE, job) + elif len(filenode) > 2 and len(filenode) < 5: + filetype = filenode else: filetype = getCompAttr(comp, B.TOPIC_NODE_FILE, B.ATTR_TYPE, "") toolname = "file"+filetype+"_tool" diff --git a/test/test_24gen.py b/test/test_24gen.py index 83b0ee0..d4d5721 100644 --- a/test/test_24gen.py +++ b/test/test_24gen.py @@ -33,6 +33,19 @@ class MyTestCase(unittest.TestCase): print((str(res))) res = utils.gen_tool.getElemList(G.KEY_LIST, "cat:countries", 6, job) print((str(res))) + for cnt in [3, 15, 75, 400]: + res = utils.gen_tool.getElemList(G.KEY_LIST, "A, B,C , D", cnt, job) + self.assertEqual(len(res), cnt) + cnttest += 1 + for cnt in [3, 15, 75, 400]: + res = utils.gen_tool.getElemList(G.KEY_LIST, "cat:countries", cnt, job) + self.assertEqual(len(res), cnt) + cnttest += 1 + for cnt in [3, 15, 75, 400]: + res = utils.gen_tool.getElemList(G.KEY_LIST, "0 .. 4", cnt, job) + self.assertEqual(len(res), cnt) + cnttest += 1 + MyTestCase.mymsg += "\n----- "+actfunction+" : "+str(cnttest) diff --git a/test/test_31db.py b/test/test_31db.py index 8893f58..6c2329f 100644 --- a/test/test_31db.py +++ b/test/test_31db.py @@ -77,7 +77,7 @@ class MyTestCase(unittest.TestCase): comp.conf[B.SUBJECT_CONN] = {} comp.conf[B.SUBJECT_CONN][B.TOPIC_NODE_DB] = {} comp.conf[B.SUBJECT_CONN][B.TOPIC_NODE_DB][B.ATTR_TYPE] = "shive" - tool = basic.toolHandling.getDbTool(comp) + tool = basic.toolHandling.getDbTool(job, comp) self.assertRegex(str(type(tool)), 'dbshive_tool.DbFcts') attr = tool.getDbAttributes("xx") self.assertRegex(attr[B.ATTR_DB_PARTITION], 'n') diff --git a/utils/data_const.py b/utils/data_const.py index 80cab05..a2a760a 100644 --- a/utils/data_const.py +++ b/utils/data_const.py @@ -25,6 +25,7 @@ DDL_TYPE = "type" DFILE_TYPE_YML = "yml" DFILE_TYPE_JSON = "json" DFILE_TYPE_CSV = "csv" +DFILE_TYPE_XML = "xml" DFILE_TESTCASE_NAME = "testspec" DFILE_TESTSUITE_NAME = "testsuite" DFILE_TABLE_PREFIX = "table_" diff --git a/utils/file_abstract.py b/utils/file_abstract.py index 1a6258e..b63688f 100644 --- a/utils/file_abstract.py +++ b/utils/file_abstract.py @@ -5,6 +5,7 @@ # Source : gitea.ucarmesin.de # --------------------------------------------------------------------------------------------------------- import os +import re import basic.program import basic.catalog import utils.config_tool @@ -13,16 +14,8 @@ import basic.toolHandling import utils.data_const as D import utils.file_tool import utils.path_tool - -""" -# TODO Beschreibung file-tools -* fileXml_tool -* fileJson_tool -* fileYaml_tool -* fileCsv_tool -* fileHtml_tool -... -""" +import xml.etree.ElementTree as ET +import basic.catalog class FileFcts(): @@ -46,7 +39,6 @@ class FileFcts(): :return: """ - def file2dict(self): pass @@ -84,7 +76,7 @@ class FileFcts(): #txt = self.createDict() utils.file_tool.writeFileText(self.comp.m, archivpath, txt) - def send_request(self, step): + def send_request(self, job, step): archivpath = "" filename = step.args["filename"] technique = step.args["technique"] @@ -95,11 +87,11 @@ class FileFcts(): continue envpath = o["envpath"] envpath = utils.path_tool.composePattern(envpath, self.comp) - fct = basic.toolHandling.getCliTool(self.comp) + fct = basic.toolHandling.getCliTool(job, self.comp) fct.copy(self.job, archivpath, envpath) elif technique == "api": txt = utils.file_tool.readFileText(archivpath, self.comp.m) - fct = basic.toolHandling.getApiTool(self.comp) + fct = basic.toolHandling.getApiTool(job, self.comp) response = fct.send(self.job, self.comp, txt) archivpath = os.path.join(utils.path_tool.composePattern("{tcresult}/response", self.comp), filename) diff --git a/utils/filejson_tool.py b/utils/filejson_tool.py index 0642ccc..bc66ceb 100644 --- a/utils/filejson_tool.py +++ b/utils/filejson_tool.py @@ -17,3 +17,4 @@ class FileFcts(utils.file_abstract.FileFcts): def __init__(self): pass + diff --git a/utils/filexml_tool.py b/utils/filexml_tool.py index 0642ccc..062a6e5 100644 --- a/utils/filexml_tool.py +++ b/utils/filexml_tool.py @@ -4,6 +4,7 @@ # Author : Ulrich Carmesin # Source : gitea.ucarmesin.de # --------------------------------------------------------------------------------------------------------- +import xmltodict import basic.program import utils.config_tool import utils.file_abstract @@ -17,3 +18,7 @@ class FileFcts(utils.file_abstract.FileFcts): def __init__(self): pass + + + def parseFile2Dict(self): + pass \ No newline at end of file diff --git a/utils/gen_tool.py b/utils/gen_tool.py index a537533..330ec8d 100644 --- a/utils/gen_tool.py +++ b/utils/gen_tool.py @@ -122,9 +122,6 @@ def getElemList(formula, values, count, job): temp = catalog.getKeys(domain, job) if not isinstance(temp, list): temp = [] - max = 3 while len(temp) > 0 and len(out) < count: out += temp - max -= 1 - if max < 0: break return out[0:count] diff --git a/utils/i18n_tool.py b/utils/i18n_tool.py index a076c9d..4ccce43 100644 --- a/utils/i18n_tool.py +++ b/utils/i18n_tool.py @@ -37,6 +37,12 @@ class I18n: return I18n.__instance + def getMessage(self, job, key, args=[]): + print("getMessage "+key+" "+str(args)) + out = self.getText(key, job) + out = out.format(args) + return out + def getText(self, key, job=None): """ this function gets the text depending on language which is set in job.conf @@ -52,8 +58,9 @@ class I18n: language = "en" if language not in self.cache: raise Exception(EXP_KEY_MISSING, (key)) - out = self.extractText(key) - key = self.extractKey(key) + if "=" in key: + out = self.extractText(key) + key = self.extractKey(key) if key in self.cache[language]: out = self.cache[language][key] elif key in self.cache[DEFAULT_LANGUAGE]: