3 #---------- setup ----------
6 signal.signal(signal.SIGINT, signal.SIG_DFL)
18 from optparse import OptionParser
20 from BeautifulSoup import BeautifulSoup
24 #---------- YPP parameters and arrays ----------
26 puzzles = ('Swordfighting/Bilging/Sailing/Rigging/Navigating'+
27 '/Battle Navigation/Gunning/Carpentry/Rumble/Treasure Haul'+
28 '/Drinking/Spades/Hearts/Treasure Drop/Poker/Distilling'+
29 '/Alchemistry/Shipwrightery/Blacksmithing/Foraging').split('/')
33 ['Sailing','Rigging'],
38 duty_puzzles = ([ 'Navigating', 'Battle Navigation' ] +
42 standingvals = ('Able/Distinguished/Respected/Master'+
43 '/Renowned/Grand-Master/Legendary/Ultimate').split('/')
44 standing_limit = len(standingvals)
46 pirate_ref_re = regexp.compile('^/yoweb/pirate\\.wm')
48 max_pirate_namelen = 12
51 #---------- general utilities ----------
55 print >>opts.debug_file, m
59 opts.debug_file.flush()
65 def format_time_interval(ti):
66 if ti < 120: return '%d:%02d' % (ti / 60, ti % 60)
67 if ti < 7200: return '%2dm' % (ti / 60)
68 if ti < 86400: return '%dh' % (ti / 3600)
69 return '%dd' % (ti / 86400)
71 #---------- caching and rate-limiting data fetcher ----------
74 def __init__(self, ocean, cachedir):
75 debug('Fetcher init %s' % cachedir)
77 self.cachedir = cachedir
78 try: os.mkdir(cachedir)
79 except (OSError,IOError), oe:
80 if oe.errno != errno.EEXIST: raise
81 self._cache_scan(time.time())
83 def default_ocean(self, ocean='ice'):
84 if self.ocean is None:
87 def _cache_scan(self, now):
88 # returns list of ages, unsorted
90 debug('Fetcher scan_cache')
91 for leaf in os.listdir(self.cachedir):
92 if not leaf.startswith('#'): continue
93 path = self.cachedir + '/' + leaf
94 try: s = os.stat(path)
95 except (OSError,IOError), oe:
96 if oe.errno != errno.ENOENT: raise
98 age = now - s.st_mtime
99 if age > opts.expire_age:
100 debug('Fetcher expire %d %s' % (age, path))
102 except (OSError,IOError), oe:
103 if oe.errno != errno.ENOENT: raise
108 def need_wait(self, now, imaginary=[]):
109 ages = self._cache_scan(now)
112 debug('Fetcher ages ' + `ages`)
116 if age < min_age and age < 300:
117 debug('Fetcher morewait min=%d age=%d' %
119 need_wait = max(need_wait, min_age - age)
124 def _rate_limit_cache_clean(self, now):
125 need_wait = self.need_wait(now)
127 debug('Fetcher wait %d' % need_wait)
130 def fetch(self, url, max_age):
131 debug('Fetcher fetch %s' % url)
132 cache_corename = urllib.quote_plus(url)
133 cache_item = "%s/#%s#" % (self.cachedir, cache_corename)
134 try: f = file(cache_item, 'r')
135 except (OSError,IOError), oe:
136 if oe.errno != errno.ENOENT: raise
139 max_age = max(opts.min_max_age, min(max_age, opts.expire_age))
141 s = os.fstat(f.fileno())
142 age = now - s.st_mtime
144 debug('Fetcher stale %d < %d'% (max_age, age))
149 debug('Fetcher cached %d > %d' % (max_age, age))
152 debug('Fetcher fetch')
153 self._rate_limit_cache_clean(now)
155 stream = urllib2.urlopen(url)
157 cache_tmp = "%s/#%s~%d#" % (
158 self.cachedir, cache_corename, os.getpid())
159 f = file(cache_tmp, 'w')
162 os.rename(cache_tmp, cache_item)
163 debug('Fetcher stored')
166 def yoweb(self, kind, tail, max_age):
168 url = 'http://%s.puzzlepirates.com/yoweb/%s%s' % (
169 self.ocean, kind, tail)
170 return self.fetch(url, max_age)
172 #---------- logging assistance for troubled screenscrapers ----------
179 def soupm(self, obj, m):
180 self.msg(m + '; in ' + `obj`)
181 def needs_msgs(self, child_souplog):
182 self.msgs += child_souplog.msgs
183 child_souplog.msgs = [ ]
186 str = ''.join(obj.findAll(text=True))
189 class SomethingSoupInfo(SoupLog):
190 def __init__(self, kind, tail, max_age):
191 SoupLog.__init__(self)
192 html = fetcher.yoweb(kind, tail, max_age)
193 self._soup = BeautifulSoup(html,
194 convertEntities=BeautifulSoup.HTML_ENTITIES
197 #---------- scraper for pirate pages ----------
199 class PirateInfo(SomethingSoupInfo):
200 # Public data members:
201 # pi.standings = { 'Treasure Haul': 'Able' ... }
203 # pi.crew = (id, name)
204 # pi.flag = (id, name)
205 # pi.msgs = [ 'message describing problem with scrape' ]
207 def __init__(self, pirate, max_age=300):
208 SomethingSoupInfo.__init__(self,
209 'pirate.wm?target=', pirate, max_age)
211 self._find_standings()
212 self.crew = self._find_crewflag('crew',
213 '^/yoweb/crew/info\\.wm')
214 self.flag = self._find_crewflag('flag',
215 '^/yoweb/flag/info\\.wm')
217 def _find_standings(self):
218 imgs = self._soup.findAll('img',
219 src=regexp.compile('/yoweb/images/stat.*'))
221 u'\\s*\\S*/([-A-Za-z]+)\\s*$|\\s*\\S*/\\S*\\s*\\(ocean\\-wide(?:\\s|\\xa0)+([-A-Za-z]+)\\)\\s*$'
225 for skill in puzzles:
226 standings[skill] = [ ]
231 try: puzzle = img['alt']
232 except KeyError: continue
234 if not puzzle in puzzles:
235 skl.soupm(img, 'unknown puzzle: "%s"' % puzzle)
237 key = img.findParent('td')
239 skl.soupm(img, 'puzzle at root! "%s"' % puzzle)
241 valelem = key.findNextSibling('td')
243 skl.soupm(key, 'puzzle missing sibling "%s"'
246 valstr = soup_text(valelem)
247 match = re.match(valstr)
249 skl.soupm(key, ('puzzle "%s" unparseable'+
250 ' standing "%s"') % (puzzle, valstr))
252 standing = match.group(match.lastindex)
253 standings[puzzle].append(standing)
257 for puzzle in puzzles:
258 sl = standings[puzzle]
260 skl.msg('puzzle "%s" multiple standings %s' %
264 skl.msg('puzzle "%s" no standing found' % puzzle)
267 for i in range(0, standing_limit):
268 if standing == standingvals[i]:
269 self.standings[puzzle] = i
270 if not puzzle in self.standings:
271 skl.msg('puzzle "%s" unknown standing "%s"' %
274 all_standings_ok = True
275 for puzzle in puzzles:
276 if not puzzle in self.standings:
279 def _find_crewflag(self, cf, yoweb_re):
280 things = self._soup.findAll('a', href=regexp.compile(yoweb_re))
282 self.msg('zero or several %s id references found' % cf)
285 id_re = '\\b%sid\\=(\\w+)$' % cf
286 id_haystack = thing['href']
287 match = regexp.compile(id_re).search(id_haystack)
289 self.soupm(thing, ('incomprehensible %s id ref'+
290 ' (%s in %s)') % (cf, id_re, id_haystack))
292 name = soup_text(thing)
293 return (match.group(1), name)
296 return `(self.crew, self.flag, self.standings, self.msgs)`
298 #---------- scraper for crew pages ----------
300 class CrewInfo(SomethingSoupInfo):
301 # Public data members:
302 # ci.crew = [ ('Captain', ['Pirate', ...]),
303 # ('Senior Officer', [...]),
305 # pi.msgs = [ 'message describing problem with scrape' ]
307 def __init__(self, crewid, max_age=300):
308 SomethingSoupInfo.__init__(self,
309 'crew/info.wm?crewid=', crewid, max_age)
312 def _find_crew(self):
314 capts = self._soup.findAll('img',
315 src='/yoweb/images/crew-captain.png')
317 self.msg('crew members: no. of captain images != 1')
320 while not tbl.find('a', href=pirate_ref_re):
321 tbl = tbl.findParent('table')
323 self.msg('crew members: cannot find table')
325 current_rank_crew = None
326 crew_rank_re = regexp.compile('/yoweb/images/crew')
327 for row in tbl.contents:
328 # findAll(recurse=False)
329 if isinstance(row,basestring):
332 is_rank = row.find('img', attrs={'src': crew_rank_re})
334 rank = soup_text(row)
335 current_rank_crew = []
336 self.crew.append((rank, current_rank_crew))
338 for cell in row.findAll('a', href=pirate_ref_re):
339 if current_rank_crew is None:
340 self.soupm(cell, 'crew members: crew'
343 current_rank_crew.append(soup_text(cell))
346 return `(self.crew, self.msgs)`
348 #---------- pretty-printer for tables of pirate puzzle standings ----------
350 class StandingsTable:
351 def __init__(self, use_puzzles=None, col_width=6, gap_every=5):
352 if use_puzzles is None:
354 use_puzzles=duty_puzzles
357 self._puzzles = use_puzzles
359 self._cw = col_width-1
360 self._gap_every = gap_every
363 def _pline(self, pirate, puzstrs, extra):
364 if (self._linecount > 0
365 and self._gap_every is not None
366 and not (self._linecount % self._gap_every)):
368 self.s += ' %-*s' % (max(max_pirate_namelen, 14), pirate)
370 self.s += ' %-*.*s' % (self._cw,self._cw, v)
372 self.s += ' ' + extra
376 def _puzstr(self, pi, puzzle):
377 if not isinstance(puzzle,list): puzzle = [puzzle]
378 try: standing = max([pi.standings[p] for p in puzzle])
379 except KeyError: return '?'
380 if not standing: return ''
383 c1 = standingvals[standing][0]
384 if standing < 3: c1 = c1.lower() # 3 = Master
388 s += '*' * (standing / 2)
389 s += '+' * (standing % 2)
392 def headings(self, lhs='', rhs=None):
393 def puzn_redact(name):
394 if isinstance(name,list):
396 ["%.*s" % (self._cw/2, puzn_redact(n))
399 if spc < 0: return name
400 return name[0:min(4,spc)] + name[spc+1:]
402 self._pline(lhs, map(puzn_redact, self._puzzles), rhs)
404 def literalline(self, line):
405 self.s += line + '\n'
407 def pirate_dummy(self, name, standingstring, extra=None):
408 self._pline(name, standingstring * len(self._puzzles), extra)
409 def pirate(self, pi, extra=None):
410 puzstrs = [self._puzstr(pi,puz) for puz in self._puzzles]
411 self._pline(pi.name, puzstrs, extra)
416 #---------- chat log parser ----------
419 # This is essentially a transparent, dumb, data class.
429 def __init__(pa, pn, v, time, event):
433 pa.last_event = event
434 pa.last_chat_time = None
435 pa.last_chat_chan = None
442 age = now - pa.pi_fetched
443 guide = random.randint(120,240)
446 debug('PirateAboard refresh %d > %d %s' % (
447 age, guide, pa.name))
451 wait = fetcher.need_wait(now, imaginary)
453 debug('PirateAboard fetcher not ready %d' % wait)
455 pa.pi = PirateInfo(pa.name, 600)
459 class ChatLogTracker:
460 # This is quite complex so we make it opaque. Use the
461 # official invokers, accessors etc.
463 def __init__(self, myself_pi, logfn):
464 self._pl = {} # self._pl['Pirate'] =
465 self._vl = {} # self._vl['Vessel']['Pirate'] = PirateAboard
466 # self._vl['Vessel']['#lastinfo']
467 # self._vl['Vessel']['#name']
468 # self._v = self._vl[self._vessel]
470 self._myself = myself_pi
471 self._f = file(logfn)
473 self._progress = [0, os.fstat(self._f.fileno()).st_size]
474 self._disembark_myself()
475 self._need_redisplay = False
476 self._lastvessel = None
478 def _disembark_myself(self):
481 self.force_redisplay()
483 def force_redisplay(self):
484 self._need_redisplay = True
486 def _vessel_updated(self, v, timestamp):
487 v['#lastinfo'] = timestamp
488 self.force_redisplay()
490 def _onboard_event(self,v,timestamp,pirate,event):
491 pa = self._pl.get(pirate, None)
492 if pa is not None and pa.v is v:
493 pa.last_time = timestamp
494 pa.last_event = event
496 if pa is not None: del pa.v[pirate]
497 pa = PirateAboard(pirate, v, timestamp, event)
498 self._pl[pirate] = pa
500 self._vessel_updated(v, timestamp)
503 def _trash_vessel(self, v):
505 if pn.startswith('#'): continue
509 if v is self._v: self._disembark_myself()
510 self.force_redisplay()
512 def _vessel_stale(self, v, timestamp):
513 return timestamp - v['#lastinfo'] > opts.ship_reboard_clearout
515 def _vessel_check_expire(self, v, timestamp):
516 if not self._vessel_stale(v, timestamp):
518 self._debug_line_disposition(timestamp,'',
519 'stale-reset ' + v['#name'])
520 self._trash_vessel(v)
523 def expire_garbage(self, timestamp):
524 for v in self._vl.values():
525 self._vessel_check_expire(v, timestamp)
527 def _vessel_lookup(self, vn, timestamp, dml=[], create=False):
528 v = self._vl.get(vn, None)
530 v = self._vessel_check_expire(v, timestamp)
537 self._vl[vn] = v = { '#name': vn }
538 self._vessel_updated(v, timestamp)
541 def _find_matching_vessel(self, pattern, timestamp, cmdr,
542 dml=[], create=False):
543 # use when a commander pirate `cmdr' specified a vessel
544 # by name `pattern' (either may be None)
545 # if create is true, will create the vessel
546 # record if an exact name is specified
548 if (pattern is not None and
550 and len(pattern.split(' ')) == 2):
553 return self._vessel_lookup(
554 vn, timestamp, dml=dml, create=create)
557 pattern_check = lambda vn: True
559 re = '(?:.* )?%s$' % pattern.lower().replace('*','.+')
560 pattern_check = regexp.compile(re, regexp.I).match
564 cmdr_pa = self._pl.get(cmdr, None)
565 if cmdr_pa: tries.append((cmdr_pa.v, 'cmdr'))
567 tries.append((self._v, 'here'))
570 for (v, dm) in tries:
571 if v is None: dml.append(dm+'?'); continue
574 if not pattern_check(vn):
582 if pattern is not None and '*' in pattern:
585 for (vn,v) in self._vl.iteritems()
586 if not self._vessel_stale(v, timestamp)
589 #debug('CLT-RE /%s/ wanted (%s) searched (%s)' % (
591 # '/'.join(tried_vns),
592 # '/'.join([vn for (vn,v) in search])))
602 def _debug_line_disposition(self,timestamp,l,m):
603 debug('CLT %13s %-40s %s' % (timestamp,m,l))
605 def chatline(self,l):
606 rm = lambda re: regexp.match(re,l)
607 d = lambda m: self._debug_line_disposition(timestamp,l,m)
610 m = rm('=+ (\\d+)/(\\d+)/(\\d+) =+$')
612 self._date = [int(x) for x in m.groups()]
613 self._previous_timestamp = None
614 return d('date '+`self._date`)
616 if self._date is None:
617 return d('date unset')
619 m = rm('\\[(\d\d):(\d\d):(\d\d)\\] ')
621 return d('no timestamp')
624 time_tuple = (self._date +
625 [int(x) for x in m.groups()] +
627 timestamp = time.mktime(time_tuple)
628 if timestamp >= self._previous_timestamp: break
630 self._debug_line_disposition(timestamp,'',
631 'new date '+`self._date`)
633 self._previous_timestamp = timestamp
635 l = l[l.find(' ')+1:]
637 def ob_x(pirate,event):
638 return self._onboard_event(
639 self._v, timestamp, pirate, event)
640 def ob1(did): ob_x(m.group(1), did); return d(did)
641 def oba(did): return ob1('%s %s' % (did, m.group(2)))
643 def disembark(v, timestamp, pirate, event):
645 v, timestamp, pirate, 'leaving '+event)
649 def disembark_me(why):
650 self._disembark_myself()
651 return d('disembark-me '+why)
653 m = rm('Going aboard the (\\S.*\\S)\\.\\.\\.$')
656 pn = self._myself.name
658 v = self._vessel_lookup(vn, timestamp, dm, create=True)
659 self._lastvessel = self._vessel = vn
661 ob_x(pn, 'we boarded')
662 self.expire_garbage(timestamp)
663 return d(' '.join(dm))
666 return d('no vessel')
668 m = rm('(\\w+) has come aboard\\.$')
669 if m: return ob1('boarded');
671 m = rm('You have ordered (\\w+) to do some (\\S.*\\S)\\.$')
673 (who,what) = m.groups()
674 pa = ob_x(who,'ord '+what)
675 if what == 'Gunning':
677 return d('duty order')
679 m = rm('(\\w+) abandoned a (\\S.*\\S) station\\.$')
680 if m: oba('stopped'); return d("end")
682 def chat_core(speaker, chan):
683 try: pa = self._pl[speaker]
684 except KeyError: return 'mystery'
685 if pa.v is not self._v: return 'elsewhere'
686 pa.last_chat_time = timestamp
687 pa.last_chat_chan = chan
688 self.force_redisplay()
693 dm = chat_core(speaker, chan)
694 return d('chat %s %s' % (chan, dm))
696 def chat_metacmd(chan):
697 (cmdr, metacmd) = m.groups()
698 metacmd = regexp.sub('\\s+', ' ', metacmd).strip()
700 '/([ad]) (?:([A-Za-z* ]+)\\s*:)?([A-Za-z ]+)$',
702 if not m2: return chat(chan)
704 (cmd, pattern, targets) = m2.groups()
705 dml = ['cmd', chan, cmd]
707 if cmd == 'a': each = self._onboard_event
708 else: each = disembark
710 if cmdr == self._myself.name:
712 how = 'cmd: %s' % cmd
715 how = 'cmd: %s %s' % (cmd,cmdr)
717 v = self._find_matching_vessel(
718 pattern, timestamp, cmdr, dml, create=True)
721 targets = targets.strip().split(' ')
722 dml.append(`len(targets)`)
723 for target in targets:
724 each(v, timestamp, target.title(), how)
725 self._vessel_updated(v, timestamp)
728 chat_core(cmdr, 'cmd '+chan)
731 m = rm('(\\w+) (?:issued an order|ordered everyone) "')
732 if m: return ob1('general order');
734 m = rm('(\\w+) says, "')
735 if m: return chat('public')
737 m = rm('(\\w+) tells ye, "')
738 if m: return chat('private')
740 m = rm('Ye told (\\w+), "(.*)"$')
741 if m: return chat_metacmd('private')
743 m = rm('(\\w+) flag officer chats, "')
744 if m: return chat('flag officer')
746 m = rm('(\\w+) officer chats, "(.*)"$')
747 if m: return chat_metacmd('officer')
749 m = rm('Ye accepted the offer to job with ')
750 if m: return disembark_me('jobbing')
752 m = rm('Ye hop on the ferry and are whisked away ')
753 if m: return disembark_me('ferry')
755 m = rm('Whisking away to yer home on the magical winds')
756 if m: return disembark_me('home')
758 m = rm('Game over\\. Winners: ([A-Za-z, ]+)\\.$')
760 pl = m.group(1).split(', ')
761 if not self._myself.name in pl:
762 return d('lost melee')
764 if ' ' in pn: continue
766 return d('won melee')
768 m = rm('(\\w+) is eliminated\\!')
769 if m: return ob1('eliminated in fray');
771 m = rm('(\\w+) has driven \w+ from the ship\\!')
772 if m: return ob1('boarder repelled');
774 m = rm('\w+ has bested (\\w+), and turns'+
775 ' to the rest of the ship\\.')
776 if m: return ob1('boarder unrepelled');
778 m = rm('(\\w+) has left the vessel\.')
781 disembark(self._v, timestamp, pirate, 'disembarked')
782 return d('disembarked')
784 return d('not-matched')
786 def _str_vessel(self, vn, v):
787 s = ' vessel %s\n' % vn
788 s += ' '*20 + "%-*s %13s\n" % (
789 max_pirate_namelen, '#lastinfo',
791 assert v['#name'] == vn
792 for pn in sorted(v.keys()):
793 if pn.startswith('#'): continue
796 assert self._pl[pn] == pa
797 s += ' '*20 + "%s %-*s %13s %-30s %13s %s\n" % (
798 (' ','G')[pa.gunner],
799 max_pirate_namelen, pn,
800 pa.last_time, pa.last_event,
801 pa.last_chat_time, pa.last_chat_chan)
805 s = '''<ChatLogTracker
808 ''' % (self._myself.name, self._vessel)
809 assert ((self._v is None and self._vessel is None) or
810 (self._v is self._vl[self._vessel]))
811 if self._vessel is not None:
812 s += self._str_vessel(self._vessel, self._v)
813 for vn in sorted(self._vl.keys()):
814 if vn == self._vessel: continue
815 s += self._str_vessel(vn, self._vl[vn])
819 assert pa.v in self._vl.values()
823 def catchup(self, progress=None):
825 more = self._f.readline()
828 self._progress[0] += len(more)
829 if progress: progress.progress(*self._progress)
832 if self._lbuf.endswith('\n'):
833 self.chatline(self._lbuf.rstrip())
836 debug(self.__str__())
837 if progress: progress.caughtup()
840 rv = self._need_redisplay
841 self._need_redisplay = False
844 # returns our pirate name
845 return self._myself.name
846 def vesselname(self):
847 # returns the vessel name we're aboard or None
849 def lastvesselname(self):
850 # returns the last vessel name we were aboard or None
851 return self._lastvessel
852 def aboard(self, vesselname=True):
853 # returns a list of PirateAboard the vessel
854 # sorted by pirate name
855 # you can pass this None and you'll get []
856 # or True for the current vessel (which is the default)
857 # the returned value is a fresh list of persistent
858 # PirateAboard objects
859 if vesselname is True: v = self._v
860 else: v = self._vl.get(vesselname.title())
861 if v is None: return []
863 for pn in sorted(v.keys())
864 if not pn.startswith('#') ]
866 #---------- implementations of actual operation modes ----------
868 def do_pirate(pirates, bu):
870 for pirate in pirates:
871 info = PirateInfo(pirate)
872 print '%s: %s,' % (`pirate`, info)
875 def prep_crew_of(args, bu, max_age=300):
876 if len(args) != 1: bu('crew-of takes one pirate name')
877 pi = PirateInfo(args[0], max_age)
878 if pi.crew is None: return None
879 return CrewInfo(pi.crew[0], max_age)
881 def do_crew_of(args, bu):
882 ci = prep_crew_of(args, bu)
885 def do_standings_crew_of(args, bu):
886 ci = prep_crew_of(args, bu, 60)
887 tab = StandingsTable()
889 for (rank, members) in ci.crew:
890 if not members: continue
892 tab.literalline('%s:' % rank)
894 pi = PirateInfo(p, random.randint(900,1800))
898 class ProgressPrintPercentage:
899 def __init__(self, f=sys.stdout):
901 def progress_string(self,done,total):
902 return "scan chat logs %3d%%\r" % ((done*100) / total)
903 def progress(self,*a):
904 self._f.write(self.progress_string(*a))
906 def show_init(self, pirate, ocean):
907 print >>self._f, 'Starting up, %s on the %s ocean' % (
913 #----- modes which use the chat log parser are quite complex -----
915 def prep_chat_log(args, bu,
916 progress=ProgressPrintPercentage(),
917 max_myself_age=3600):
918 if len(args) != 1: bu('this action takes only chat log filename')
920 logfn_re = '(?:.*/)?([A-Z][a-z]+)_([a-z]+)_'
921 match = regexp.match(logfn_re, logfn)
922 if not match: bu('chat log filename is not in expected format')
923 (pirate, ocean) = match.groups()
924 fetcher.default_ocean(ocean)
926 progress.show_init(pirate, fetcher.ocean)
927 myself = PirateInfo(pirate,max_myself_age)
928 track = ChatLogTracker(myself, logfn)
931 track.catchup(progress)
934 track.force_redisplay()
936 return (myself, track)
938 def do_track_chat_log(args, bu):
939 (myself, track) = prep_chat_log(args, bu)
946 #----- ship management aid -----
948 class Display_dumb(ProgressPrintPercentage):
950 ProgressPrintPercentage.__init__(self)
956 class Display_overwrite(ProgressPrintPercentage):
958 ProgressPrintPercentage.__init__(self)
960 null = file('/dev/null','w')
961 curses.setupterm(fd=null.fileno())
963 self._clear = curses.tigetstr('clear')
965 self._debug('missing clear!')
966 self.show = Display_dumb.show
969 self._t = {'el':'', 'ed':''}
970 if not self._init_sophisticated():
971 for k in self._t.keys(): self._t[k] = ''
972 self._t['ho'] = self._clear
974 def _debug(self,m): debug('display overwrite: '+m)
976 def _init_sophisticated(self):
977 for k in self._t.keys():
978 s = curses.tigetstr(k)
980 self._t['ho'] = curses.tigetstr('ho')
981 if not self._t['ho']:
982 cup = curses.tigetstr('cup')
983 self._t['ho'] = curses.tparm(cup,0,0)
984 missing = [k for k in self._t.keys() if not self._t[k]]
986 self.debug('missing '+(' '.join(missing)))
992 def wti(k): w(self._t[k])
996 for l in s.rstrip().split('\n'):
1005 def realstart(self):
1006 sys.stdout.write(self._clear)
1010 def do_ship_aid(args, bu):
1011 if opts.ship_duty is None: opts.ship_duty = True
1013 displayer = globals()['Display_'+opts.display]()
1015 (myself, track) = prep_chat_log(args, bu, progress=displayer)
1017 displayer.realstart()
1019 if os.isatty(0): kr_create = KeystrokeReader
1020 else: kr_create = DummyKeystrokeReader
1023 kreader = kr_create(0, 10)
1024 ship_aid_core(myself, track, displayer, kreader)
1029 class KeyBasedSorter:
1030 def compar_key_pa(self, pa):
1031 return self.compar_key(pa.pirate_info())
1032 def lsort_pa(self, l):
1033 l.sort(key = self.compar_key_pa)
1035 class NameSorter(KeyBasedSorter):
1036 def compar_key(self, pi): return pi.name
1037 def desc(self): return 'name'
1039 class SkillSorter(NameSorter):
1040 def __init__(self, relevant):
1041 self._want = frozenset(relevant.split('/'))
1043 for p in core_duty_puzzles:
1044 if isinstance(p,basestring): self._avoid.add(p)
1045 else: self._avoid |= set(p)
1046 self._avoid -= self._want
1047 self._desc = '%s' % relevant
1049 def desc(self): return self._desc
1051 def compar_key(self, pi):
1053 pi.standings.get(puz,-1)
1054 for puz in self._want
1057 -pi.standings.get(puz,standing_limit)
1058 for puz in self._avoid
1061 def negate(x): return -x
1062 debug('compar_key %s bw=%s ba=%s' % (pi.name, `best_want`,
1064 return (-best_want, map(negate, best_avoid), pi.name)
1066 def ship_aid_core(myself, track, displayer, kreader):
1069 vn = track.vesselname()
1070 if vn: return (vn, " on board the %s" % vn)
1071 vn = track.lastvesselname()
1072 if vn: return (vn, " ashore from the %s" % vn)
1073 return (None, " not on a vessel")
1076 if t is None: return ' ' * 22
1077 return " %-4s %-16s" % (format_time_interval(now - t),e)
1079 displayer.show(track.myname() + find_vessel()[1] + '...')
1089 (vn, s) = find_vessel()
1090 s = track.myname() + s
1091 s += " at %s" % time.strftime("%Y-%m-%d %H:%M:%S")
1095 aboard = track.aboard(vn)
1096 sort.lsort_pa(aboard)
1098 tbl = StandingsTable()
1099 tbl.headings(' %d aboard' % len(aboard),
1100 ' sorted by '+sort.desc())
1103 pi = pa.pirate_info()
1106 if pa.gunner: xs += 'G '
1108 xs += timeevent(pa.last_time, pa.last_event)
1109 xs += timeevent(pa.last_chat_time, pa.last_chat_chan)
1112 tbl.pirate_dummy(pa.name, rotate_nya[0], xs)
1121 rotate_nya = rotate_nya[1:3] + rotate_nya[0]
1125 elif k == 'g': sort = SkillSorter('Gunning')
1126 elif k == 'c': sort = SkillSorter('Carpentry')
1127 elif k == 's': sort = SkillSorter('Sailing/Rigging')
1128 elif k == 'b': sort = SkillSorter('Bilging')
1129 elif k == 'n': sort = SkillSorter('Navigating')
1130 elif k == 'd': sort = SkillSorter('Battle Navigation')
1131 elif k == 't': sort = SkillSorter('Treasure Haul')
1132 elif k == 'a': sort = NameSorter()
1133 else: pass # unknown key command
1135 #---------- individual keystroke input ----------
1137 class DummyKeystrokeReader:
1138 def __init__(self,fd,timeout_dummy): pass
1139 def stop(self): pass
1140 def getch(self): sleep(1); return None
1141 def info(self): return ' [noninteractive]'
1143 class KeystrokeReader(DummyKeystrokeReader):
1144 def __init__(self, fd, timeout_decisec=0):
1146 self._saved = termios.tcgetattr(fd)
1147 a = termios.tcgetattr(fd)
1148 a[3] &= ~(termios.ECHO | termios.ECHONL |
1149 termios.ICANON | termios.IEXTEN)
1150 a[6][termios.VMIN] = 0
1151 a[6][termios.VTIME] = timeout_decisec
1152 termios.tcsetattr(fd, termios.TCSANOW, a)
1154 termios.tcsetattr(self._fd, termios.TCSANOW, self._saved)
1157 byte = os.read(self._fd, 1)
1158 if not len(byte): return None
1163 #---------- main program ----------
1166 global opts, fetcher
1169 '''usage: .../yoweb-scrape [OPTION...] ACTION [ARGS...]
1171 yoweb-scrape [--ocean OCEAN ...] pirate PIRATE
1172 yoweb-scrape [--ocean OCEAN ...] crew-of PIRATE
1173 yoweb-scrape [--ocean OCEAN ...] standings-crew-of PIRATE
1174 yoweb-scrape [--ocean OCEAN ...] track-chat-log CHAT-LOG
1175 yoweb-scrape [options] ship-aid CHAT-LOG (must be .../PIRATE_OCEAN_chat-log*)
1177 display modes (for --display) apply to ship-aid:
1178 --display=dumb just print new information, scrolling the screen
1179 --display=overwrite use cursor motion, selective clear, etc. to redraw at top
1182 ao('-O','--ocean',dest='ocean', metavar='OCEAN', default=None,
1183 help='select ocean OCEAN')
1184 ao('--cache-dir', dest='cache_dir', metavar='DIR',
1185 default='~/.yoweb-scrape-cache',
1186 help='cache yoweb pages in DIR')
1187 ao('-D','--debug', action='count', dest='debug', default=0,
1188 help='enable debugging output')
1189 ao('--debug-fd', type='int', dest='debug_fd',
1190 help='write any debugging output to specified fd')
1191 ao('-q','--quiet', action='store_true', dest='quiet',
1192 help='suppress warning output')
1193 ao('--display', action='store', dest='display',
1194 type='choice', choices=['dumb','overwrite'],
1195 help='how to display ship aid')
1197 ao('--ship-duty', action='store_true', dest='ship_duty',
1198 help='show ship duty station puzzles')
1199 ao('--all-puzzles', action='store_false', dest='ship_duty',
1200 help='show all puzzles, not just ship duty stations')
1202 ao('--min-cache-reuse', type='int', dest='min_max_age',
1203 metavar='SECONDS', default=60,
1204 help='always reuse cache yoweb data if no older than this')
1206 (opts,args) = pa.parse_args()
1210 pa.error('need a mode argument')
1212 if opts.debug_fd is not None:
1213 opts.debug_file = os.fdopen(opts.debug_fd, 'w')
1215 opts.debug_file = sys.stdout
1218 mode_fn_name = 'do_' + mode.replace('_','#').replace('-','_')
1219 try: mode_fn = globals()[mode_fn_name]
1220 except KeyError: pa.error('unknown mode "%s"' % mode)
1223 opts.expire_age = max(3600, opts.min_max_age)
1225 opts.ship_reboard_clearout = 3600
1227 if opts.cache_dir.startswith('~/'):
1228 opts.cache_dir = os.getenv('HOME') + opts.cache_dir[1:]
1230 if opts.display is None:
1231 if ((opts.debug > 0 and opts.debug_fd is None)
1232 or not os.isatty(sys.stdout.fileno())):
1233 opts.display = 'dumb'
1235 opts.display = 'overwrite'
1237 fetcher = Fetcher(opts.ocean, opts.cache_dir)
1239 mode_fn(args[1:], pa.error)