chiark / gitweb /
Move some code into ChatLogTracker
authorIan Jackson <ian@liberator.relativity.greenend.org.uk>
Sat, 16 May 2009 17:33:39 +0000 (18:33 +0100)
committerIan Jackson <ian@liberator.relativity.greenend.org.uk>
Sat, 16 May 2009 17:33:39 +0000 (18:33 +0100)
yoweb-scrape

index f66dda1f999f434d9c43d728cba07bd77b9967b4..2790ac7c7c66c27acb6ea3a8129c8a7b3ba3d65e 100755 (executable)
@@ -402,7 +402,7 @@ class PirateAboard:
                pa.gunner = False
 
 class ChatLogTracker:
                pa.gunner = False
 
 class ChatLogTracker:
-       def __init__(self, myself_pi):
+       def __init__(self, myself_pi, logfn):
                self._pl = {}   # self._pl['Pirate'] =
                self._vl = {}   #   self._vl['Vessel']['Pirate'] = PirateAboard
                                # self._vl['Vessel']['#lastaboard']
                self._pl = {}   # self._pl['Pirate'] =
                self._vl = {}   #   self._vl['Vessel']['Pirate'] = PirateAboard
                                # self._vl['Vessel']['#lastaboard']
@@ -411,6 +411,9 @@ class ChatLogTracker:
                self._date = None
                self._myself = myself_pi
                self.need_redisplay = False
                self._date = None
                self._myself = myself_pi
                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
 
        def _refresh(self):
                self.need_redisplay = True
@@ -602,25 +605,42 @@ class ChatLogTracker:
                s += '>\n'
                return s
 
                s += '>\n'
                return s
 
-def do_ship_aid(args, bu):
+       def catchup(self, progress=None):
+               while True:
+                       more = self._f.readline()
+                       if not more: return
+
+                       self._progress[0] += len(more)
+                       if progress is not None:
+                               progress(*self._progress)
+
+                       self._lbuf += more
+                       if self._lbuf.endswith('\n'):
+                               self.chatline(self._lbuf.rstrip())
+                               self._lbuf = ''
+
+def do_track_chat_log(args, bu):
        if len(args) != 1: bu('ship-aid takes only chat log filename')
        logfn = args[0]
        logfn_re = '(?:.*/)?([A-Z][a-z]+)_([a-z]+)_chat-log-\\w+$'
        match = regexp.match(logfn_re, logfn)
        if not match: bu('ship-aid chat log filename is not in default format')
        (pirate, fetcher.ocean) = match.groups()
        if len(args) != 1: bu('ship-aid takes only chat log filename')
        logfn = args[0]
        logfn_re = '(?:.*/)?([A-Z][a-z]+)_([a-z]+)_chat-log-\\w+$'
        match = regexp.match(logfn_re, logfn)
        if not match: bu('ship-aid chat log filename is not in default format')
        (pirate, fetcher.ocean) = match.groups()
+
+       track = ChatLogTracker(pirate, logfn)
        myself_pi = PirateInfo(pirate,3600)
        myself_pi = PirateInfo(pirate,3600)
-       track = ChatLogTracker(myself_pi)
-       f = file(logfn)
-       l = ''
+       track = ChatLogTracker(myself_pi, logfn)
+
+       def pprogress(done,total):
+               sys.stdout.write("scan chat logs %3d%%\r" %
+                                       ((done*100) / total))
+               sys.stdout.flush()
+
+       track.catchup(pprogress)
+       sys.stdout.write('                   \r')
+
        while True:
        while True:
-               l += f.readline()
-               if l.endswith('\n'):
-                       track.chatline(l.rstrip())
-                       l = ''
-                       continue
-               if l:
-                       continue
+               track.catchup()
                if track.need_redisplay:
                        print track
                        track.need_redisplay = False
                if track.need_redisplay:
                        print track
                        track.need_redisplay = False
@@ -635,6 +655,7 @@ actions:
  yoweb-scrape [--ocean OCEAN ...] pirate PIRATE
  yoweb-scrape [--ocean OCEAN ...] crew-of PIRATE
  yoweb-scrape [--ocean OCEAN ...] standings-crew-of PIRATE
  yoweb-scrape [--ocean OCEAN ...] pirate PIRATE
  yoweb-scrape [--ocean OCEAN ...] crew-of PIRATE
  yoweb-scrape [--ocean OCEAN ...] standings-crew-of PIRATE
+ yoweb-scrape [--ocean OCEAN ...] track-chat-log CHAT-LOG
  yoweb-scrape [--ocean OCEAN ...] ship-aid CHAT-LOG
 ''')
        ao = pa.add_option
  yoweb-scrape [--ocean OCEAN ...] ship-aid CHAT-LOG
 ''')
        ao = pa.add_option