Begin to separate unit parsing from report generation.
This commit is contained in:
parent
f0eba50d9f
commit
2d862f5646
1 changed files with 102 additions and 98 deletions
|
@ -18,115 +18,118 @@ 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)
|
||||
WML = wmldata.DataSub("WML")
|
||||
|
||||
# First, parse through some macro definitions.
|
||||
parser.parse_text("{core/macros/}\n")
|
||||
parser.parse_top(None)
|
||||
# Create a new parser.
|
||||
parser = wmlparser.Parser(datadir)
|
||||
WML = wmldata.DataSub("WML")
|
||||
|
||||
# Now parse the actual text.
|
||||
if text_to_parse:
|
||||
parser.parse_text(text_to_parse)
|
||||
else:
|
||||
parser.parse_file(os.path.join(units_filename))
|
||||
parser.parse_top(WML)
|
||||
# First, parse through some macro definitions.
|
||||
parser.parse_text("{core/macros/}\n")
|
||||
parser.parse_top(None)
|
||||
|
||||
units = WML.get_first("+units")
|
||||
|
||||
doubles = {}
|
||||
races = {}
|
||||
for u in 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" %
|
||||
u.get_text_val("id"))
|
||||
continue
|
||||
if not name in tx:
|
||||
# Hm...
|
||||
sys.stderr.write("Unit %s has no translation (?)\n" % name)
|
||||
if name in doubles:
|
||||
sys.stderr.write("Unit %s found multiple times!\n" % name)
|
||||
continue
|
||||
doubles[name] = 1
|
||||
|
||||
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
|
||||
|
||||
def poname(name):
|
||||
return name[name.find("^") + 1:]
|
||||
|
||||
def place_units(race):
|
||||
if use_html:
|
||||
print "<font size=5>%s</font>" % (race + campaign)
|
||||
print "<table border=solid>"
|
||||
# Now parse the actual text.
|
||||
if text_to_parse:
|
||||
parser.parse_text(text_to_parse)
|
||||
else:
|
||||
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 = []
|
||||
for i in range(6):
|
||||
levels.append(races[race].get(str(i), []))
|
||||
parser.parse_file(os.path.join(units_filename))
|
||||
parser.parse_top(WML)
|
||||
|
||||
row = 0
|
||||
while 1:
|
||||
if use_html: print "<tr>"
|
||||
else: print "|-"
|
||||
ok = False
|
||||
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 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" %
|
||||
u.get_text_val("id"))
|
||||
continue
|
||||
if not name in tx:
|
||||
# Hm...
|
||||
sys.stderr.write("Unit %s has no translation (?)\n" % name)
|
||||
if name in doubles:
|
||||
sys.stderr.write("Unit %s found multiple times!\n" % name)
|
||||
continue
|
||||
doubles[name] = 1
|
||||
|
||||
r = u.get_text_val("race") or "unknown"
|
||||
r = r[0].upper() + r[1:]
|
||||
l = u.get_text_val("level")
|
||||
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:]
|
||||
|
||||
def place_units(race):
|
||||
if use_html:
|
||||
print "<font size=5>%s</font>" % (race + campaign)
|
||||
print "<table border=solid>"
|
||||
else:
|
||||
print '| colspan="6" | <font size=5>%s</font>' % (race + campaign)
|
||||
print '|-'
|
||||
print '| level 0 || level 1 || level 2 || level 3 || level 4 || level 5'
|
||||
self.levels = []
|
||||
for i in range(6):
|
||||
if row < len(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]
|
||||
name = u.get_text_val("name")
|
||||
translated = tx.get(name, "?")
|
||||
if use_html:
|
||||
print "<b>%s</b>" % translated
|
||||
print "<br>"
|
||||
print poname(name)
|
||||
else:
|
||||
print "'''%s''' <br>" % translated,
|
||||
print poname(name),
|
||||
f = u.get_first("female")
|
||||
if f:
|
||||
name = f.get_text_val("name")
|
||||
self.levels.append(self.races[race].get(str(i), []))
|
||||
|
||||
row = 0
|
||||
while 1:
|
||||
if use_html: print "<tr>"
|
||||
else: print "|-"
|
||||
ok = False
|
||||
units = []
|
||||
for i in range(6):
|
||||
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(self.levels[i]):
|
||||
u = self.levels[i][row]
|
||||
name = u.get_text_val("name")
|
||||
translated = tx.get(name, "?")
|
||||
if use_html:
|
||||
print "<br>"
|
||||
print "<b>%s</b>" % translated
|
||||
print "<br>"
|
||||
print poname(name)
|
||||
print poname(name)
|
||||
else:
|
||||
print "<br>",
|
||||
print "'''%s''' <br>" % translated,
|
||||
print poname(name),
|
||||
if use_html: print "</td>"
|
||||
else: print
|
||||
if use_html: print "</tr>"
|
||||
else: print "|-"
|
||||
row += 1
|
||||
if use_html: print "</table>"
|
||||
f = u.get_first("female")
|
||||
if f:
|
||||
name = f.get_text_val("name")
|
||||
translated = tx.get(name, "?")
|
||||
if use_html:
|
||||
print "<br>"
|
||||
print "<b>%s</b>" % translated
|
||||
print "<br>"
|
||||
print poname(name)
|
||||
else:
|
||||
print "<br>",
|
||||
print "'''%s''' <br>" % translated,
|
||||
print poname(name),
|
||||
if use_html: print "</td>"
|
||||
else: print
|
||||
if use_html: print "</tr>"
|
||||
else: print "|-"
|
||||
row += 1
|
||||
if use_html: print "</table>"
|
||||
|
||||
rlist = races.keys()
|
||||
rlist.sort()
|
||||
for race in rlist:
|
||||
place_units(race)
|
||||
rlist = self.races.keys()
|
||||
rlist.sort()
|
||||
for race in rlist:
|
||||
place_units(race)
|
||||
|
||||
if __name__ == '__main__':
|
||||
import getopt
|
||||
|
@ -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>"
|
||||
|
|
Loading…
Add table
Reference in a new issue