chiark / gitweb /
yoweb-scrape: wip new flag and ocean functionality - do_embargoes
[ypp-sc-tools.db-live.git] / yoweb-scrape
index 53da06e2a7d98847055a8e14d2d83b867726b508..90a5677c50e3ff1b1a01fb5427d98ff5ebf564ca 100755 (executable)
@@ -590,14 +590,67 @@ class IslandExtendedInfo(IslandBasicInfo):
                return `(self.ocean, self.islandid, self.name,
                        self.yoweb_url, self.flagid)`
 
+class IslandFlagInfo(IslandExtendedInfo):
+       # Public data attributes (inherited):
+       #  ocean
+       #  name
+       #  islandid
+       #  yoweb_url
+       #  flagid
+       # Public data attributes (additional):
+       #  flag
+       def __init__(self, ocean, islename):
+               IslandExtendedInfo.__init__(self, ocean, islename)
+               self.flag = None
+               self._collect_flag()
+
+       def _collect_flag(self):
+               if self.flagid is None: return
+               self.flag = FlagInfo(self.flagid, 1800)
+
+       def __str__(self):
+               return IslandExtendedInfo.__str__(self) + '; ' + str(self.flag)
+
+class NullProgressReporter():
+       def start(self): pass
+       def doing(self, msg): pass
+       def stop(self): pass
+
+class TypewriterProgressReporter():
+       def start(self):
+               self._l = 0
+       def doing(self,m):
+               self._doing(m + '...')
+       def _doing(self,m):
+               self._write('\r')
+               self._write(m)
+               less = self._l - len(m)
+               if less > 0:
+                       self._write(' ' * less)
+                       self._write('\b' * less)
+               self._l = len(m)
+               sys.stdout.flush()
+       def stop(self):
+               self._doing('')
+               self._l = 0
+       def _write(self,t):
+               sys.stdout.write(t)
+
 class OceanInfo():
        # Public data attributes:
        #   oi.islands[islename] = IslandInfo(...)
        #   oi.arches[archname][islename] = IslandInfo(...)
-       def __init__(self, isleclass=IslandBasicInfo):
+       def __init__(self, isleclass=IslandBasicInfo, progressreporter=None):
+               if progressreporter is None:
+                       if opts.debug: progressreporter = NullProgressReporter()
+                       else: progressreporter = TypewriterProgressReporter()
+
                self.isleclass = isleclass
                self.ocean = fetcher.ocean.lower().capitalize()
 
+               progressreporter.start()
+               progressreporter.doing('fetching ocean info')
+
                cmdl = ['./yppedia-ocean-scraper']
                if opts.localhtml is not None:
                        cmdl += ['--local-html-dir',opts.localhtml]
@@ -615,10 +668,16 @@ class OceanInfo():
                arch_re = regexp.compile('^ (\S.*)')
                island_re = regexp.compile('^  (\S.*)')
 
+               oscraper.wait()
+               assert(oscraper.returncode == 0)
+
                self.islands = { }
                self.arches = { }
                archname = None
 
+               isles = [ ]
+               progressreporter.doing('parsing ocean info')
+
                for l in oscraper.stdout:
                        debug('OceanInfo collect l '+`l`)
                        l = l.rstrip('\n')
@@ -626,10 +685,7 @@ class OceanInfo():
                        if m:
                                assert(archname is not None)
                                islename = m.group(1)
-                               isle = self.isleclass(self.ocean, islename)
-                               isle.arch = archname
-                               self.islands[islename] = isle
-                               self.arches[archname][islename] = isle
+                               isles.append((archname, islename))
                                continue
                        m = arch_re.match(l)
                        if m:
@@ -638,8 +694,18 @@ class OceanInfo():
                                self.arches[archname] = { }
                                continue
                        assert(False)
-               oscraper.wait()
-               assert(oscraper.returncode == 0)
+
+               for i in xrange(0, len(isles)-1):
+                       (archname, islename) = isles[i]
+                       progressreporter.doing(
+                               'fetching isle info %2d/%d (%s: %s)'
+                               % (i, len(isles), archname, islename))
+                       isle = self.isleclass(self.ocean, islename)
+                       isle.arch = archname
+                       self.islands[islename] = isle
+                       self.arches[archname][islename] = isle
+
+               progressreporter.stop()
 
        def __str__(self):
                return `(self.islands, self.arches)`
@@ -1326,12 +1392,44 @@ def do_standings_crew_of(args, bu):
 def do_ocean(args, bu):
        if (len(args)): bu('ocean takes no further arguments')
        fetcher.default_ocean()
-       oi = OceanInfo(IslandExtendedInfo)
+       oi = OceanInfo(IslandFlagInfo)
        print oi
        for islename in sorted(oi.islands.keys()):
                isle = oi.islands[islename]
                print isle
 
+def do_embargoes(args, bu):
+       if (len(args)): bu('ocean takes no further arguments')
+       fetcher.default_ocean()
+       oi = OceanInfo(IslandFlagInfo)
+       wr = sys.stdout.write
+       print ('EMBARGOES:  Island    | Owning flag'+
+               '                    | Embargoed flags')
+
+       def getflname(isle):
+               if isle.islandid is None: return 'uncolonisable'
+               if isle.flag is None: return 'uncolonised'
+               return isle.flag.name
+
+       for archname in sorted(oi.arches.keys()):
+               print 'ARCHIPELAGO: ',archname
+               for islename in sorted(oi.arches[archname].keys()):
+                       isle = oi.islands[islename]
+                       wr(' %-20s | ' % isle.name)
+                       flname = getflname(isle)
+                       wr('%-30s | ' % flname)
+                       flag = isle.flag
+                       if flag is None: print ''; continue
+                       delim = ''
+                       for rel in flag.relations:
+                               (oname, oid, dummy, thisdeclaring,
+                                       odeclaringmin,odeclaringmax) = rel
+                               if thisdeclaring >= 0: continue
+                               wr(delim)
+                               wr(oname)
+                               delim = '; '
+                       print ''
+
 #----- modes which use the chat log parser are quite complex -----
 
 class ProgressPrintPercentage: