wesnoth/data/tools/wmlunits

200 lines
6.0 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env python
"""
wmlunits -- List unit names by race and level in either wikimedia or HTML tables
usage: wmlunits [-h] [-l lang]
-h = list as html
-l lang = specify language (as ISO country code)
"""
# Makes things faster on 32-bit systems
try: import psyco; psyco.full()
except ImportError: pass
import sys, os, re, glob
import wesnoth.wmldata as wmldata
import wesnoth.wmlparser as wmlparser
2008-03-31 09:58:49 +00:00
import wesnoth.wmltools as wmltools
class UnitList:
def __init__(self):
self.units_by_campaign = {}
def add(self, units_filename, text_to_parse, campaign):
"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)
# 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)
self.campaign = campaign
# Collect unit data
self.units_by_campaign[campaign] = WML.get_first("+units").get_all("unit_type")
def report_unit_names(campaign, unitlist, isocode):
tx = None
doubles = {}
races = {}
for u in unitlist:
# Fetch name of unit
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
# Swap in the appropriate translation dictionary for this unit
if not u.textdomain:
sys.stderr.write("Unit %s has no textdomain (?)\n" % name)
continue
if tx == None or u.textdomain != tx.textdomain:
tx = wmltools.Translation(u.textdomain, isocode)
# Sanity check
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, {})
ulist = levels.get(l, [])
ulist.append(u)
levels[l] = ulist
races[r] = levels
def poname(name):
return name[name.find("^") + 1:]
def place_units(race):
if use_html:
print "<font size=5>%s - %s</font>" % (race, campaign)
print "<table border=solid>"
else:
print '| colspan="6" | <font size=5>%s - %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), []))
row = 0
while 1:
if use_html: print "<tr>"
else: print "|-"
ok = False
units = []
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")
2008-03-31 10:39:59 +00:00
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)
2008-03-31 09:58:49 +00:00
if __name__ == '__main__':
import getopt
try:
(options, arguments) = getopt.getopt(sys.argv[1:], "hl:?", [
"html",
"lang=",
"usage",
])
except getopt.GetoptError:
help()
sys.exit(1)
isocode = "fr"
use_html = False
for (switch, val) in options:
if switch in ('-h', '--html'):
html = True
elif switch in ('-l', '--lang'):
isocode = val
elif switch in ('-?', '--usage'):
print __doc__
sys.exit(1)
wmltools.pop_to_top("wmlunits")
2008-03-31 09:58:49 +00:00
datadir = os.getcwd() + "/data"
unitlist = UnitList()
# Parse all unit data
unitlist.add("data/core/units.cfg", None, "mainline")
campaigns = glob.glob("data/campaigns/*")
for campaign in campaigns:
dirname = campaign[5:] # strip leading data/
description = dirname[10:].replace("_", " ")
unitlist.add(None,
"[+units]{%s/units}[/units]" % dirname,
description)
# Report generation
if use_html:
print "<html><body>"
else:
print '{| border="solid"'
2008-03-31 09:58:49 +00:00
for (campaign, unitgroup) in unitlist.units_by_campaign.items():
report_unit_names(campaign, unitgroup, isocode)
2008-03-31 09:58:49 +00:00
if use_html:
print "</body></html>"
else:
print "|}"
2008-03-31 09:58:49 +00:00