chiark / gitweb /
Print results for standings-crew-of as we get them
authorIan Jackson <ian@liberator.relativity.greenend.org.uk>
Wed, 27 May 2009 23:34:36 +0000 (00:34 +0100)
committerIan Jackson <ian@liberator.relativity.greenend.org.uk>
Wed, 27 May 2009 23:34:36 +0000 (00:34 +0100)
yoweb-scrape

index 4369cab..08ebf8e 100755 (executable)
@@ -16,6 +16,7 @@ import random
 import curses
 import termios
 from optparse import OptionParser
+from StringIO import StringIO
 
 from BeautifulSoup import BeautifulSoup
 
@@ -348,29 +349,32 @@ class CrewInfo(SomethingSoupInfo):
 #---------- pretty-printer for tables of pirate puzzle standings ----------
 
 class StandingsTable:
-       def __init__(self, use_puzzles=None, col_width=6, gap_every=5):
+       def __init__(self, f, use_puzzles=None, col_width=6, gap_every=5):
                if use_puzzles is None:
                        if opts.ship_duty:
                                use_puzzles=duty_puzzles
                        else:
                                use_puzzles=puzzles
                self._puzzles = use_puzzles
-               self.s = ''
+               self.f = f
                self._cw = col_width-1
                self._gap_every = gap_every
                self._linecount = 0
+               self._o = f.write
+
+       def _nl(self): self._o('\n')
 
        def _pline(self, pirate, puzstrs, extra):
                if (self._linecount > 0
                    and self._gap_every is not None
                    and not (self._linecount % self._gap_every)):
-                       self.s += '\n'
-               self.s += ' %-*s' % (max(max_pirate_namelen, 14), pirate)
+                       self._nl()
+               self._o(' %-*s' % (max(max_pirate_namelen, 14), pirate))
                for v in puzstrs:
-                       self.s += ' %-*.*s' % (self._cw,self._cw, v)
+                       self._o(' %-*.*s' % (self._cw,self._cw, v))
                if extra:
-                       self.s += ' ' + extra
-               self.s += '\n'
+                       self._o(' ' + extra)
+               self._nl()
                self._linecount += 1
 
        def _puzstr(self, pi, puzzle):
@@ -402,7 +406,8 @@ class StandingsTable:
                self._pline(lhs, map(puzn_redact, self._puzzles), rhs)
                self._linecount = 0
        def literalline(self, line):
-               self.s += line + '\n'
+               self._o(line)
+               self._nl()
                self._linecount = 0
        def pirate_dummy(self, name, standingstring, extra=None):
                self._pline(name, standingstring * len(self._puzzles), extra)
@@ -410,8 +415,6 @@ class StandingsTable:
                puzstrs = [self._puzstr(pi,puz) for puz in self._puzzles]
                self._pline(pi.name, puzstrs, extra)
 
-       def results(self):
-               return self.s
 
 #---------- chat log parser ----------
 
@@ -884,7 +887,7 @@ def do_crew_of(args, bu):
 
 def do_standings_crew_of(args, bu):
        ci = prep_crew_of(args, bu, 60)
-       tab = StandingsTable()
+       tab = StandingsTable(sys.stdout)
        tab.headings()
        for (rank, members) in ci.crew:
                if not members: continue
@@ -893,7 +896,6 @@ def do_standings_crew_of(args, bu):
                for p in members:
                        pi = PirateInfo(p, random.randint(900,1800))
                        tab.pirate(pi)
-       print tab.results()
 
 class ProgressPrintPercentage:
        def __init__(self, f=sys.stdout):
@@ -1095,7 +1097,8 @@ def ship_aid_core(myself, track, displayer, kreader):
                aboard = track.aboard(vn)
                sort.lsort_pa(aboard)
 
-               tbl = StandingsTable()
+               tbl_s = StringIO()
+               tbl = StandingsTable(tbl_s)
                tbl.headings(' %d aboard' % len(aboard),
                                '  sorted by '+sort.desc())
 
@@ -1113,8 +1116,9 @@ def ship_aid_core(myself, track, displayer, kreader):
                        else:
                                tbl.pirate(pi, xs)
 
-               s += tbl.results()
+               s += tbl_s.getvalue()
                displayer.show(s)
+               tbl_s.close()
 
                k = kreader.getch()
                if k is None: