<s> """<STR_LIT>""" <EOL> __author__ = '<STR_LIT>' <EOL> import json <EOL> import logging <EOL> import os <EOL> import sys <EOL> import time <EOL> from functools import partial <EOL> from operator import attrgetter <EOL> from tornado import httpclient , ioloop , options <EOL> from viewfinder . backend . base import util <EOL> from viewfinder . resources . geodata . geoprocessor import GeoProcessor <EOL> options . define ( '<STR_LIT>' , default = '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> options . define ( '<STR_LIT>' , default = <NUM_LIT:10> , help = '<STR_LIT>' ) <EOL> class GeoLookup ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , io_loop , geoproc ) : <EOL> self . _io_loop = io_loop <EOL> self . _geoproc = geoproc <EOL> self . _http_client = httpclient . AsyncHTTPClient ( ) <EOL> try : <EOL> with open ( options . options . lookupdb , '<STR_LIT:r>' ) as rf : <EOL> self . _lookup_db = json . load ( rf ) <EOL> except : <EOL> self . _lookup_db = { } <EOL> def RefreshDB ( self , callback ) : <EOL> """<STR_LIT>""" <EOL> def _OnLookup ( city , barrier_cb , response ) : <EOL> if response . code != <NUM_LIT:200> : <EOL> logging . error ( '<STR_LIT>' % response ) <EOL> else : <EOL> try : <EOL> json_response = json . loads ( response . body ) <EOL> components = [ ] <EOL> if len ( json_response [ '<STR_LIT>' ] ) > <NUM_LIT:0> : <EOL> for comp in json_response [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] : <EOL> for t in comp [ '<STR_LIT>' ] : <EOL> if t in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> components . append ( comp ) <EOL> break <EOL> self . _lookup_db [ city . id ] = components <EOL> except : <EOL> logging . exception ( '<STR_LIT>' % response . body ) <EOL> barrier_cb ( ) <EOL> with util . Barrier ( callback ) as b : <EOL> def _ProcessCities ( cities ) : <EOL> start_time = time . time ( ) <EOL> lookup_count = <NUM_LIT:0> <EOL> for index in xrange ( len ( cities ) ) : <EOL> city = cities [ index ] <EOL> if city . id not in self . _lookup_db : <EOL> lat , lon = float ( city . lat ) , float ( city . lon ) <EOL> logging . info ( '<STR_LIT>' % ( city . name , lat , lon ) ) <EOL> if lookup_count / ( <NUM_LIT:1.0> + time . time ( ) - start_time ) > options . options . query_rate : <EOL> logging . info ( '<STR_LIT>' ) <EOL> return self . _io_loop . add_timeout ( time . time ( ) + <NUM_LIT:1.0> , partial ( _ProcessCities , cities [ index : ] ) ) <EOL> lookup_count += <NUM_LIT:1> <EOL> self . _http_client . fetch ( '<STR_LIT>' % <EOL> ( lat , lon ) , callback = partial ( _OnLookup , city , b . Callback ( ) ) , method = '<STR_LIT:GET>' ) <EOL> cities = self . _geoproc . GetCities ( ) <EOL> _ProcessCities ( cities ) <EOL> def SaveDB ( self ) : <EOL> """<STR_LIT>""" <EOL> tmp_file = options . options . lookupdb + '<STR_LIT>' <EOL> try : <EOL> with open ( tmp_file , '<STR_LIT:w>' ) as wf : <EOL> json . dump ( self . _lookup_db , wf ) <EOL> os . rename ( tmp_file , options . options . lookupdb ) <EOL> except : <EOL> logging . exception ( '<STR_LIT>' ) <EOL> os . unlink ( tmp_file ) <EOL> def main ( ) : <EOL> io_loop = ioloop . IOLoop . instance ( ) <EOL> options . parse_command_line ( ) <EOL> geoproc = GeoProcessor ( ) <EOL> geolookup = GeoLookup ( io_loop , geoproc ) <EOL> def _OnRefresh ( ) : <EOL> geolookup . SaveDB ( ) <EOL> io_loop . stop ( ) <EOL> geolookup . RefreshDB ( _OnRefresh ) <EOL> io_loop . start ( ) <EOL> return <NUM_LIT:0> <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> sys . exit ( main ( ) ) </s>
