<s> from __future__ import print_function , unicode_literals <EOL> import StringIO <EOL> from flask import request , jsonify <EOL> from weblab . core . web import weblab_api , get_argument <EOL> from weblab . core . codes import WEBLAB_GENERAL_EXCEPTION_CODE , PYTHON_GENERAL_EXCEPTION_CODE <EOL> import weblab . experiment . util as Util <EOL> FILE_SENT = '<STR_LIT>' <EOL> FILE_INFO = '<STR_LIT>' <EOL> SESSION_ID = '<STR_LIT>' <EOL> RESERVATION_ID = '<STR_LIT>' <EOL> IS_ASYNC = '<STR_LIT>' <EOL> BASE_HTML_TEMPLATE = """<STR_LIT>""" <EOL> SUCCESS_HTML_TEMPLATE = BASE_HTML_TEMPLATE % { <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> FAULT_HTML_TEMPLATE = BASE_HTML_TEMPLATE % { <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> @ weblab_api . route_web ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> def upload ( ) : <EOL> """<STR_LIT>""" <EOL> response_format = request . args . get ( '<STR_LIT>' , '<STR_LIT:html>' ) . lower ( ) <EOL> try : <EOL> file_info , file_sent , reservation_id , is_async = _check_arguments ( ) <EOL> file_content = Util . serialize ( file_sent ) <EOL> weblab_api . ctx . reservation_id = reservation_id <EOL> if not is_async : <EOL> result = weblab_api . api . send_file ( file_content , file_info ) <EOL> else : <EOL> result = weblab_api . api . send_async_file ( file_content , file_info ) <EOL> except FileUploadError as fue : <EOL> code , message = fue . args <EOL> if response_format == '<STR_LIT>' : <EOL> return jsonify ( is_exception = True , message = message , code = code ) <EOL> else : <EOL> return FAULT_HTML_TEMPLATE % { <EOL> '<STR_LIT>' : code , <EOL> '<STR_LIT>' : message <EOL> } <EOL> except Exception as e : <EOL> message = e . args [ <NUM_LIT:0> ] <EOL> if response_format == '<STR_LIT>' : <EOL> return jsonify ( is_exception = True , message = message , code = PYTHON_GENERAL_EXCEPTION_CODE ) <EOL> else : <EOL> return FAULT_HTML_TEMPLATE % { <EOL> '<STR_LIT>' : PYTHON_GENERAL_EXCEPTION_CODE , <EOL> '<STR_LIT>' : message <EOL> } <EOL> else : <EOL> if not is_async : <EOL> resultstr = result . commandstring <EOL> else : <EOL> resultstr = result <EOL> print ( "<STR_LIT>" , resultstr ) <EOL> if response_format == '<STR_LIT>' : <EOL> return jsonify ( is_exception = False , result = { '<STR_LIT>' : resultstr } ) <EOL> else : <EOL> return SUCCESS_HTML_TEMPLATE % { <EOL> '<STR_LIT>' : resultstr <EOL> } <EOL> def _check_arguments ( ) : <EOL> """<STR_LIT>""" <EOL> file_info = get_argument ( FILE_INFO ) <EOL> if file_info is None : <EOL> raise FileUploadError ( WEBLAB_GENERAL_EXCEPTION_CODE , "<STR_LIT>" % FILE_INFO ) <EOL> file_sent = request . files . get ( FILE_SENT ) <EOL> if file_sent is None : <EOL> file_sent = get_argument ( FILE_SENT ) <EOL> if file_sent is None : <EOL> raise FileUploadError ( WEBLAB_GENERAL_EXCEPTION_CODE , "<STR_LIT>" % FILE_SENT ) <EOL> else : <EOL> sio = StringIO . StringIO ( ) <EOL> file_sent . save ( sio ) <EOL> file_sent = sio . getvalue ( ) <EOL> reservation_id = get_argument ( RESERVATION_ID ) <EOL> if reservation_id is None : <EOL> reservation_id = get_argument ( SESSION_ID ) <EOL> if reservation_id is None : <EOL> raise FileUploadError ( WEBLAB_GENERAL_EXCEPTION_CODE , "<STR_LIT>" % '<STR_LIT>' . join ( ( SESSION_ID , RESERVATION_ID ) ) ) <EOL> is_async_str = get_argument ( IS_ASYNC ) <EOL> if is_async_str is None : <EOL> is_async = False <EOL> else : <EOL> is_async_str = is_async_str . lower ( ) <EOL> if is_async_str in ( "<STR_LIT:true>" , "<STR_LIT:yes>" , "<STR_LIT:1>" ) : <EOL> is_async = True <EOL> elif is_async_str in ( "<STR_LIT:false>" , "<STR_LIT>" , "<STR_LIT:0>" ) : <EOL> is_async = False <EOL> else : <EOL> raise FileUploadError ( WEBLAB_GENERAL_EXCEPTION_CODE , "<STR_LIT>" % IS_ASYNC ) <EOL> return file_info , file_sent , reservation_id , is_async <EOL> class FileUploadError ( Exception ) : <EOL> pass </s>
