|
@ -180,48 +180,144 @@ class fcts: |
|
|
|
|
|
|
|
|
def getTidyTab(self, depth=0): |
|
|
def getTidyTab(self, depth=0): |
|
|
tab = "" |
|
|
tab = "" |
|
|
|
|
|
# print("depth "+str(depth)) |
|
|
for i in range(0, depth): |
|
|
for i in range(0, depth): |
|
|
tab += "\t" |
|
|
tab += "\t" |
|
|
return tab |
|
|
return tab |
|
|
|
|
|
|
|
|
def tidy(self, tree, depth=0): |
|
|
def hasTidyAttr(self, tree): |
|
|
# tree_dict = { "eins": "zwei", "drei": "vier" } |
|
|
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+"</"+x+">" |
|
|
|
|
|
return xml |
|
|
|
|
|
else: |
|
|
|
|
|
return "" |
|
|
|
|
|
elif isinstance(tree, dict): |
|
|
|
|
|
for y in tree: |
|
|
|
|
|
pass |
|
|
|
|
|
return "" |
|
|
|
|
|
|
|
|
|
|
|
def getTidyDict(self, tree, x, depth=0): |
|
|
xml = "" |
|
|
xml = "" |
|
|
tab = self.getTidyTab(depth) |
|
|
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 + "</" + x + ">\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): |
|
|
if isinstance(tree, dict): |
|
|
print("dict") |
|
|
|
|
|
for x in tree: |
|
|
for x in tree: |
|
|
print(x) |
|
|
if verify: print("dict " + str(x)) |
|
|
if isinstance(tree[x], dict): |
|
|
if isinstance(tree[x], dict): |
|
|
|
|
|
hasa = self.hasTidyAttr(tree[x]) |
|
|
newTree = {} |
|
|
newTree = {} |
|
|
attr = "" |
|
|
attr = "" |
|
|
for y in tree[x]: |
|
|
for y in tree[x]: |
|
|
|
|
|
if verify: print("y-for "+str(y)) |
|
|
if y[0:1] == "@": |
|
|
if y[0:1] == "@": |
|
|
attr = " "+y[1:]+"=\""+tree[x][y]+"\"" |
|
|
attr = " " + y[1:] + "=\"" + tree[x][y] + "\"" |
|
|
else: |
|
|
else: |
|
|
newTree[y] = tree[x][y] |
|
|
newTree[y] = tree[x][y] |
|
|
if len(newTree) > 0: |
|
|
if len(newTree) > 0: |
|
|
xml += tab+"<"+x+attr+">\n"+self.tidy(newTree, depth+1)+tab+"</"+x+">\n" |
|
|
xml += tab + "<" + x + attr + ">\n" + self.tidy(newTree, depth + 1) + tab + "</" + x + ">\n" |
|
|
else: |
|
|
else: |
|
|
xml += tab+"<"+x+attr+"/>\n" |
|
|
xml += tab + "<" + x + attr + "/>\n" |
|
|
elif isinstance(tree[x], list): |
|
|
elif isinstance(tree[x], list): |
|
|
xml += tab+"<"+x+">\n"+self.tidy(tree[x], depth+1)+tab+"</"+x+">\n" |
|
|
hasa = self.hasTidyAttr(tree[x]) |
|
|
|
|
|
if hasa: |
|
|
|
|
|
xml += tab + "<" + x + self.tidy(tree[x], depth) + "</" + x + ">\n" |
|
|
|
|
|
else: |
|
|
|
|
|
xml += tab + "<" + x + ">\n" + self.tidy(tree[x], depth + 1) + tab + "</" + x + ">\n" |
|
|
|
|
|
elif x[0:1] == "@": |
|
|
|
|
|
attr = " " + x[1:] + "=\"" + tree[x] + "\"" |
|
|
|
|
|
elif x == "#text": |
|
|
|
|
|
xml = str(tree[x]) |
|
|
|
|
|
return xml |
|
|
else: |
|
|
else: |
|
|
xml += tab+"<"+x+">"+str(tree[x])+"</"+x+">\n" |
|
|
xml += tab + "<" + x + ">" + str(tree[x]) + "</" + x + ">\n" |
|
|
elif isinstance(tree, list): |
|
|
elif isinstance(tree, list): |
|
|
# tree_list = { "eins": [ {"zwei": 2, "drei": 3, "vier": 4 } ] } |
|
|
|
|
|
print("list") |
|
|
|
|
|
for x in tree: |
|
|
for x in tree: |
|
|
|
|
|
if verify: print("list " + str(x)) |
|
|
if isinstance(x, dict): |
|
|
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: |
|
|
else: |
|
|
xml += tab+"<"+x+">\n"+self.tidy(x, depth)+tab+"</"+x+">" |
|
|
xml += tab + "<" + x + ">\n" + self.tidy(x, depth) + tab + "</" + x + ">" |
|
|
else: |
|
|
else: |
|
|
xml += "<"+str(tree)+">" |
|
|
xml += "<" + str(tree) + ">" |
|
|
return xml |
|
|
return xml |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def addMerkmal(baum, pfad, index, wert): |
|
|
def addMerkmal(baum, pfad, index, wert): |
|
|
""" |
|
|
""" |
|
|
Eingabe: |
|
|
Eingabe: |
|
|