From 4511b4a559f2b85fe9d3cd0a55058ce4dff0a6e1 Mon Sep 17 00:00:00 2001 From: Ulrich Carmesin Date: Wed, 4 May 2022 19:29:26 +0200 Subject: [PATCH] compose xml-tidy-text --- test/test_xml.py | 12 ++++- utils/xml_tool.py | 126 ++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 121 insertions(+), 17 deletions(-) diff --git a/test/test_xml.py b/test/test_xml.py index aff4f2a..ce1acb1 100644 --- a/test/test_xml.py +++ b/test/test_xml.py @@ -72,6 +72,8 @@ class MyTestCase(unittest.TestCase): tree_list = { "eins": [ {"zwei": 2, "drei": 3, "vier": 4 } ] } tree_attr = { "eins": [ {"zwei": 2, "drei": 3, "vier": { "@name": "attr"} } ] } tree_attrx = { "eins": [ {"zwei": 2, "drei": 3, "vier": { "@name": "attr", "fuenf": 5} } ] } + tree_attr2 = { "eins": [ {"zwei": 2, "drei": 3, "vier": [ { "@name": "attr", "fuenf": 5} ] } ] } + tree_attr3 = { "eins": [ {"zwei": 2, "drei": 3, "vier": [ { "@name": "attr", "#text": "text" } ] } ] } c = utils.xml_tool.fcts() xml = c.tidy(tree_dict, 0) print("-------------------------------------") @@ -83,10 +85,16 @@ class MyTestCase(unittest.TestCase): print("-------------------------------------") print(xml) xml = c.tidy(tree_attr, 0) - print("-------------------------------------") + print("------ tree_attr -------------------------------") print(xml) xml = c.tidy(tree_attrx, 0) - print("-------------------------------------") + print("------ tree_attrx -------------------------------") + print(xml) + xml = c.tidy(tree_attr2, 0) + print("------ tree_attr2 -------------------------------") + print(xml) + xml = c.tidy(tree_attr3, 0) + print("------ tree_attr3 -------------------------------") print(xml) if __name__ == '__main__': diff --git a/utils/xml_tool.py b/utils/xml_tool.py index f42cb44..e77a83a 100644 --- a/utils/xml_tool.py +++ b/utils/xml_tool.py @@ -180,48 +180,144 @@ class fcts: def getTidyTab(self, depth=0): tab = "" + # print("depth "+str(depth)) for i in range(0, depth): tab += "\t" return tab - def tidy(self, tree, depth=0): - # tree_dict = { "eins": "zwei", "drei": "vier" } + def hasTidyAttr(self, tree): + first = True + if isinstance(tree, dict): + for x in tree: + if first and isinstance(tree[x], dict): + return self.hasTidyAttr(tree[x]) + elif first and isinstance(tree[x], list): + return self.hasTidyAttr(tree[x]) + elif x[0:1] == "@": + return True + else: + return False + if isinstance(tree, list): + for x in tree: + if first and isinstance(x, dict): + return self.hasTidyAttr(x) + elif first and isinstance(x, list): + return self.hasTidyAttr(x) + elif x[0:1] == "@": + return True + else: + return False + elif tree[0:1] == "@": + return True + return False + + def getTidyAttr(self, tree, x, depth=0): + xml = "" + attr = "" + if isinstance(tree, dict): + for y in tree: + if isinstance(y, dict): + return "" + elif isinstance(y, list): + attr = self.getTidyAttr(y, x, depth) + elif y[0:1] == "@": + attr += " "+y[1:]+"="+tree[y] + elif len(attr) > 1: + xml = "<"+x+attr+">"+y+"" + return xml + else: + return "" + elif isinstance(tree, dict): + for y in tree: + pass + return "" + + def getTidyDict(self, tree, x, depth=0): xml = "" tab = self.getTidyTab(depth) + newTree = {} + attr = "" + for y in tree: + if y[0:1] == "@": + attr = " " + y[1:] + "=\"" + tree[y] + "\"" + else: + newTree[y] = tree[y] + if len(newTree) > 0: + xml += tab + "<" + x + attr + ">\n" + self.tidy(newTree, depth + 1) + tab + "\n" + else: + xml += tab + "<" + x + attr + "/>\n" + return xml - print("tidy "+str(type(tree))) + def tidy(self, tree, depth=0): + verify = 0 + xml = "" + tab = self.getTidyTab(depth) + hasa = self.hasTidyAttr(tree) + if verify: print("getTidy "+str(type(tree))+" hasAttr "+str(hasa)) if isinstance(tree, dict): - print("dict") for x in tree: - print(x) + if verify: print("dict " + str(x)) if isinstance(tree[x], dict): + hasa = self.hasTidyAttr(tree[x]) newTree = {} attr = "" for y in tree[x]: + if verify: print("y-for "+str(y)) if y[0:1] == "@": - attr = " "+y[1:]+"=\""+tree[x][y]+"\"" + attr = " " + y[1:] + "=\"" + tree[x][y] + "\"" else: newTree[y] = tree[x][y] if len(newTree) > 0: - xml += tab+"<"+x+attr+">\n"+self.tidy(newTree, depth+1)+tab+"\n" + xml += tab + "<" + x + attr + ">\n" + self.tidy(newTree, depth + 1) + tab + "\n" else: - xml += tab+"<"+x+attr+"/>\n" + xml += tab + "<" + x + attr + "/>\n" elif isinstance(tree[x], list): - xml += tab+"<"+x+">\n"+self.tidy(tree[x], depth+1)+tab+"\n" + hasa = self.hasTidyAttr(tree[x]) + if hasa: + xml += tab + "<" + x + self.tidy(tree[x], depth) + "\n" + else: + xml += tab + "<" + x + ">\n" + self.tidy(tree[x], depth + 1) + tab + "\n" + elif x[0:1] == "@": + attr = " " + x[1:] + "=\"" + tree[x] + "\"" + elif x == "#text": + xml = str(tree[x]) + return xml else: - xml += tab+"<"+x+">"+str(tree[x])+"\n" + xml += tab + "<" + x + ">" + str(tree[x]) + "\n" elif isinstance(tree, list): - # tree_list = { "eins": [ {"zwei": 2, "drei": 3, "vier": 4 } ] } - print("list") for x in tree: + if verify: print("list " + str(x)) if isinstance(x, dict): - xml += self.tidy(x, depth) + newTree = {} + attr = "" + text = False + hasa = self.hasTidyAttr(x) + for y in x: + if verify: print("y-for "+str(y)) + if y[0:1] == "@": + attr = " " + y[1:] + "=\"" + x[y] + "\"" + elif y == "#text": + text = x[y] + else: + newTree[y] = x[y] + if text and len(newTree) > 0 and len(attr) > 0: + xml += "" + attr + ">" + self.tidy(newTree, depth + 1) + elif len(newTree) > 0 and len(attr) > 0: + xml += "" + attr + ">\n" + self.tidy(newTree, depth + 1) + tab + elif len(attr) > 0: + if not isinstance(x, dict): + xml += tab + "<296" + x + attr + "/>\n" + else: + xml += attr + ">"+text + else: + xml += self.tidy(newTree, depth) else: - xml += tab+"<"+x+">\n"+self.tidy(x, depth)+tab+"" + xml += tab + "<" + x + ">\n" + self.tidy(x, depth) + tab + "" else: - xml += "<"+str(tree)+">" + xml += "<" + str(tree) + ">" return xml + def addMerkmal(baum, pfad, index, wert): """ Eingabe: