[Add] resedit: Recursive data loading implemented.

This commit is contained in:
Allanis 2013-04-19 18:55:50 +01:00
parent 15082ba17d
commit 31b1f706e3
4 changed files with 61 additions and 38 deletions

View File

@ -20,44 +20,57 @@ def load(xmlfile, tag, has_name=True, do_array=None, do_special=None):
if(has_name): if(has_name):
name = xmlNode.attributes["name"].value name = xmlNode.attributes["name"].value
# Process the nodes. # Append the element to the dictionary.
for bignode in filter(lambda x: x.nodeType==x.ELEMENT_NODE, xmlNode.childNodes): for bignode in filter(lambda x: x.nodeType==x.ELEMENT_NODE, xmlNode.childNodes):
# Load the nodes. mdic[bignode.nodeName], size = load_Tag(bignode, do_array, do_special)
# Figure out if we need an array or dictionary. dictionary[name] = mdic
if bignode.nodeName in do_array:
dom.unlink()
return dictionary
def load_Tag(node, do_array=None, do_special=None):
i = 0
# Figure out if we need an array or dic.
if node.nodeName in do_array:
array = [] array = []
use_array = True use_array = True
else: else:
section = {} section = {}
use_array = False use_array = False
for node in filter(lambda x: x.nodeType==x.ELEMENT_NODE, for child in filter(lambda x: x.nodeType == x.ELEMENT_NODE, node.childNodes):
bignode.childNodes): n = 0
if node.nodeName not in do_array and \
do_special != None and \
node.nodeName not in do_special.keys():
children, n = load_Tag(child, do_array, do_special)
# Just slap the children on.
if n > 0:
section[child.nodeName] = children
# Big ugly hack to use list instead of array. # Big ugly hack to use list instead of array.
if bignode.nodeName in do_array: elif use_array:
use_array = True array.append(child.firstChild.data)
array.append(node.firstChild.data)
# Uglier hack for special things. # Uglier hack for special things.
elif do_special != None and bignode.nodeName in do_special.keys(): elif do_special != None and node.nodeName in do_special.keys():
section[node.firstChild.data] = node.attributes[do_special[bignode.nodeName]].value section[child.firstChild.data] = \
child.attributes[do_special[node.nodeName]].value
# Normal way (but will overwrite lists). # Normal way (but will overwrite lists).
else: else:
section[node.nodeName] = node.firstChild.data section[child.nodeName] = child.firstChild.data
i = i+1
# Return.
if use_array: if use_array:
mdic[bignode.nodeName] = array return array, i
else: else:
mdic[bignode.nodeName] = section return section, i
# Append the element to the dictionary.
dictionary[name] = mdic
dom.unlink()
return dictionary
def save(xmlfile, data, basetag, tag, has_name=True, do_array=None, do_special=None): def save(xmlfile, data, basetag, tag, has_name=True, do_array=None, do_special=None):
""" """
@ -97,8 +110,17 @@ def save(xmlfile, data, basetag, tag, has_name=True, do_array=None, do_special=N
else: else:
for key3, value3 in value2.items(): for key3, value3 in value2.items():
node2 = xml.createElement(key3) node2 = xml.createElement(key3)
if isinstance(value3, dict):
for key4, value4 in value3.items():
node3 = xml.createElement(key4)
txtnode = xml.createTextNode(str(value4))
node3.appendChild(txtnode)
node2.appendChild(node3)
else:
txtnode = xml.createTextNode(str(value3)) txtnode = xml.createTextNode(str(value3))
node2.appendChild(txtnode) node2.appendChild(txtnode)
node.appendChild(node2) node.appendChild(node2)
elem.appendChild(node) elem.appendChild(node)

Binary file not shown.

View File

@ -104,7 +104,8 @@ class Space:
hooks = { hooks = {
"butNew":["clicked", self.__pnew], "butNew":["clicked", self.__pnew],
"trePlanets":["button-release-event", self.__pupdate], "trePlanets":["button-release-event", self.__pupdate],
"comSystem":["changed", self.__pnewSys] "comSystem":["changed", self.__pnewSys],
"butSave":["clicked", self.savePlanets]
} }
for key, val in hooks.items(): for key, val in hooks.items():
self.__pwidget(key).connect(val[0], val[1]) self.__pwidget(key).connect(val[0], val[1])
@ -273,7 +274,7 @@ class Space:
wgt.pack_start(cell, True) wgt.pack_start(cell, True)
wgt.add_attribute(cell, 'text', 0) wgt.add_attribute(cell, 'text', 0)
wgt.set_model(combo) wgt.set_model(combo)
self.__genPlanerTree() self.__genPlanetTree()
i = 0 i = 0
for row in combo: for row in combo:
if row[0] == self.planetTree[self.cur_planet]: if row[0] == self.planetTree[self.cur_planet]:

Binary file not shown.