<s> import json <EOL> import logging <EOL> from urllib import unquote <EOL> from bottle import Bottle , request , response <EOL> from platformio_api import api , config <EOL> from platformio_api . database import db_session <EOL> from platformio_api . exception import APIBadRequest , APINotFound <EOL> app = Bottle ( ) <EOL> logger = logging . getLogger ( __name__ ) <EOL> @ app . hook ( "<STR_LIT>" ) <EOL> def db_disconnect ( ) : <EOL> db_session . close ( ) <EOL> def finalize_json_response ( handler , kwargs ) : <EOL> assert issubclass ( handler , api . APIBase ) <EOL> response . set_header ( "<STR_LIT>" , <EOL> config [ '<STR_LIT>' ] ) <EOL> response . set_header ( "<STR_LIT>" , <EOL> "<STR_LIT>" ) <EOL> response . set_header ( "<STR_LIT>" , <EOL> "<STR_LIT>" ) <EOL> response . set_header ( "<STR_LIT>" , "<STR_LIT:application/json>" ) <EOL> status = <NUM_LIT:200> <EOL> error = None <EOL> result = None <EOL> try : <EOL> obj = handler ( ** kwargs ) <EOL> result = obj . get_result ( ) <EOL> except APIBadRequest as error : <EOL> status = <NUM_LIT> <EOL> except APINotFound as error : <EOL> status = <NUM_LIT> <EOL> except Exception as e : <EOL> status = <NUM_LIT> <EOL> error = "<STR_LIT>" <EOL> logger . exception ( e ) <EOL> if error : <EOL> item = dict ( <EOL> status = status , <EOL> title = str ( error ) <EOL> ) <EOL> result = dict ( errors = [ item ] ) <EOL> response . status = status <EOL> return json . dumps ( result ) <EOL> @ app . route ( "<STR_LIT:/>" , method = "<STR_LIT>" ) <EOL> def cors ( request ) : <EOL> """<STR_LIT>""" <EOL> response . set_header ( "<STR_LIT>" , <EOL> config [ '<STR_LIT>' ] ) <EOL> response . set_header ( "<STR_LIT>" , <EOL> "<STR_LIT>" ) <EOL> response . set_header ( "<STR_LIT>" , <EOL> "<STR_LIT>" ) <EOL> return None <EOL> @ app . route ( "<STR_LIT>" ) <EOL> def boards ( ) : <EOL> return finalize_json_response ( api . BoardsAPI , { } ) <EOL> @ app . route ( "<STR_LIT>" ) <EOL> def frameworks ( ) : <EOL> return finalize_json_response ( api . FrameworksAPI , { } ) <EOL> @ app . route ( "<STR_LIT>" ) <EOL> def packages ( ) : <EOL> if "<STR_LIT>" in request . headers . get ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> return finalize_json_response ( api . PackagesManifestAPI , { } ) <EOL> else : <EOL> return finalize_json_response ( api . PackagesAPI , { } ) <EOL> @ app . route ( "<STR_LIT>" ) <EOL> def packages_manifest ( ) : <EOL> return finalize_json_response ( api . PackagesManifestAPI , { } ) <EOL> @ app . route ( "<STR_LIT>" ) <EOL> def platforms ( ) : <EOL> return finalize_json_response ( api . PlatformsAPI , { } ) <EOL> @ app . route ( "<STR_LIT>" ) <EOL> def lib_search ( ) : <EOL> args = dict ( <EOL> query = unquote ( request . query . query [ : <NUM_LIT:255> ] ) , <EOL> page = int ( request . query . page ) if request . query . page else <NUM_LIT:0> , <EOL> ) <EOL> return finalize_json_response ( api . LibSearchAPI , args ) <EOL> def lib_search_solr ( ) : <EOL> strict = request . query . strict <EOL> if strict . lower ( ) in [ '<STR_LIT:0>' , '<STR_LIT:false>' , '<STR_LIT>' ] : <EOL> strict = False <EOL> args = dict ( <EOL> query = unquote ( request . query . query [ : <NUM_LIT:255> ] ) , <EOL> page = int ( request . query . page ) if request . query . page else <NUM_LIT:0> , <EOL> strict = bool ( strict ) , <EOL> ) <EOL> return finalize_json_response ( api . LibSearchSolrAPI , args ) <EOL> @ app . route ( "<STR_LIT>" ) <EOL> def lib_examples ( ) : <EOL> args = dict ( <EOL> query = unquote ( request . query . query [ : <NUM_LIT:255> ] ) , <EOL> page = int ( request . query . page ) if request . query . page else <NUM_LIT:0> , <EOL> ) <EOL> return finalize_json_response ( api . LibExamplesAPI , args ) <EOL> @ app . route ( "<STR_LIT>" ) <EOL> def lib_info ( id_ ) : <EOL> return finalize_json_response ( api . LibInfoAPI , dict ( id_ = id_ ) ) <EOL> @ app . route ( "<STR_LIT>" ) <EOL> def lib_download ( id_ ) : <EOL> args = dict ( <EOL> id_ = id_ , <EOL> ip = request . remote_addr , <EOL> version = request . query . version , <EOL> ci = "<STR_LIT>" in request . headers . get ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> ) <EOL> return finalize_json_response ( api . LibDownloadAPI , args ) <EOL> @ app . route ( "<STR_LIT>" ) <EOL> def lib_version ( ids ) : <EOL> ids = [ int ( i ) for i in ids . split ( "<STR_LIT:U+002C>" ) [ : <NUM_LIT:50> ] ] <EOL> return finalize_json_response ( api . LibVersionAPI , dict ( ids = ids ) ) <EOL> @ app . route ( "<STR_LIT>" , method = "<STR_LIT:POST>" ) <EOL> def lib_register ( ) : <EOL> return finalize_json_response ( <EOL> api . LibRegisterAPI , dict ( conf_url = request . forms . get ( "<STR_LIT>" ) ) ) <EOL> @ app . route ( "<STR_LIT>" ) <EOL> def lib_stats ( ) : <EOL> return finalize_json_response ( api . LibStatsAPI , { } ) </s>
