From: Ian Jackson Date: Sat, 16 May 2009 17:38:03 +0000 (+0100) Subject: abstract away the progress printing X-Git-Tag: 1.0~58 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~yarrgweb/git?p=ypp-sc-tools.web-live.git;a=commitdiff_plain;h=db96f9717ac132a6102c281bda32bd3dbb6cd12e;hp=bd97786b00269408267fe97ccd3703fe28454dbe abstract away the progress printing --- diff --git a/yoweb-scrape b/yoweb-scrape index 2790ac7..5b2fe98 100755 --- a/yoweb-scrape +++ b/yoweb-scrape @@ -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: