<s> from os import environ , path <EOL> from eve import Eve <EOL> from eve . io . mongo import Validator <EOL> from eve . methods . delete import delete , deleteitem <EOL> from eve . methods . post import post_internal <EOL> from eve . methods . put import put <EOL> from flask import current_app as app <EOL> from flask . ext . bootstrap import Bootstrap <EOL> from flask . ext . compress import Compress <EOL> from eve_docs import eve_docs <EOL> from settings import API_NAME , requests , resources <EOL> class KeySchemaValidator ( Validator ) : <EOL> def _validate_dynamicschema ( self , schema , field , dct ) : <EOL> """<STR_LIT>""" <EOL> query = { schema [ '<STR_LIT>' ] : self . document [ schema [ '<STR_LIT>' ] ] } <EOL> res = app . data . find_one ( schema [ '<STR_LIT>' ] , None , ** query ) <EOL> if res : <EOL> dynamic_schema = res [ schema [ '<STR_LIT>' ] ] <EOL> if '<STR_LIT>' in schema : <EOL> dynamic_schema = schema [ '<STR_LIT>' ] ( dynamic_schema ) <EOL> self . _validate_schema ( dynamic_schema , field , dct , None ) <EOL> else : <EOL> self . _error ( field , "<STR_LIT>" % <EOL> ( schema [ '<STR_LIT>' ] , query ) ) <EOL> def _validate_keyschema ( self , schema , field , dct ) : <EOL> "<STR_LIT>" <EOL> for key , value in dct . items ( ) : <EOL> self . _validate_schema ( schema , key , value , None ) <EOL> def _validate_type_point ( self , field , value ) : <EOL> "<STR_LIT>" <EOL> schema = { '<STR_LIT:type>' : { '<STR_LIT:type>' : '<STR_LIT:string>' , '<STR_LIT>' : [ '<STR_LIT>' ] } , <EOL> '<STR_LIT>' : { '<STR_LIT:type>' : '<STR_LIT:list>' , <EOL> '<STR_LIT>' : <NUM_LIT:2> , <EOL> '<STR_LIT>' : <NUM_LIT:2> , <EOL> '<STR_LIT>' : { '<STR_LIT:type>' : '<STR_LIT:float>' } } } <EOL> self . _validate_schema ( schema , field , value , None ) <EOL> if not - <NUM_LIT> <= value [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <= <NUM_LIT> : <EOL> self . _error ( field , "<STR_LIT>" ) <EOL> if not - <NUM_LIT> <= value [ '<STR_LIT>' ] [ <NUM_LIT:1> ] <= <NUM_LIT> : <EOL> self . _error ( field , "<STR_LIT>" ) <EOL> settingsfile = path . join ( path . abspath ( path . dirname ( __file__ ) ) , '<STR_LIT>' ) <EOL> api = Eve ( API_NAME , validator = KeySchemaValidator , settings = settingsfile ) <EOL> Bootstrap ( api ) <EOL> Compress ( api ) <EOL> api . register_blueprint ( eve_docs , url_prefix = '<STR_LIT>' ) <EOL> resource_url = lambda resource : '<STR_LIT:/>' + api . config [ '<STR_LIT>' ] + '<STR_LIT:/>' + resource <EOL> def get_schema ( resource ) : <EOL> "<STR_LIT>" <EOL> return api . config [ '<STR_LIT>' ] [ resource ] [ '<STR_LIT>' ] <EOL> def add_document ( resource , document ) : <EOL> "<STR_LIT>" <EOL> with api . test_request_context ( resource_url ( resource ) ) : <EOL> return post_internal ( resource , payl = document , skip_validation = True ) <EOL> def update_document ( resource , document ) : <EOL> "<STR_LIT>" <EOL> with api . test_request_context ( resource_url ( resource ) ) : <EOL> return put ( resource , payl = document ) <EOL> def delete_document ( resource , document ) : <EOL> "<STR_LIT>" <EOL> with api . test_request_context ( resource_url ( resource ) ) : <EOL> return deleteitem ( resource , document ) <EOL> def delete_documents ( resource ) : <EOL> "<STR_LIT>" <EOL> with api . test_request_context ( resource_url ( resource ) ) : <EOL> return delete ( resource ) <EOL> def register_resource ( resource , schema , source , filt ) : <EOL> """<STR_LIT>""" <EOL> api . register_resource ( resource , { '<STR_LIT>' : resource , <EOL> '<STR_LIT>' : schema , <EOL> '<STR_LIT>' : { '<STR_LIT:source>' : source , <EOL> '<STR_LIT>' : filt } } ) <EOL> def register_resources ( resources , conf ) : <EOL> "<STR_LIT>" <EOL> for res in resources : <EOL> if '<STR_LIT>' in res : <EOL> schema = conf [ '<STR_LIT>' ] <EOL> schema . update ( res [ '<STR_LIT>' ] ) <EOL> register_resource ( res [ '<STR_LIT>' ] , schema , conf [ '<STR_LIT:source>' ] , <EOL> { conf [ '<STR_LIT:key>' ] : res [ conf [ '<STR_LIT:key>' ] ] } ) <EOL> register_services = lambda d : register_resources ( d , requests ) <EOL> register_facilities = lambda d : register_resources ( d , resources ) <EOL> def add_services ( ) : <EOL> "<STR_LIT>" <EOL> with api . app_context ( ) : <EOL> register_services ( api . data . driver . db [ '<STR_LIT>' ] . find ( ) ) <EOL> def add_facilities ( ) : <EOL> "<STR_LIT>" <EOL> with api . app_context ( ) : <EOL> register_facilities ( api . data . driver . db [ '<STR_LIT>' ] . find ( ) ) <EOL> api . on_insert_services += register_services <EOL> api . on_insert_facilities += register_facilities <EOL> add_services ( ) <EOL> add_facilities ( ) <EOL> def main ( ) : <EOL> if '<STR_LIT>' in environ : <EOL> port = int ( environ . get ( '<STR_LIT>' ) ) <EOL> host = '<STR_LIT>' <EOL> else : <EOL> port = <NUM_LIT> <EOL> host = '<STR_LIT:127.0.0.1>' <EOL> api . run ( host = host , port = port ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
