chiark / gitweb /
Basics seem to work
[ypp-sc-tools.main.git] / yoweb-scrape
index 2d968921ab1eed4023fea7344ce90951f2c7c21d..1f5486e2a656f3c9fd2c8642213bde5b021d2dd4 100755 (executable)
@@ -32,7 +32,7 @@ max_pirate_namelen = 12
 
 
 def debug(m):
-       if opts.debug:
+       if opts.debug > 0:
                print m
 
 class Fetcher:
@@ -331,7 +331,6 @@ class StandingsTable:
                if not isinstance(puzzle,list): puzzle = [puzzle]
                try: standing = max([pi.standings[p] for p in puzzle])
                except KeyError: return '?'
-               if isinstance(standing,basestring): return standing
                if not standing: return ''
                s = ''
                if self._cw > 4:
@@ -356,6 +355,8 @@ class StandingsTable:
                self._pline('', map(puzn_redact, self._puzzles))
        def literalline(self, line):
                self.s += line + '\n'
+       def pirate_dummy(self, name, standingstring):
+               self._pline(name, standingstring * len(self._puzzles))
        def pirate(self, pi):
                puzstrs = [self._puzstr(pi,puz) for puz in self._puzzles]
                self._pline(pi.name, puzstrs)
@@ -393,6 +394,7 @@ def do_standings_crew_of(args, bu):
 
 class PirateAboard:
        # pa.v
+       # pa.name
        # pa.last_time
        # pa.last_event
        # pa.gunner
@@ -401,7 +403,7 @@ class PirateAboard:
        # pa.pi
 
        def __init__(pa, pn, v, time, event):
-               pa.pn = pn
+               pa.name = pn
                pa.v = v
                pa.last_time = time
                pa.last_event = event
@@ -410,8 +412,8 @@ class PirateAboard:
                pa.gunner = False
                pa.pi = None
 
-       def pirate_info(self):
-               if not pa.pi and not fetcher.need_wait(time.time):
+       def pirate_info(pa):
+               if not pa.pi and not fetcher.need_wait(time.time()):
                        pa.pi = PirateInfo(pa.name, 3600)
                return pa.pi
 
@@ -424,13 +426,13 @@ class ChatLogTracker:
                self._vessel = None     #       self._vl[self._vessel]
                self._date = None
                self._myself = myself_pi
-               self.need_redisplay = False
+               self._need_redisplay = False
                self._f = file(logfn)
                self._lbuf = ''
                self._progress = [0, os.fstat(self._f.fileno()).st_size]
 
        def _refresh(self):
-               self.need_redisplay = True
+               self._need_redisplay = True
 
        def _onboard_event(self,timestamp,pirate,event):
                try: pa = self._pl[pirate]
@@ -633,6 +635,22 @@ class ChatLogTracker:
                                self._lbuf = ''
                if progress: progress.caughtup()
 
+       def changed(self):
+               rv = self._need_redisplay
+               self._need_redisplay = False
+               return rv
+       def myname(self):
+               # returns our pirate name
+               return self._myself.name
+       def vessel(self):
+               # returns the vessel we're aboard or None
+               return self._vessel
+       def aboard(self):
+               # returns a list of PirateAboard sorted by name
+               return [ self._v[pn]
+                        for pn in sorted(self._v.keys())
+                        if not pn.startswith('#') ]
+
 class ProgressPrintPercentage:
        def __init__(self, f=sys.stdout): self._f = f
        def progress(self,done,total):
@@ -642,10 +660,9 @@ class ProgressPrintPercentage:
                self._f.write('                   \r')
                self._f.flush()
 
-def simple_printer(x): print x
-
-def run_chat_log(args, bu, progress=ProgressPrintPercentage(),
-               display=simple_printer, max_myself_age=3600):
+def prep_chat_log(args, bu,
+               progress=ProgressPrintPercentage(),
+               max_myself_age=3600):
        if len(args) != 1: bu('this action takes only chat log filename')
        logfn = args[0]
        logfn_re = '(?:.*/)?([A-Z][a-z]+)_([a-z]+)_chat-log-\\w+$'
@@ -653,21 +670,54 @@ def run_chat_log(args, bu, progress=ProgressPrintPercentage(),
        if not match: bu('chat log filename is not in default format')
        (pirate, fetcher.ocean) = match.groups()
        
-       myself_pi = PirateInfo(pirate,max_myself_age)
-       track = ChatLogTracker(myself_pi, logfn)
+       myself = PirateInfo(pirate,max_myself_age)
+       track = ChatLogTracker(myself, logfn)
 
+       opts.debug -= 1
        track.catchup(progress)
+       opts.debug += 1
+
+       return (myself, track)
+
+def do_track_chat_log(args, bu):
+       (myself, track) = prep_chat_log(args, bu)
        while True:
                track.catchup()
-               if track.need_redisplay:
-                       display(track)
-                       track.need_redisplay = False
+               if track.changed():
+                       print track
                time.sleep(1)
 
-def do_track_chat_log(args, bu):
-       run_chat_log(args, bu)
+def do_ship_aid(args, bu):
+       if opts.ship_duty is None: opts.ship_duty = True
+
+       (myself, track) = prep_chat_log(args, bu)
+
+       rotate_nya = '/-\\'
+
+       while True:
+               track.catchup()
+               now = time.time()
+               sys.stdout.write("\n\n%s" % track.myname())
+
+               vn = track.vessel()
+               if vn is None: print " ...?"; return
+
+               print " on board the %s at %s\n" % (
+                       vn, time.strftime("%Y-%m-%d %H:%M:%S"))
 
-#def do_ship_aid(args, bu):
+               tbl = StandingsTable()
+               tbl.headings()
+
+               for pa in track.aboard():
+                       pi = pa.pirate_info()
+                       if pi is None:
+                               tbl.pirate_dummy(pa.name, rotate_nya[0])
+                       else:
+                               tbl.pirate(pi)
+
+               print tbl.results()
+               time.sleep(1)
+               rotate_nya = rotate_nya[1:2] + rotate_nya[0]
 
 def main():
        global opts, fetcher
@@ -687,7 +737,7 @@ actions:
        ao('--cache-dir', dest='cache_dir', metavar='DIR',
                default='~/.yoweb-scrape-cache',
                help='cache yoweb pages in DIR')
-       ao('-D','--debug', action='store_true', dest='debug', default=False,
+       ao('-D','--debug', action='count', dest='debug', default=0,
                help='enable debugging output')
        ao('-q','--quiet', action='store_true', dest='quiet',
                help='suppress warning output')