[Add] resedit: Load/save systems, Lacking some features.

This commit is contained in:
Allanis 2013-04-16 12:47:59 +01:00
parent 046f7d8eb9
commit 5346dc540b
4 changed files with 41 additions and 15 deletions

View File

@ -9,7 +9,7 @@ def uniq(alist): # Fastest order preserving.
for a in s:
alist.append(a)
def load(xmlfile, tag, has_name=True, do_array=None):
def load(xmlfile, tag, has_name=True, do_array=None, do_special=None):
dom = minidom.parse(xmlfile)
xmlNodes = dom.getElementsByTagName(tag)
@ -23,16 +23,32 @@ def load(xmlfile, tag, has_name=True, do_array=None):
# Process the nodes.
for bignode in filter(lambda x: x.nodeType==x.ELEMENT_NODE, xmlNode.childNodes):
# Load the nodes.
section = {}
array = []
# Figure out if we need an array or dictionary.
if bignode.nodeName in do_array:
array = []
use_array = True
else:
section = {}
use_array = False
for node in filter(lambda x: x.nodeType==x.ELEMENT_NODE,
bignode.childNodes):
if bignode.nodeName in do_array: # Big ugly hack to use list instead of array.
array.append(node.firstChild.data)
else: # Normal way (but will overwrite lists).
section[node.nodeName] = node.firstChild.data
if len(array) > 0:
# Big ugly hack to use list instead of array.
if bignode.nodeName in do_array:
use_array = True
array.append(node.firstChild.data)
# Uglier hack for special things.
elif do_special != None and bignode.nodeName in do_special.keys():
section[node.firstChild.data] = node.attributes[do_special[bignode.nodeName]].value
# Normal way (but will overwrite lists).
else:
section[node.nodeName] = node.firstChild.data
if use_array:
mdic[bignode.nodeName] = array
else:
mdic[bignode.nodeName] = section
@ -43,7 +59,7 @@ def load(xmlfile, tag, has_name=True, do_array=None):
dom.unlink()
return dictionary
def save(xmlfile, data, basetag, tag, has_name=True, do_array=None):
def save(xmlfile, data, basetag, tag, has_name=True, do_array=None, do_special=None):
"""
do_array is a DICTIONARY, not a list here
"""
@ -64,7 +80,16 @@ def save(xmlfile, data, basetag, tag, has_name=True, do_array=None):
if do_array != None and key2 in do_array.keys():
for text in value2:
node2 = xml.createElement(do_array[key2])
txtnode = xml.createTextNode(text)
txtnode = xml.createTextNode(str(text))
node2.appendChild(txtnode)
node.appendChild(node2)
# Check to see if we need to run the ULTRA_UBER_HACK.
elif do_special != None and key2 in do_special.keys():
for key3, value3 in value2.items():
node2 = xml.createElement(do_special[key2][0])
node2.setAttribute(do_special[key2][1], value3)
txtnode = xml.createTextNode(str(key3))
node2.appendChild(txtnode)
node.appendChild(node2)
@ -72,7 +97,7 @@ def save(xmlfile, data, basetag, tag, has_name=True, do_array=None):
else:
for key3, value3 in value2.items():
node2 = xml.createElement(key3)
txtnode = xml.createTextNode(value3)
txtnode = xml.createTextNode(str(value3))
node2.appendChild(txtnode)
node.appendChild(node2)

Binary file not shown.

View File

@ -21,12 +21,13 @@ class space:
self.loadPlanets(self.planetsXML)
def loadSystems(self, xmlfile):
self.systems = data.load(xmlfile, "ssys", True, ["jumps","fleets","planets"])
self.systems = data.load(xmlfile, "ssys", True,
["jumps", "planets"], {"fleets":"chance"} )
def saveSystems(self, xmlfile):
data.save("ssys.xml", self.systems, "Systems", "ssys", True,
{ "jumps":"jump", "fleets":"fleet", "planets":"planet" })
data.save( "ssys.xml", self.systems, "Systems", "ssys", True,
{"jumps":"jump","planets":"planet"}, {"fleets":["fleet","chance"]})
def loadPlanets(self, xmlfile):
self.planets = data.load(xmlfile, "planet", True, ["commodities"])
@ -383,7 +384,7 @@ class space:
name = "untitled"
gen = { "asteroids":0, "interference":0, "stars":100 }
pos = { "x":0, "y":0 }
new_ssys = { "general":gen, "pos":pos, "jumps":[], "fleets":[], "planets":[] }
new_ssys = { "general":gen, "pos":pos, "jumps":[], "fleets":{}, "planets":[] }
self.systems[name] = new_ssys
self.__create_treSystems()
self.__selSys(name)

Binary file not shown.