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._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]
for pn in v:
if pn.startswith('#'): continue
del self._pl[pn]
+ self._refresh()
def expire_garbage(self, timestamp):
for (vn,v) in list(self._vl.iteritems()):
s += '>\n'
return s
-def do_ship_aid(args, bu):
+ def catchup(self, progress=None):
+ while True:
+ more = self._f.readline()
+ if not more: break
+
+ self._progress[0] += len(more)
+ 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')
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)
- track = ChatLogTracker(myself_pi)
- f = file(logfn)
- l = ''
+ track = ChatLogTracker(myself_pi, logfn)
+ track.catchup(ProgressPrintPercentage(sys.stdout))
while True:
- l += f.readline()
- if l.endswith('\n'):
- track.chatline(l.rstrip())
- l = ''
+ track.catchup()
+ if track.need_redisplay:
print track
- continue
- if l:
- continue
- print track
- os.sleep(1)
+ track.need_redisplay = False
+ time.sleep(1)
def main():
global opts, fetcher
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