chiark / gitweb /
abstract away the progress printing
authorIan Jackson <ian@liberator.relativity.greenend.org.uk>
Sat, 16 May 2009 17:38:03 +0000 (18:38 +0100)
committerIan Jackson <ian@liberator.relativity.greenend.org.uk>
Sat, 16 May 2009 17:38:03 +0000 (18:38 +0100)
yoweb-scrape

index 2790ac7..5b2fe98 100755 (executable)
@@ -608,16 +608,25 @@ class ChatLogTracker:
        def catchup(self, progress=None):
                while True:
                        more = self._f.readline()
-                       if not more: return
+                       if not more: break
 
                        self._progress[0] += len(more)
-                       if progress is not None:
-                               progress(*self._progress)
+                       if progress: progress.progress(*self._progress)
 
                        self._lbuf += more
                        if self._lbuf.endswith('\n'):
                                self.chatline(self._lbuf.rstrip())
                                self._lbuf = ''
+               if progress: progress.caughtup()
+
+class ProgressPrintPercentage:
+       def __init__(self, f): self._f = f
+       def progress(self,done,total):
+               self._f.write("scan chat logs %3d%%\r" % ((done*100) / total))
+               self._f.flush()
+       def caughtup(self):
+               self._f.write('                   \r')
+               self._f.flush()
 
 def do_track_chat_log(args, bu):
        if len(args) != 1: bu('ship-aid takes only chat log filename')
@@ -630,15 +639,7 @@ def do_track_chat_log(args, bu):
        track = ChatLogTracker(pirate, logfn)
        myself_pi = PirateInfo(pirate,3600)
        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')
-
+       track.catchup(ProgressPrintPercentage(sys.stdout))
        while True:
                track.catchup()
                if track.need_redisplay: