mirror of
https://github.com/wesnoth/wesnoth
synced 2025-05-12 05:30:09 +00:00
Begin to separate unit parsing from report generation.
This commit is contained in:
parent
f0eba50d9f
commit
2d862f5646
@ -18,115 +18,118 @@ import wesnoth.wmldata as wmldata
|
|||||||
import wesnoth.wmlparser as wmlparser
|
import wesnoth.wmlparser as wmlparser
|
||||||
import wesnoth.wmltools as wmltools
|
import wesnoth.wmltools as wmltools
|
||||||
|
|
||||||
def list_units(units_filename, text_to_parse, domain, isocode, campaign):
|
class UnitList:
|
||||||
"List all units in the specified namespace, None = mainline."
|
def __init__(self, units_filename, text_to_parse):
|
||||||
tx = wmltools.Translation(domain, isocode)
|
"Collect all units in the specified namespace, None = mainline."
|
||||||
|
|
||||||
# Create a new parser.
|
# Create a new parser.
|
||||||
parser = wmlparser.Parser(datadir)
|
parser = wmlparser.Parser(datadir)
|
||||||
WML = wmldata.DataSub("WML")
|
WML = wmldata.DataSub("WML")
|
||||||
|
|
||||||
# First, parse through some macro definitions.
|
# First, parse through some macro definitions.
|
||||||
parser.parse_text("{core/macros/}\n")
|
parser.parse_text("{core/macros/}\n")
|
||||||
parser.parse_top(None)
|
parser.parse_top(None)
|
||||||
|
|
||||||
# Now parse the actual text.
|
# Now parse the actual text.
|
||||||
if text_to_parse:
|
if text_to_parse:
|
||||||
parser.parse_text(text_to_parse)
|
parser.parse_text(text_to_parse)
|
||||||
else:
|
|
||||||
parser.parse_file(os.path.join(units_filename))
|
|
||||||
parser.parse_top(WML)
|
|
||||||
|
|
||||||
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>"
|
|
||||||
else:
|
else:
|
||||||
print '| colspan="6" | <font size=5>%s</font>' % (race + campaign)
|
parser.parse_file(os.path.join(units_filename))
|
||||||
print '|-'
|
parser.parse_top(WML)
|
||||||
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), []))
|
|
||||||
|
|
||||||
row = 0
|
self.units = WML.get_first("+units")
|
||||||
while 1:
|
self.races = {}
|
||||||
if use_html: print "<tr>"
|
|
||||||
else: print "|-"
|
def report_units(self, domain, isocode, campaign):
|
||||||
ok = False
|
tx = wmltools.Translation(domain, isocode)
|
||||||
units = []
|
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):
|
for i in range(6):
|
||||||
if row < len(levels[i]):
|
self.levels.append(self.races[race].get(str(i), []))
|
||||||
ok = True
|
|
||||||
if not ok: break
|
row = 0
|
||||||
for i in range(6):
|
while 1:
|
||||||
if use_html: print "<td>"
|
if use_html: print "<tr>"
|
||||||
else: print "|",
|
else: print "|-"
|
||||||
if row < len(levels[i]):
|
ok = False
|
||||||
u = levels[i][row]
|
units = []
|
||||||
name = u.get_text_val("name")
|
for i in range(6):
|
||||||
translated = tx.get(name, "?")
|
if row < len(self.levels[i]):
|
||||||
if use_html:
|
ok = True
|
||||||
print "<b>%s</b>" % translated
|
if not ok: break
|
||||||
print "<br>"
|
for i in range(6):
|
||||||
print poname(name)
|
if use_html: print "<td>"
|
||||||
else:
|
else: print "|",
|
||||||
print "'''%s''' <br>" % translated,
|
if row < len(self.levels[i]):
|
||||||
print poname(name),
|
u = self.levels[i][row]
|
||||||
f = u.get_first("female")
|
name = u.get_text_val("name")
|
||||||
if f:
|
|
||||||
name = f.get_text_val("name")
|
|
||||||
translated = tx.get(name, "?")
|
translated = tx.get(name, "?")
|
||||||
if use_html:
|
if use_html:
|
||||||
print "<br>"
|
|
||||||
print "<b>%s</b>" % translated
|
print "<b>%s</b>" % translated
|
||||||
print "<br>"
|
print "<br>"
|
||||||
print poname(name)
|
print poname(name)
|
||||||
else:
|
else:
|
||||||
print "<br>",
|
|
||||||
print "'''%s''' <br>" % translated,
|
print "'''%s''' <br>" % translated,
|
||||||
print poname(name),
|
print poname(name),
|
||||||
if use_html: print "</td>"
|
f = u.get_first("female")
|
||||||
else: print
|
if f:
|
||||||
if use_html: print "</tr>"
|
name = f.get_text_val("name")
|
||||||
else: print "|-"
|
translated = tx.get(name, "?")
|
||||||
row += 1
|
if use_html:
|
||||||
if use_html: print "</table>"
|
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 = self.races.keys()
|
||||||
rlist.sort()
|
rlist.sort()
|
||||||
for race in rlist:
|
for race in rlist:
|
||||||
place_units(race)
|
place_units(race)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
import getopt
|
import getopt
|
||||||
@ -160,7 +163,8 @@ if __name__ == '__main__':
|
|||||||
print '{| border="solid"'
|
print '{| border="solid"'
|
||||||
|
|
||||||
# Mainline
|
# 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
|
||||||
campaigns = glob.glob("data/campaigns/*")
|
campaigns = glob.glob("data/campaigns/*")
|
||||||
@ -171,8 +175,8 @@ if __name__ == '__main__':
|
|||||||
abbreviation = "".join(abbreviation).lower()
|
abbreviation = "".join(abbreviation).lower()
|
||||||
if abbreviation == "t": abbreviation = "tutorial"
|
if abbreviation == "t": abbreviation = "tutorial"
|
||||||
description = dirname[10:].replace("_", " ")
|
description = dirname[10:].replace("_", " ")
|
||||||
list_units(None, "[+units]{%s/units}[/units]" % dirname,
|
campaign = UnitList(None, "[+units]{%s/units}[/units]" % dirname)
|
||||||
abbreviation, isocode, " - " + description)
|
campaign.report_units(abbreviation, isocode, " - " + description)
|
||||||
|
|
||||||
if use_html:
|
if use_html:
|
||||||
print "</body></html>"
|
print "</body></html>"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user