mail[Wesnoth-commits] r25377 - /trunk/data/tools/wmlunits


Others Months | Index by Date | Thread Index
>>   [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Header


Content

Posted by esr on March 31, 2008 - 15:35:
Author: esr
Date: Mon Mar 31 15:35:23 2008
New Revision: 25377

URL: http://svn.gna.org/viewcvs/wesnoth?rev=25377&view=rev
Log:
Begin to separate unit parsing from report generation.

Modified:
    trunk/data/tools/wmlunits

Modified: trunk/data/tools/wmlunits
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/data/tools/wmlunits?rev=25377&r1=25376&r2=25377&view=diff
==============================================================================
--- trunk/data/tools/wmlunits (original)
+++ trunk/data/tools/wmlunits Mon Mar 31 15:35:23 2008
@@ -18,115 +18,118 @@
 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")
+        # 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)
 
-    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
+        self.units = WML.get_first("+units")
+        self.races = {}
 
-        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 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
 
-    def poname(name):
-        return name[name.find("^") + 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 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'
-        levels = []
-        for i in range(6):
-            levels.append(races[race].get(str(i), []))
+        def poname(name):
+            return name[name.find("^") + 1:]
 
-        row = 0
-        while 1:
-            if use_html: print "<tr>"
-            else: print "|-"
-            ok = False
-            units = []
+        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 @@
         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 @@
         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>"




Related Messages


Powered by MHonArc, Updated Mon Mar 31 16:21:24 2008