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):
352 if use_puzzles is None:
354 use_puzzles=duty_puzzles
357 self._puzzles = use_puzzles
359 self._cw = col_width-1
361 def _pline(self, pirate, puzstrs, extra):
362 self.s += ' %-*s' % (max(max_pirate_namelen, 14), pirate)
364 self.s += ' %-*.*s' % (self._cw,self._cw, v)
366 self.s += ' ' + extra
369 def _puzstr(self, pi, puzzle):
370 if not isinstance(puzzle,list): puzzle = [puzzle]
371 try: standing = max([pi.standings[p] for p in puzzle])
372 except KeyError: return '?'
373 if not standing: return ''
376 c1 = standingvals[standing][0]
377 if standing < 3: c1 = c1.lower() # 3 = Master
381 s += '*' * (standing / 2)
382 s += '+' * (standing % 2)
386 def puzn_redact(name):
387 if isinstance(name,list):
389 ["%.*s" % (self._cw/2, puzn_redact(n))
392 if spc < 0: return name
393 return name[0:min(4,spc)] + name[spc+1:]
394 self._pline('', map(puzn_redact, self._puzzles), None)
395 def literalline(self, line):
396 self.s += line + '\n'
397 def pirate_dummy(self, name, standingstring, extra=None):
398 self._pline(name, standingstring * len(self._puzzles), extra)
399 def pirate(self, pi, extra=None):
400 puzstrs = [self._puzstr(pi,puz) for puz in self._puzzles]
401 self._pline(pi.name, puzstrs, extra)
406 #---------- chat log parser ----------
409 # This is essentially a transparent, dumb, data class.
419 def __init__(pa, pn, v, time, event):
423 pa.last_event = event
424 pa.last_chat_time = None
425 pa.last_chat_chan = None
432 age = now - pa.pi_fetched
433 guide = random.randint(120,240)
436 debug('PirateAboard refresh %d > %d %s' % (
437 age, guide, pa.name))
441 wait = fetcher.need_wait(now, imaginary)
443 debug('PirateAboard fetcher not ready %d' % wait)
445 pa.pi = PirateInfo(pa.name, 600)
449 class ChatLogTracker:
450 # This is quite complex so we make it opaque. Use the
451 # official invokers, accessors etc.
453 def __init__(self, myself_pi, logfn):
454 self._pl = {} # self._pl['Pirate'] =
455 self._vl = {} # self._vl['Vessel']['Pirate'] = PirateAboard
456 # self._vl['Vessel']['#lastinfo']
457 # self._vl['Vessel']['#name']
458 # self._v = self._vl[self._vessel]
460 self._myself = myself_pi
461 self._f = file(logfn)
463 self._progress = [0, os.fstat(self._f.fileno()).st_size]
464 self._disembark_myself()
465 self._need_redisplay = False
466 self._lastvessel = None
468 def _disembark_myself(self):
471 self.force_redisplay()
473 def force_redisplay(self):
474 self._need_redisplay = True
476 def _vessel_updated(self, v, timestamp):
477 v['#lastinfo'] = timestamp
478 self.force_redisplay()
480 def _onboard_event(self,v,timestamp,pirate,event):
481 pa = self._pl.get(pirate, None)
482 if pa is not None and pa.v is v:
483 pa.last_time = timestamp
484 pa.last_event = event
486 if pa is not None: del pa.v[pirate]
487 pa = PirateAboard(pirate, v, timestamp, event)
488 self._pl[pirate] = pa
490 self._vessel_updated(v, timestamp)
493 def _trash_vessel(self, v):
495 if pn.startswith('#'): continue
499 if v is self._v: self._disembark_myself()
500 self.force_redisplay()
502 def _vessel_stale(self, v, timestamp):
503 return timestamp - v['#lastinfo'] > opts.ship_reboard_clearout
505 def _vessel_check_expire(self, v, timestamp):
506 if not self._vessel_stale(v, timestamp):
508 self._debug_line_disposition(timestamp,'',
509 'stale-reset ' + v['#name'])
510 self._trash_vessel(v)
513 def expire_garbage(self, timestamp):
514 for v in self._vl.values():
515 self._vessel_check_expire(v, timestamp)
517 def _vessel_lookup(self, vn, timestamp, dml=[], create=False):
518 v = self._vl.get(vn, None)
520 v = self._vessel_check_expire(v, timestamp)
527 self._vl[vn] = v = { '#name': vn }
528 self._vessel_updated(v, timestamp)
531 def _find_matching_vessel(self, pattern, timestamp, cmdr,
532 dml=[], create=False):
533 # use when a commander pirate `cmdr' specified a vessel
534 # by name `pattern' (either may be None)
535 # if create is true, will create the vessel
536 # record if an exact name is specified
538 if (pattern is not None and
540 and len(pattern.split(' ')) == 2):
543 return self._vessel_lookup(
544 vn, timestamp, dml=dml, create=create)
547 pattern_check = lambda vn: True
549 re = '(?:.* )?%s$' % pattern.lower().replace('*','.+')
550 pattern_check = regexp.compile(re, regexp.I).match
554 cmdr_pa = self._pl.get(cmdr, None)
555 if cmdr_pa: tries.append((cmdr_pa.v, 'cmdr'))
557 tries.append((self._v, 'here'))
560 for (v, dm) in tries:
561 if v is None: dml.append(dm+'?'); continue
564 if not pattern_check(vn):
572 if pattern is not None and '*' in pattern:
575 for (vn,v) in self._vl.iteritems()
576 if not self._vessel_stale(v, timestamp)
579 #debug('CLT-RE /%s/ wanted (%s) searched (%s)' % (
581 # '/'.join(tried_vns),
582 # '/'.join([vn for (vn,v) in search])))
592 def _debug_line_disposition(self,timestamp,l,m):
593 debug('CLT %13s %-40s %s' % (timestamp,m,l))
595 def chatline(self,l):
596 rm = lambda re: regexp.match(re,l)
597 d = lambda m: self._debug_line_disposition(timestamp,l,m)
600 m = rm('=+ (\\d+)/(\\d+)/(\\d+) =+$')
602 self._date = [int(x) for x in m.groups()]
603 self._previous_timestamp = None
604 return d('date '+`self._date`)
606 if self._date is None:
607 return d('date unset')
609 m = rm('\\[(\d\d):(\d\d):(\d\d)\\] ')
611 return d('no timestamp')
614 time_tuple = (self._date +
615 [int(x) for x in m.groups()] +
617 timestamp = time.mktime(time_tuple)
618 if timestamp >= self._previous_timestamp: break
620 self._debug_line_disposition(timestamp,'',
621 'new date '+`self._date`)
623 self._previous_timestamp = timestamp
625 l = l[l.find(' ')+1:]
627 def ob_x(pirate,event):
628 return self._onboard_event(
629 self._v, timestamp, pirate, event)
630 def ob1(did): ob_x(m.group(1), did); return d(did)
631 def oba(did): return ob1('%s %s' % (did, m.group(2)))
633 def disembark(v, timestamp, pirate, event):
635 v, timestamp, pirate, 'leaving '+event)
639 def disembark_me(why):
640 self._disembark_myself()
641 return d('disembark-me '+why)
643 m = rm('Going aboard the (\\S.*\\S)\\.\\.\\.$')
646 pn = self._myself.name
648 v = self._vessel_lookup(vn, timestamp, dm, create=True)
649 self._lastvessel = self._vessel = vn
651 ob_x(pn, 'we boarded')
652 self.expire_garbage(timestamp)
653 return d(' '.join(dm))
656 return d('no vessel')
658 m = rm('(\\w+) has come aboard\\.$')
659 if m: return ob1('boarded');
661 m = rm('You have ordered (\\w+) to do some (\\S.*\\S)\\.$')
663 (who,what) = m.groups()
664 pa = ob_x(who,'ord '+what)
665 if what == 'Gunning':
667 return d('duty order')
669 m = rm('(\\w+) abandoned a (\\S.*\\S) station\\.$')
670 if m: oba('stopped'); return d("end")
672 def chat_core(speaker, chan):
673 try: pa = self._pl[speaker]
674 except KeyError: return 'mystery'
675 if pa.v is not self._v: return 'elsewhere'
676 pa.last_chat_time = timestamp
677 pa.last_chat_chan = chan
678 self.force_redisplay()
683 dm = chat_core(speaker, chan)
684 return d('chat %s %s' % (chan, dm))
686 def chat_metacmd(chan):
687 (cmdr, metacmd) = m.groups()
688 metacmd = regexp.sub('\\s+', ' ', metacmd).strip()
690 '/([ad]) (?:([A-Za-z* ]+)\\s*:)?([A-Za-z ]+)$',
692 if not m2: return chat(chan)
694 (cmd, pattern, targets) = m2.groups()
695 dml = ['cmd', chan, cmd]
697 if cmd == 'a': each = self._onboard_event
698 else: each = disembark
700 if cmdr == self._myself.name:
702 how = 'cmd: %s' % cmd
705 how = 'cmd: %s %s' % (cmd,cmdr)
707 v = self._find_matching_vessel(
708 pattern, timestamp, cmdr, dml, create=True)
711 targets = targets.strip().split(' ')
712 dml.append(`len(targets)`)
713 for target in targets:
714 each(v, timestamp, target.title(), how)
715 self._vessel_updated(v, timestamp)
718 chat_core(cmdr, 'cmd '+chan)
721 m = rm('(\\w+) (?:issued an order|ordered everyone) "')
722 if m: return ob1('general order');
724 m = rm('(\\w+) says, "')
725 if m: return chat('public')
727 m = rm('(\\w+) tells ye, "')
728 if m: return chat('private')
730 m = rm('Ye told (\\w+), "(.*)"$')
731 if m: return chat_metacmd('private')
733 m = rm('(\\w+) flag officer chats, "')
734 if m: return chat('flag officer')
736 m = rm('(\\w+) officer chats, "(.*)"$')
737 if m: return chat_metacmd('officer')
739 m = rm('Ye accepted the offer to job with ')
740 if m: return disembark_me('jobbing')
742 m = rm('Ye hop on the ferry and are whisked away ')
743 if m: return disembark_me('ferry')
745 m = rm('Whisking away to yer home on the magical winds')
746 if m: return disembark_me('home')
748 m = rm('Game over\\. Winners: ([A-Za-z, ]+)\\.$')
750 pl = m.group(1).split(', ')
751 if not self._myself.name in pl:
752 return d('lost melee')
754 if ' ' in pn: continue
756 return d('won melee')
758 m = rm('(\\w+) is eliminated\\!')
759 if m: return ob1('eliminated in fray');
761 m = rm('(\\w+) has driven \w+ from the ship\\!')
762 if m: return ob1('boarder repelled');
764 m = rm('\w+ has bested (\\w+), and turns'+
765 ' to the rest of the ship\\.')
766 if m: return ob1('boarder unrepelled');
768 m = rm('(\\w+) has left the vessel\.')
771 disembark(self._v, timestamp, pirate, 'disembarked')
772 return d('disembarked')
774 return d('not-matched')
776 def _str_vessel(self, vn, v):
777 s = ' vessel %s\n' % vn
778 s += ' '*20 + "%-*s %13s\n" % (
779 max_pirate_namelen, '#lastinfo',
781 assert v['#name'] == vn
782 for pn in sorted(v.keys()):
783 if pn.startswith('#'): continue
786 assert self._pl[pn] == pa
787 s += ' '*20 + "%s %-*s %13s %-30s %13s %s\n" % (
788 (' ','G')[pa.gunner],
789 max_pirate_namelen, pn,
790 pa.last_time, pa.last_event,
791 pa.last_chat_time, pa.last_chat_chan)
795 s = '''<ChatLogTracker
798 ''' % (self._myself.name, self._vessel)
799 assert ((self._v is None and self._vessel is None) or
800 (self._v is self._vl[self._vessel]))
801 if self._vessel is not None:
802 s += self._str_vessel(self._vessel, self._v)
803 for vn in sorted(self._vl.keys()):
804 if vn == self._vessel: continue
805 s += self._str_vessel(vn, self._vl[vn])
809 assert pa.v in self._vl.values()
813 def catchup(self, progress=None):
815 more = self._f.readline()
818 self._progress[0] += len(more)
819 if progress: progress.progress(*self._progress)
822 if self._lbuf.endswith('\n'):
823 self.chatline(self._lbuf.rstrip())
826 debug(self.__str__())
827 if progress: progress.caughtup()
830 rv = self._need_redisplay
831 self._need_redisplay = False
834 # returns our pirate name
835 return self._myself.name
836 def vesselname(self):
837 # returns the vessel name we're aboard or None
839 def lastvesselname(self):
840 # returns the last vessel name we were aboard or None
841 return self._lastvessel
842 def aboard(self, vesselname=True):
843 # returns a list of PirateAboard the vessel
844 # sorted by pirate name
845 # you can pass this None and you'll get []
846 # or True for the current vessel (which is the default)
847 # the returned value is a fresh list of persistent
848 # PirateAboard objects
849 if vesselname is True: v = self._v
850 else: v = self._vl.get(vesselname.title())
851 if v is None: return []
853 for pn in sorted(v.keys())
854 if not pn.startswith('#') ]
856 #---------- implementations of actual operation modes ----------
858 def do_pirate(pirates, bu):
860 for pirate in pirates:
861 info = PirateInfo(pirate)
862 print '%s: %s,' % (`pirate`, info)
865 def prep_crew_of(args, bu, max_age=300):
866 if len(args) != 1: bu('crew-of takes one pirate name')
867 pi = PirateInfo(args[0], max_age)
868 if pi.crew is None: return None
869 return CrewInfo(pi.crew[0], max_age)
871 def do_crew_of(args, bu):
872 ci = prep_crew_of(args, bu)
875 def do_standings_crew_of(args, bu):
876 ci = prep_crew_of(args, bu, 60)
877 tab = StandingsTable()
879 for (rank, members) in ci.crew:
880 if not members: continue
881 tab.literalline('%s:' % rank)
883 pi = PirateInfo(p, random.randint(900,1800))
887 class ProgressPrintPercentage:
888 def __init__(self, f=sys.stdout):
890 def progress_string(self,done,total):
891 return "scan chat logs %3d%%\r" % ((done*100) / total)
892 def progress(self,*a):
893 self._f.write(self.progress_string(*a))
895 def show_init(self, pirate, ocean):
896 print >>self._f, 'Starting up, %s on the %s ocean' % (
902 #----- modes which use the chat log parser are quite complex -----
904 def prep_chat_log(args, bu,
905 progress=ProgressPrintPercentage(),
906 max_myself_age=3600):
907 if len(args) != 1: bu('this action takes only chat log filename')
909 logfn_re = '(?:.*/)?([A-Z][a-z]+)_([a-z]+)_'
910 match = regexp.match(logfn_re, logfn)
911 if not match: bu('chat log filename is not in expected format')
912 (pirate, ocean) = match.groups()
913 fetcher.default_ocean(ocean)
915 progress.show_init(pirate, fetcher.ocean)
916 myself = PirateInfo(pirate,max_myself_age)
917 track = ChatLogTracker(myself, logfn)
920 track.catchup(progress)
923 track.force_redisplay()
925 return (myself, track)
927 def do_track_chat_log(args, bu):
928 (myself, track) = prep_chat_log(args, bu)
935 #----- ship management aid -----
937 class Display_dumb(ProgressPrintPercentage):
939 ProgressPrintPercentage.__init__(self)
945 class Display_overwrite(ProgressPrintPercentage):
947 ProgressPrintPercentage.__init__(self)
949 null = file('/dev/null','w')
950 curses.setupterm(fd=null.fileno())
952 self._clear = curses.tigetstr('clear')
954 self._debug('missing clear!')
955 self.show = Display_dumb.show
958 self._t = {'el':'', 'ed':''}
959 if not self._init_sophisticated():
960 for k in self._t.keys(): self._t[k] = ''
961 self._t['ho'] = self._clear
963 def _debug(self,m): debug('display overwrite: '+m)
965 def _init_sophisticated(self):
966 for k in self._t.keys():
967 s = curses.tigetstr(k)
969 self._t['ho'] = curses.tigetstr('ho')
970 if not self._t['ho']:
971 cup = curses.tigetstr('cup')
972 self._t['ho'] = curses.tparm(cup,0,0)
973 missing = [k for k in self._t.keys() if not self._t[k]]
975 self.debug('missing '+(' '.join(missing)))
981 def wti(k): w(self._t[k])
985 for l in s.rstrip().split('\n'):
995 sys.stdout.write(self._clear)
999 def do_ship_aid(args, bu):
1000 if opts.ship_duty is None: opts.ship_duty = True
1002 displayer = globals()['Display_'+opts.display]()
1004 (myself, track) = prep_chat_log(args, bu, progress=displayer)
1006 displayer.realstart()
1008 if os.isatty(0): kr_create = KeystrokeReader
1009 else: kr_create = DummyKeystrokeReader
1012 kreader = kr_create(0, 10)
1013 ship_aid_core(myself, track, displayer, kreader)
1018 class KeyBasedSorter:
1019 def compar_key_pa(self, pa):
1020 return self.compar_key(pa.pirate_info())
1021 def lsort_pa(self, l):
1022 l.sort(key = self.compar_key_pa)
1024 class NameSorter(KeyBasedSorter):
1025 def compar_key(self, pi): return pi.name
1027 class SkillSorter(NameSorter):
1028 def __init__(self, relevant):
1029 self._want = frozenset(relevant.split('/'))
1031 for p in core_duty_puzzles:
1032 if isinstance(p,basestring): self._avoid.add(p)
1033 else: self._avoid |= set(p)
1034 self._avoid -= self._want
1036 def compar_key(self, pi):
1038 pi.standings.get(puz,-1)
1039 for puz in self._want
1042 -pi.standings.get(puz,standing_limit)
1043 for puz in self._avoid
1046 def negate(x): return -x
1047 debug('compar_key %s bw=%s ba=%s' % (pi.name, `best_want`,
1049 return (-best_want, map(negate, best_avoid), pi.name)
1051 def ship_aid_core(myself, track, displayer, kreader):
1054 vn = track.vesselname()
1055 if vn: return (vn, " on board the %s" % vn)
1056 vn = track.lastvesselname()
1057 if vn: return (vn, " ashore from the %s" % vn)
1058 return (None, " not on a vessel")
1061 if t is None: return ' ' * 22
1062 return " %-4s %-16s" % (format_time_interval(now - t),e)
1064 displayer.show(track.myname() + find_vessel()[1] + '...')
1074 (vn, s) = find_vessel()
1075 s = track.myname() + s
1076 s += " at %s" % time.strftime("%Y-%m-%d %H:%M:%S")
1080 tbl = StandingsTable()
1083 aboard = track.aboard(vn)
1085 sort.lsort_pa(aboard)
1088 pi = pa.pirate_info()
1091 if pa.gunner: xs += 'G '
1093 xs += timeevent(pa.last_time, pa.last_event)
1094 xs += timeevent(pa.last_chat_time, pa.last_chat_chan)
1097 tbl.pirate_dummy(pa.name, rotate_nya[0], xs)
1106 rotate_nya = rotate_nya[1:3] + rotate_nya[0]
1110 elif k == 'g': sort = SkillSorter('Gunning')
1111 elif k == 'c': sort = SkillSorter('Carpentry')
1112 elif k == 's': sort = SkillSorter('Sailing/Rigging')
1113 elif k == 'b': sort = SkillSorter('Bilging')
1114 elif k == 'n': sort = SkillSorter('Navigating')
1115 elif k == 'd': sort = SkillSorter('Battle Navigation')
1116 elif k == 't': sort = SkillSorter('Treasure Haul')
1117 elif k == 'a': sort = NameSorter()
1118 else: pass # unknown key command
1120 #---------- individual keystroke input ----------
1122 class DummyKeystrokeReader:
1123 def __init__(self,fd,timeout_dummy): pass
1124 def stop(self): pass
1125 def getch(self): sleep(1); return None
1126 def info(self): return ' [noninteractive]'
1128 class KeystrokeReader(DummyKeystrokeReader):
1129 def __init__(self, fd, timeout_decisec=0):
1131 self._saved = termios.tcgetattr(fd)
1132 a = termios.tcgetattr(fd)
1133 a[3] &= ~(termios.ECHO | termios.ECHONL |
1134 termios.ICANON | termios.IEXTEN)
1135 a[6][termios.VMIN] = 0
1136 a[6][termios.VTIME] = timeout_decisec
1137 termios.tcsetattr(fd, termios.TCSANOW, a)
1139 termios.tcsetattr(self._fd, termios.TCSANOW, self._saved)
1142 byte = os.read(self._fd, 1)
1143 if not len(byte): return None
1148 #---------- main program ----------
1151 global opts, fetcher
1154 '''usage: .../yoweb-scrape [OPTION...] ACTION [ARGS...]
1156 yoweb-scrape [--ocean OCEAN ...] pirate PIRATE
1157 yoweb-scrape [--ocean OCEAN ...] crew-of PIRATE
1158 yoweb-scrape [--ocean OCEAN ...] standings-crew-of PIRATE
1159 yoweb-scrape [--ocean OCEAN ...] track-chat-log CHAT-LOG
1160 yoweb-scrape [options] ship-aid CHAT-LOG (must be .../PIRATE_OCEAN_chat-log*)
1162 display modes (for --display) apply to ship-aid:
1163 --display=dumb just print new information, scrolling the screen
1164 --display=overwrite use cursor motion, selective clear, etc. to redraw at top
1167 ao('-O','--ocean',dest='ocean', metavar='OCEAN', default=None,
1168 help='select ocean OCEAN')
1169 ao('--cache-dir', dest='cache_dir', metavar='DIR',
1170 default='~/.yoweb-scrape-cache',
1171 help='cache yoweb pages in DIR')
1172 ao('-D','--debug', action='count', dest='debug', default=0,
1173 help='enable debugging output')
1174 ao('--debug-fd', type='int', dest='debug_fd',
1175 help='write any debugging output to specified fd')
1176 ao('-q','--quiet', action='store_true', dest='quiet',
1177 help='suppress warning output')
1178 ao('--display', action='store', dest='display',
1179 type='choice', choices=['dumb','overwrite'],
1180 help='how to display ship aid')
1182 ao('--ship-duty', action='store_true', dest='ship_duty',
1183 help='show ship duty station puzzles')
1184 ao('--all-puzzles', action='store_false', dest='ship_duty',
1185 help='show all puzzles, not just ship duty stations')
1187 (opts,args) = pa.parse_args()
1191 pa.error('need a mode argument')
1193 if opts.debug_fd is not None:
1194 opts.debug_file = os.fdopen(opts.debug_fd, 'w')
1196 opts.debug_file = sys.stdout
1199 mode_fn_name = 'do_' + mode.replace('_','#').replace('-','_')
1200 try: mode_fn = globals()[mode_fn_name]
1201 except KeyError: pa.error('unknown mode "%s"' % mode)
1204 opts.min_max_age = 60
1205 opts.expire_age = 3600
1206 opts.ship_reboard_clearout = 3600
1208 if opts.cache_dir.startswith('~/'):
1209 opts.cache_dir = os.getenv('HOME') + opts.cache_dir[1:]
1211 if opts.display is None:
1212 if ((opts.debug > 0 and opts.debug_fd is None)
1213 or not os.isatty(sys.stdout.fileno())):
1214 opts.display = 'dumb'
1216 opts.display = 'overwrite'
1218 fetcher = Fetcher(opts.ocean, opts.cache_dir)
1220 mode_fn(args[1:], pa.error)