<s> """<STR_LIT>""" <EOL> from datetime import datetime , timedelta <EOL> from girder . plugins . minerva . geonames import import_data <EOL> from pymongo import GEOSPHERE <EOL> from bson . objectid import ObjectId <EOL> from girder . api . rest import Resource , loadmodel , RestException <EOL> from girder . api . describe import Description <EOL> from girder . api import access <EOL> from girder . utility . progress import ProgressContext <EOL> from girder . constants import AccessType <EOL> class Geonames ( Resource ) : <EOL> """<STR_LIT>""" <EOL> _geonames_folder = None <EOL> def __init__ ( self ) : <EOL> """<STR_LIT>""" <EOL> self . resourceName = '<STR_LIT>' <EOL> def geonames_folder ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _geonames_folder <EOL> if self . _geonames_folder is None : <EOL> self . _geonames_folder = ObjectId ( <EOL> self . model ( '<STR_LIT>' ) . get ( <EOL> '<STR_LIT>' , None <EOL> ) <EOL> ) <EOL> return self . _geonames_folder <EOL> def _progress_adapter ( self , ctx , unknown = False ) : <EOL> """<STR_LIT>""" <EOL> def progress ( count , total , message , units ) : <EOL> """<STR_LIT>""" <EOL> now = datetime . utcnow ( ) <EOL> if unknown : <EOL> total = <NUM_LIT:0> <EOL> ctx . update ( <EOL> total = total , <EOL> current = count , <EOL> message = message , <EOL> expires = now + timedelta ( minutes = <NUM_LIT:5> ) <EOL> ) <EOL> return progress <EOL> @ access . admin <EOL> @ loadmodel ( <EOL> model = '<STR_LIT>' , <EOL> map = { '<STR_LIT>' : '<STR_LIT>' } , <EOL> level = AccessType . ADMIN <EOL> ) <EOL> def setup ( self , folder , params ) : <EOL> """<STR_LIT>""" <EOL> progress = self . boolParam ( '<STR_LIT>' , params , default = False ) <EOL> self . model ( '<STR_LIT>' ) . createItem ( <EOL> '<STR_LIT>' , self . getCurrentUser ( ) , folder , <EOL> description = datetime . utcnow ( ) . isoformat ( ) <EOL> ) <EOL> with ProgressContext ( progress , user = self . getCurrentUser ( ) , <EOL> title = u'<STR_LIT>' ) as ctx : <EOL> import_data . download_all_countries ( <EOL> progress = self . _progress_adapter ( ctx ) , <EOL> url = params . get ( '<STR_LIT:url>' ) <EOL> ) <EOL> ctx . update ( message = '<STR_LIT>' , force = True ) <EOL> with ProgressContext ( progress , user = self . getCurrentUser ( ) , <EOL> title = u'<STR_LIT>' ) as ctx : <EOL> import_data . read_geonames ( <EOL> folder , self . getCurrentUser ( ) , <EOL> progress = self . _progress_adapter ( ctx , unknown = True ) <EOL> ) <EOL> ctx . update ( message = '<STR_LIT>' , force = True ) <EOL> with ProgressContext ( progress , user = self . getCurrentUser ( ) , <EOL> title = u'<STR_LIT>' ) as ctx : <EOL> self . model ( '<STR_LIT>' ) . collection . ensure_index ( [ ( <EOL> '<STR_LIT>' , <EOL> GEOSPHERE <EOL> ) ] ) <EOL> self . model ( '<STR_LIT>' ) . collection . ensure_index ( '<STR_LIT>' ) <EOL> self . model ( '<STR_LIT>' ) . collection . ensure_index ( '<STR_LIT>' ) <EOL> ctx . update ( message = '<STR_LIT>' , force = True ) <EOL> self . model ( '<STR_LIT>' ) . createItem ( <EOL> '<STR_LIT>' , self . getCurrentUser ( ) , folder , <EOL> description = datetime . utcnow ( ) . isoformat ( ) <EOL> ) <EOL> setup . description = ( <EOL> Description ( '<STR_LIT>' ) <EOL> . param ( '<STR_LIT>' , '<STR_LIT>' , required = True ) <EOL> . param ( '<STR_LIT:url>' , '<STR_LIT>' , required = False ) <EOL> . param ( '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> required = False , <EOL> dataType = '<STR_LIT>' ) <EOL> ) <EOL> @ access . public <EOL> def geocode ( self , params ) : <EOL> """<STR_LIT>""" <EOL> folder = self . geonames_folder ( ) <EOL> if folder is None : <EOL> raise RestException ( '<STR_LIT>' ) <EOL> results = list ( self . model ( '<STR_LIT>' ) . textSearch ( <EOL> params . get ( '<STR_LIT:name>' ) , <EOL> user = self . getCurrentUser ( ) , <EOL> filters = { '<STR_LIT>' : folder } , <EOL> limit = params . get ( '<STR_LIT>' , <NUM_LIT:10> ) <EOL> ) ) <EOL> geo = { <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ ] <EOL> } <EOL> for result in results : <EOL> result [ '<STR_LIT>' ] = result . pop ( '<STR_LIT>' ) <EOL> result [ '<STR_LIT:id>' ] = result [ '<STR_LIT>' ] . pop ( '<STR_LIT>' ) <EOL> geo [ '<STR_LIT>' ] . append ( result ) <EOL> return geo <EOL> geocode . description = ( <EOL> Description ( '<STR_LIT>' ) <EOL> . param ( '<STR_LIT:name>' , '<STR_LIT>' , required = True ) <EOL> . param ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> required = False , <EOL> dataType = '<STR_LIT>' <EOL> ) <EOL> ) </s>
