<s> import json <EOL> import re <EOL> from urllib . parse import unquote_plus <EOL> from http . cookies import SimpleCookie <EOL> from datetime import datetime , timedelta <EOL> from traceback import format_exc <EOL> from framework . status_codes import status_codes <EOL> from framework . database import make_db_connection , close_db_connection <EOL> from framework . routes import find_path , abort <EOL> import framework . database <EOL> import framework . mail <EOL> from modules . util import json_serial <EOL> config = { <EOL> '<STR_LIT>' : False <EOL> } <EOL> def update_config ( conf_ ) : <EOL> """<STR_LIT>""" <EOL> config . update ( conf_ ) <EOL> framework . database . config . update ( conf_ ) <EOL> framework . mail . config . update ( conf_ ) <EOL> def serve ( environ , start_response ) : <EOL> """<STR_LIT>""" <EOL> db_conn = make_db_connection ( ) <EOL> request = construct_request ( environ , db_conn ) <EOL> code , data = call_handler ( request ) <EOL> close_db_connection ( db_conn ) <EOL> response_headers = [ ( '<STR_LIT:Content-Type>' , '<STR_LIT>' ) ] <EOL> if isinstance ( data , dict ) : <EOL> response_headers += set_cookie_headers ( data . pop ( '<STR_LIT>' , { } ) ) <EOL> status = str ( code ) + '<STR_LIT:U+0020>' + status_codes . get ( code , '<STR_LIT>' ) <EOL> start_response ( status , response_headers ) <EOL> if isinstance ( data , str ) : <EOL> body = data . encode ( ) <EOL> elif isinstance ( data , dict ) : <EOL> body = json . dumps ( data , default = json_serial , ensure_ascii = False ) <EOL> body = body . encode ( ) <EOL> return [ body ] <EOL> def construct_request ( environ , db_conn ) : <EOL> """<STR_LIT>""" <EOL> request = { } <EOL> request [ '<STR_LIT>' ] = environ [ '<STR_LIT>' ] <EOL> request [ '<STR_LIT:path>' ] = environ [ '<STR_LIT>' ] + environ [ '<STR_LIT>' ] <EOL> request [ '<STR_LIT>' ] = db_conn <EOL> request [ '<STR_LIT>' ] = pull_cookies ( environ ) <EOL> if request [ '<STR_LIT>' ] == '<STR_LIT:GET>' : <EOL> request [ '<STR_LIT>' ] = pull_query_string ( environ ) <EOL> elif request [ '<STR_LIT>' ] in ( '<STR_LIT>' , '<STR_LIT:POST>' ) : <EOL> request [ '<STR_LIT>' ] = pull_body ( environ ) <EOL> return request <EOL> def call_handler ( request ) : <EOL> """<STR_LIT>""" <EOL> method = request [ '<STR_LIT>' ] <EOL> if method not in ( '<STR_LIT:GET>' , '<STR_LIT:POST>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return abort ( <NUM_LIT> ) <EOL> path = request [ '<STR_LIT:path>' ] <EOL> handler , parameters = find_path ( method , path ) <EOL> if not handler : <EOL> return abort ( <NUM_LIT> ) <EOL> try : <EOL> return handler ( request = request , ** parameters ) <EOL> except Exception : <EOL> if config [ '<STR_LIT>' ] : <EOL> return <NUM_LIT> , format_exc ( ) <EOL> return abort ( <NUM_LIT> ) <EOL> def pull_query_string ( environ ) : <EOL> """<STR_LIT>""" <EOL> args = unquote_plus ( environ . get ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> if not args : <EOL> return { } <EOL> def _ ( pair ) : <EOL> pair = pair . split ( '<STR_LIT:=>' ) <EOL> if len ( pair ) == <NUM_LIT:2> : <EOL> return pair <EOL> return pair [ <NUM_LIT:0> ] , '<STR_LIT>' <EOL> args = dict ( map ( _ , args . split ( '<STR_LIT:&>' ) ) ) <EOL> return { key : valuefy ( value ) for key , value in args . items ( ) } <EOL> def valuefy ( value ) : <EOL> """<STR_LIT>""" <EOL> if value == '<STR_LIT:true>' : <EOL> return True <EOL> if value == '<STR_LIT:false>' : <EOL> return False <EOL> if value == '<STR_LIT:null>' : <EOL> return None <EOL> if re . match ( r'<STR_LIT>' , value ) : <EOL> return int ( value ) <EOL> if re . match ( r'<STR_LIT>' , value ) : <EOL> return float ( value ) <EOL> return value <EOL> def pull_body ( environ ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> content_length = int ( environ . get ( '<STR_LIT>' , <NUM_LIT:0> ) ) <EOL> body = environ [ '<STR_LIT>' ] . read ( content_length ) <EOL> body = body . decode ( ) <EOL> return json . loads ( body , strict = False ) <EOL> except : <EOL> return { } <EOL> def pull_cookies ( environ ) : <EOL> """<STR_LIT>""" <EOL> cookie = SimpleCookie ( environ . get ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> return { key : morsel . value for key , morsel in cookie . items ( ) } <EOL> def set_cookie_headers ( cookies ) : <EOL> """<STR_LIT>""" <EOL> expires = ( ( datetime . utcnow ( ) + timedelta ( weeks = <NUM_LIT:2> ) ) <EOL> . strftime ( '<STR_LIT>' ) ) <EOL> return [ <EOL> ( '<STR_LIT>' , '<STR_LIT>' . join ( [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] ) . format ( key = key , value = value , expires = expires ) ) <EOL> for key , value in cookies . items ( ) <EOL> ] </s>
