<s> """<STR_LIT>""" <EOL> import re <EOL> import urllib <EOL> import datetime <EOL> import simplejson <EOL> import psycopg2 <EOL> import psycopg2 . extras <EOL> from flask import request , make_response <EOL> def parse_rfc1738_args ( name ) : <EOL> """<STR_LIT>""" <EOL> if name is None : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> pattern = re . compile ( r'''<STR_LIT>''' , re . X ) <EOL> match = pattern . match ( name ) <EOL> if match is None : <EOL> raise RuntimeError ( "<STR_LIT>" % name ) <EOL> components = match . groupdict ( ) <EOL> if components [ '<STR_LIT:name>' ] != '<STR_LIT>' : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> if components [ '<STR_LIT:password>' ] is not None : <EOL> components [ '<STR_LIT:password>' ] = urllib . unquote_plus ( components [ '<STR_LIT:password>' ] ) <EOL> del components [ '<STR_LIT:name>' ] <EOL> return components <EOL> def _convert_http_value ( value ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return simplejson . loads ( value ) <EOL> except simplejson . JSONDecodeError : <EOL> return value <EOL> def _get_procedure_arguments ( fields , values ) : <EOL> """<STR_LIT>""" <EOL> if fields is None : <EOL> return [ ] <EOL> if values : <EOL> return [ values . get ( field ) for field in fields ] <EOL> return [ _convert_http_value ( request . values . get ( field ) ) <EOL> for field in fields if request . values . get ( field ) is not None ] <EOL> def dthandler ( obj ) : <EOL> if isinstance ( obj , datetime . datetime ) : <EOL> return obj . isoformat ( ) <EOL> class MoreSQL ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , app ) : <EOL> self . app = app <EOL> creds = parse_rfc1738_args ( app . config . get ( '<STR_LIT>' ) ) <EOL> self . connection = psycopg2 . connect ( user = creds [ '<STR_LIT:username>' ] , <EOL> password = creds [ '<STR_LIT:password>' ] , <EOL> dbname = creds [ '<STR_LIT>' ] , <EOL> host = creds [ '<STR_LIT:host>' ] , <EOL> port = creds [ '<STR_LIT:port>' ] ) <EOL> self . cursor = self . connection . cursor ( <EOL> cursor_factory = psycopg2 . extras . RealDictCursor ) <EOL> def execute ( self , procname , fields = None , values = None ) : <EOL> """<STR_LIT>""" <EOL> procargs = _get_procedure_arguments ( fields , values ) <EOL> result = self . cursor . callproc ( procname , procargs ) <EOL> self . connection . commit ( ) <EOL> if procargs == result : <EOL> result = self . cursor . fetchall ( ) <EOL> if len ( result ) == <NUM_LIT:1> : <EOL> if len ( result [ <NUM_LIT:0> ] ) == <NUM_LIT:1> : <EOL> result = result [ <NUM_LIT:0> ] . values ( ) [ <NUM_LIT:0> ] <EOL> else : <EOL> result = result [ <NUM_LIT:0> ] <EOL> return make_response ( simplejson . dumps ( result , default = dthandler ) ) </s>
