|
|
@ -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+"</"+x+">" |
|
|
|
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 + "</" + 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): |
|
|
|
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+"</"+x+">\n" |
|
|
|
xml += tab + "<" + x + attr + ">\n" + self.tidy(newTree, depth + 1) + tab + "</" + x + ">\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+"</"+x+">\n" |
|
|
|
hasa = self.hasTidyAttr(tree[x]) |
|
|
|
if hasa: |
|
|
|
xml += tab + "<" + x + self.tidy(tree[x], depth) + "</" + x + ">\n" |
|
|
|
else: |
|
|
|
xml += tab+"<"+x+">"+str(tree[x])+"</"+x+">\n" |
|
|
|
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: |
|
|
|
xml += tab + "<" + x + ">" + str(tree[x]) + "</" + 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: |
|
|
|
xml += tab+"<"+x+">\n"+self.tidy(x, depth)+tab+"</"+x+">" |
|
|
|
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 += "<"+str(tree)+">" |
|
|
|
xml += attr + ">"+text |
|
|
|
else: |
|
|
|
xml += self.tidy(newTree, depth) |
|
|
|
else: |
|
|
|
xml += tab + "<" + x + ">\n" + self.tidy(x, depth) + tab + "</" + x + ">" |
|
|
|
else: |
|
|
|
xml += "<" + str(tree) + ">" |
|
|
|
return xml |
|
|
|
|
|
|
|
|
|
|
|
def addMerkmal(baum, pfad, index, wert): |
|
|
|
""" |
|
|
|
Eingabe: |
|
|
|