<s> import sys <EOL> import json <EOL> import psycopg2 <EOL> import psycopg2 . extras <EOL> import re <EOL> import datetime <EOL> import os <EOL> pg_connect_str = "<STR_LIT>" <EOL> pg_conn = None <EOL> debug_incoming_data = False <EOL> debug_path = '<STR_LIT>' <EOL> def respond ( result = None , error = None , httperrorcode = None ) : <EOL> global pg_conn <EOL> if result is not None : <EOL> data = """<STR_LIT>""" + result <EOL> else : <EOL> if httperrorcode is None : <EOL> httperrorcode = <NUM_LIT> <EOL> if error is None : <EOL> error = '<STR_LIT>' <EOL> data = """<STR_LIT>""" % ( httperrorcode , error ) <EOL> print data <EOL> debug_request ( '<STR_LIT>' , data ) <EOL> if pg_conn is not None : <EOL> pg_conn . close ( ) <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> def error_log ( s ) : <EOL> sys . stderr . write ( "<STR_LIT>" % ( s ) ) <EOL> def debug_request ( extension , data ) : <EOL> global debug_incoming_data <EOL> global debug_path <EOL> if debug_incoming_data == True : <EOL> td = datetime . datetime . today ( ) <EOL> path = debug_path + '<STR_LIT:/>' + td . strftime ( '<STR_LIT>' ) <EOL> filename = path + '<STR_LIT:/>' + td . isoformat ( ) <EOL> if not os . path . exists ( path ) : <EOL> os . makedirs ( path ) <EOL> fh = open ( filename + '<STR_LIT:.>' + extension , '<STR_LIT:w>' ) <EOL> fh . write ( data ) <EOL> try : <EOL> pg_conn = psycopg2 . connect ( pg_connect_str ) <EOL> except psycopg2 . Error as e : <EOL> error_log ( "<STR_LIT>" % ( e . pgerror , e . pgcode ) ) <EOL> respond ( error = '<STR_LIT>' , httperrorcode = <NUM_LIT> ) <EOL> try : <EOL> indata = sys . stdin . read ( ) . strip ( ) <EOL> except Exception as e : <EOL> respond ( error = '<STR_LIT>' , httperrorcode = <NUM_LIT> ) <EOL> if indata is None or len ( indata ) == <NUM_LIT:0> : <EOL> respond ( error = '<STR_LIT>' , httperrorcode = <NUM_LIT> ) <EOL> debug_request ( '<STR_LIT>' , indata ) <EOL> cur = pg_conn . cursor ( cursor_factory = psycopg2 . extras . DictCursor ) <EOL> sqldata = None <EOL> try : <EOL> cur . execute ( '<STR_LIT>' , <EOL> [ indata ] ) <EOL> sqldata = cur . fetchone ( ) <EOL> pg_conn . commit ( ) <EOL> respond ( result = sqldata [ '<STR_LIT>' ] ) <EOL> except psycopg2 . InternalError as e : <EOL> pg_conn . rollback ( ) <EOL> error_log ( "<STR_LIT>" % ( e . pgerror , e . pgcode ) ) <EOL> mg = re . match ( '<STR_LIT>' , e . pgerror ) <EOL> if mg is not None : <EOL> respond ( error = mg . group ( <NUM_LIT:1> ) , httperrorcode = <NUM_LIT> ) <EOL> else : <EOL> respond ( error = '<STR_LIT>' , httperrorcode = <NUM_LIT> ) <EOL> except psycopg2 . ProgrammingError as e : <EOL> error_log ( "<STR_LIT>" % ( e . pgerror , e . pgcode ) ) <EOL> pg_conn . rollback ( ) <EOL> respond ( error = '<STR_LIT>' , httperrorcode = <NUM_LIT> ) <EOL> sys . exit ( <NUM_LIT:0> ) </s>
