<s> """<STR_LIT>""" <EOL> from __future__ import print_function <EOL> import json <EOL> import sys <EOL> from twisted . internet . task import react <EOL> from twisted . python import usage <EOL> from twisted . web . http import CREATED <EOL> from treq import json_content , post <EOL> class SubmitFailure ( Exception ) : <EOL> def __init__ ( self , code , phrase , message ) : <EOL> super ( SubmitFailure , self ) . __init__ ( message ) <EOL> self . code = code <EOL> self . phrase = phrase <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" . format ( self . code , self . phrase , self . message ) <EOL> def submit ( server_url , result ) : <EOL> """<STR_LIT>""" <EOL> req = post ( <EOL> server_url + "<STR_LIT>" , <EOL> json . dumps ( result ) , <EOL> headers = ( { '<STR_LIT:Content-Type>' : [ '<STR_LIT:application/json>' ] } ) , <EOL> ) <EOL> def get_response_content ( response ) : <EOL> d = json_content ( response ) <EOL> d . addCallback ( lambda content : ( response , content ) ) <EOL> return d <EOL> req . addCallback ( get_response_content ) <EOL> def process_response ( response_and_content ) : <EOL> ( response , content ) = response_and_content <EOL> if response . code != CREATED : <EOL> raise SubmitFailure ( response . code , response . phrase , <EOL> content [ '<STR_LIT:message>' ] ) <EOL> else : <EOL> return content [ '<STR_LIT:id>' ] <EOL> req . addCallback ( process_response ) <EOL> return req <EOL> class Options ( usage . Options ) : <EOL> longdesc = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> optParameters = [ <EOL> [ '<STR_LIT:address>' , None , None , "<STR_LIT>" ] , <EOL> [ '<STR_LIT:port>' , None , <NUM_LIT> , "<STR_LIT>" , int ] , <EOL> ] <EOL> def postOptions ( self ) : <EOL> if self . get ( '<STR_LIT:address>' ) is None : <EOL> raise usage . UsageError ( "<STR_LIT>" ) <EOL> def main ( reactor , args ) : <EOL> try : <EOL> options = Options ( ) <EOL> options . parseOptions ( args ) <EOL> except usage . UsageError as e : <EOL> print ( e . args [ <NUM_LIT:0> ] , file = sys . stderr ) <EOL> print ( '<STR_LIT>' , file = sys . stderr ) <EOL> print ( str ( options ) , file = sys . stderr ) <EOL> raise SystemExit ( <NUM_LIT:1> ) <EOL> server_url = b"<STR_LIT>" . format ( <EOL> options [ '<STR_LIT:address>' ] , options [ '<STR_LIT:port>' ] , <EOL> ) <EOL> try : <EOL> result_input = sys . stdin . read ( ) <EOL> result = json . loads ( result_input ) <EOL> except ValueError as e : <EOL> print ( result_input , file = sys . stderr ) <EOL> print ( <EOL> "<STR_LIT>" . format ( e . message ) , <EOL> file = sys . stderr , <EOL> ) <EOL> raise SystemExit ( <NUM_LIT:1> ) <EOL> d = submit ( server_url , result ) <EOL> def succeeded ( result_id ) : <EOL> print ( "<STR_LIT>" . format ( result_id ) ) <EOL> def failed ( failure ) : <EOL> failure . trap ( SubmitFailure ) <EOL> print ( "<STR_LIT>" . format ( failure . value ) , <EOL> file = sys . stderr ) <EOL> raise SystemExit ( <NUM_LIT:1> ) <EOL> d . addCallbacks ( succeeded , failed ) <EOL> return d <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> react ( main , ( sys . argv [ <NUM_LIT:1> : ] , ) ) </s>
