<s> import functools <EOL> import os <EOL> import os . path <EOL> from datetime import timedelta <EOL> from functools import update_wrapper <EOL> from flask import Flask , abort , current_app , jsonify , make_response , request <EOL> import psycopg2 <EOL> import rollbar <EOL> import dbcreds <EOL> QUERY_FORMAT = """<STR_LIT>""" <EOL> app = Flask ( __name__ ) <EOL> def setup_rollbar ( environment ) : <EOL> if '<STR_LIT>' not in os . environ : <EOL> return <EOL> rollbar . init ( os . environ [ '<STR_LIT>' ] , environment , <EOL> allow_logging_basic_config = False ) <EOL> if not app . debug : <EOL> import logging <EOL> from logging . handlers import RotatingFileHandler <EOL> logfile = os . path . expanduser ( '<STR_LIT>' ) <EOL> file_handler = RotatingFileHandler ( logfile ) <EOL> file_handler . setLevel ( logging . INFO ) <EOL> app . logger . addHandler ( file_handler ) <EOL> def crossdomain ( origin = None , methods = None , headers = None , <EOL> max_age = <NUM_LIT> , attach_to_all = True , <EOL> automatic_options = True ) : <EOL> if methods is not None : <EOL> methods = '<STR_LIT:U+002CU+0020>' . join ( sorted ( x . upper ( ) for x in methods ) ) <EOL> if headers is not None and not isinstance ( headers , str ) : <EOL> headers = '<STR_LIT:U+002CU+0020>' . join ( x . upper ( ) for x in headers ) <EOL> if not isinstance ( origin , str ) : <EOL> origin = '<STR_LIT:U+002CU+0020>' . join ( origin ) <EOL> if isinstance ( max_age , timedelta ) : <EOL> max_age = max_age . total_seconds ( ) <EOL> def get_methods ( ) : <EOL> if methods is not None : <EOL> return methods <EOL> options_resp = current_app . make_default_options_response ( ) <EOL> return options_resp . headers [ '<STR_LIT>' ] <EOL> def decorator ( f ) : <EOL> def wrapped_function ( * args , ** kwargs ) : <EOL> if automatic_options and request . method == '<STR_LIT>' : <EOL> resp = current_app . make_default_options_response ( ) <EOL> else : <EOL> resp = make_response ( f ( * args , ** kwargs ) ) <EOL> if not attach_to_all and request . method != '<STR_LIT>' : <EOL> return resp <EOL> h = resp . headers <EOL> h [ '<STR_LIT>' ] = origin <EOL> h [ '<STR_LIT>' ] = get_methods ( ) <EOL> h [ '<STR_LIT>' ] = str ( max_age ) <EOL> if headers is not None : <EOL> h [ '<STR_LIT>' ] = headers <EOL> return resp <EOL> f . provide_automatic_options = False <EOL> return update_wrapper ( wrapped_function , f ) <EOL> return decorator <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' , '<STR_LIT>' ] ) <EOL> @ crossdomain ( origin = '<STR_LIT:*>' , headers = [ '<STR_LIT:Content-Type>' , '<STR_LIT>' ] ) <EOL> def division_lookup ( ) : <EOL> try : <EOL> if request . json is None and request . method == '<STR_LIT:POST>' : <EOL> abort ( <NUM_LIT> , "<STR_LIT>" ) <EOL> elif request . method == '<STR_LIT:POST>' : <EOL> args = request . json <EOL> else : <EOL> args = request . args <EOL> if '<STR_LIT>' not in args : <EOL> abort ( <NUM_LIT> , "<STR_LIT>" ) <EOL> if '<STR_LIT>' not in args : <EOL> abort ( <NUM_LIT> , "<STR_LIT>" ) <EOL> conn = psycopg2 . connect ( host = dbcreds . HOSTNAME , database = dbcreds . DATABASE , <EOL> user = dbcreds . USERNAME , password = dbcreds . PASSWORD ) <EOL> cursor = conn . cursor ( ) <EOL> cursor . execute ( QUERY_FORMAT . format ( latitude = float ( args [ '<STR_LIT>' ] ) , <EOL> longitude = float ( args [ '<STR_LIT>' ] ) ) ) <EOL> result = cursor . fetchone ( ) <EOL> if result is None : <EOL> name = None <EOL> else : <EOL> name = result [ <NUM_LIT:0> ] . lower ( ) . translate ( None , "<STR_LIT>" ) <EOL> return jsonify ( { '<STR_LIT>' : name } ) <EOL> except : <EOL> rollbar . report_exc_info ( ) <EOL> raise <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> setup_rollbar ( '<STR_LIT>' ) <EOL> app . run ( debug = True ) <EOL> else : <EOL> setup_rollbar ( '<STR_LIT>' ) </s>
