Begin to separate unit parsing from report generation.

This commit is contained in:
Eric S. Raymond 2008-03-31 13:35:23 +00:00
parent f0eba50d9f
commit 2d862f5646

View File

@ -18,9 +18,9 @@ import wesnoth.wmldata as wmldata
import wesnoth.wmlparser as wmlparser
import wesnoth.wmltools as wmltools
def list_units(units_filename, text_to_parse, domain, isocode, campaign):
"List all units in the specified namespace, None = mainline."
tx = wmltools.Translation(domain, isocode)
class UnitList:
def __init__(self, units_filename, text_to_parse):
"Collect all units in the specified namespace, None = mainline."
# Create a new parser.
parser = wmlparser.Parser(datadir)
@ -37,11 +37,14 @@ def list_units(units_filename, text_to_parse, domain, isocode, campaign):
parser.parse_file(os.path.join(units_filename))
parser.parse_top(WML)
units = WML.get_first("+units")
self.units = WML.get_first("+units")
self.races = {}
def report_units(self, domain, isocode, campaign):
tx = wmltools.Translation(domain, isocode)
doubles = {}
races = {}
for u in units.get_all("unit_type"):
for u in self.units.get_all("unit_type"):
name = u.get_text_val("name")
if name == None or name == "":
sys.stderr.write("Empty name detected! (id = %s)\n" %
@ -58,11 +61,11 @@ def list_units(units_filename, text_to_parse, domain, isocode, campaign):
r = u.get_text_val("race") or "unknown"
r = r[0].upper() + r[1:]
l = u.get_text_val("level")
levels = races.get(r, {})
unitlist = levels.get(l, [])
unitlist.append(u)
levels[l] = unitlist
races[r] = levels
self.levels = self.races.get(r, {})
self.unitlist = self.levels.get(l, [])
self.unitlist.append(u)
self.levels[l] = self.unitlist
self.races[r] = self.levels
def poname(name):
return name[name.find("^") + 1:]
@ -75,9 +78,9 @@ def list_units(units_filename, text_to_parse, domain, isocode, campaign):
print '| colspan="6" | <font size=5>%s</font>' % (race + campaign)
print '|-'
print '| level 0 || level 1 || level 2 || level 3 || level 4 || level 5'
levels = []
self.levels = []
for i in range(6):
levels.append(races[race].get(str(i), []))
self.levels.append(self.races[race].get(str(i), []))
row = 0
while 1:
@ -86,14 +89,14 @@ def list_units(units_filename, text_to_parse, domain, isocode, campaign):
ok = False
units = []
for i in range(6):
if row < len(levels[i]):
if row < len(self.levels[i]):
ok = True
if not ok: break
for i in range(6):
if use_html: print "<td>"
else: print "|",
if row < len(levels[i]):
u = levels[i][row]
if row < len(self.levels[i]):
u = self.levels[i][row]
name = u.get_text_val("name")
translated = tx.get(name, "?")
if use_html:
@ -123,7 +126,7 @@ def list_units(units_filename, text_to_parse, domain, isocode, campaign):
row += 1
if use_html: print "</table>"
rlist = races.keys()
rlist = self.races.keys()
rlist.sort()
for race in rlist:
place_units(race)
@ -160,7 +163,8 @@ if __name__ == '__main__':
print '{| border="solid"'
# Mainline
list_units("data/core/units.cfg", None, "units", isocode, " - mainline")
mainline = UnitList("data/core/units.cfg", None)
mainline.report_units("units", isocode, " - mainline")
# Campaigns
campaigns = glob.glob("data/campaigns/*")
@ -171,8 +175,8 @@ if __name__ == '__main__':
abbreviation = "".join(abbreviation).lower()
if abbreviation == "t": abbreviation = "tutorial"
description = dirname[10:].replace("_", " ")
list_units(None, "[+units]{%s/units}[/units]" % dirname,
abbreviation, isocode, " - " + description)
campaign = UnitList(None, "[+units]{%s/units}[/units]" % dirname)
campaign.report_units(abbreviation, isocode, " - " + description)
if use_html:
print "</body></html>"