import curses
import termios
from optparse import OptionParser
+from StringIO import StringIO
from BeautifulSoup import BeautifulSoup
#---------- 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):
s += '+' * (standing % 2)
return s
- def headings(self):
+ def headings(self, lhs='', rhs=None):
def puzn_redact(name):
if isinstance(name,list):
return '/'.join(
if spc < 0: return name
return name[0:min(4,spc)] + name[spc+1:]
self._linecount = -2
- self._pline('', map(puzn_redact, self._puzzles), None)
+ 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)
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 ----------
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
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):
class NameSorter(KeyBasedSorter):
def compar_key(self, pi): return pi.name
+ def desc(self): return 'name'
class SkillSorter(NameSorter):
def __init__(self, relevant):
if isinstance(p,basestring): self._avoid.add(p)
else: self._avoid |= set(p)
self._avoid -= self._want
+ self._desc = '%s' % relevant
+ def desc(self): return self._desc
+
def compar_key(self, pi):
best_want = max([
pi.standings.get(puz,-1)
s += kreader.info()
s += '\n'
- tbl = StandingsTable()
- tbl.headings()
-
aboard = track.aboard(vn)
-
sort.lsort_pa(aboard)
+ tbl_s = StringIO()
+ tbl = StandingsTable(tbl_s)
+ tbl.headings(' %d aboard' % len(aboard),
+ ' sorted by '+sort.desc())
+
for pa in aboard:
pi = pa.pirate_info()
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: