示例#1
0
def call_method_create_response(tomo_num,
                                method_name,
                                args=(),
                                GET_FRAME_method=False):
    if type(args) not in (tuple, list):
        args = (args, )

    tomograph = TOMOGRAPHS[tomo_num - 1]
    # tomo_num - 1, because in TOMOGRAPHS list numeration begins from 0

    try:
        result = getattr(tomograph, method_name)(*args)
    except ModExpError as e:
        e.log(exp_id='')
        return e.create_response()
    """
    except Exception as e:
        logger.info(e.message)
        try:
            return create_response(success=False, error="unexpected exception", exception_message=e.message)
        except Exception as e2:
            logger.info(e.message)
            return create_response(success=False, error="unexpected exception")
    """
    if not GET_FRAME_method:
        return create_response(success=True, result=result)
    else:
        success, ModExpError_if_fail = prepare_send_frame(
            raw_image_with_metadata=result, experiment=None)
        if not success:
            return ModExpError_if_fail.create_response()

        return send_file('../' + FRAME_PNG_FILENAME, mimetype='image/png')
示例#2
0
文件: views.py 项目: meerstein/rbtm
def call_method_create_response(tomo_num, method_name, args=(), GET_FRAME_method=False):
    if type(args) not in (tuple, list):
        args = (args,)

    tomograph = TOMOGRAPHS[tomo_num - 1]
    # tomo_num - 1, because in TOMOGRAPHS list numeration begins from 0

    try:
        result = getattr(tomograph, method_name)(*args)
    except ModExpError as e:
        e.log(exp_id='')
        return e.create_response()
    """
    except Exception as e:
        logger.info(e.message)
        try:
            return create_response(success=False, error="unexpected exception", exception_message=e.message)
        except Exception as e2:
            logger.info(e.message)
            return create_response(success=False, error="unexpected exception")
    """
    if not GET_FRAME_method:
        return create_response(success=True, result=result)
    else:
        success, ModExpError_if_fail = prepare_send_frame(raw_image_with_metadata=result, experiment=None)
        if not success:
            return ModExpError_if_fail.create_response()

        return send_file('../' + FRAME_PNG_FILENAME, mimetype='image/png')
示例#3
0
def check_state(tomo_num):
    logger.info('\n\nREQUEST: CHECK STATE')
    tomograph = TOMOGRAPHS[tomo_num - 1]
    # tomo_num - 1, because in TOMOGRAPHS list numeration begins from 0
    tomo_state, exception_message = tomograph.tomo_state()
    return create_response(success=True,
                           result=tomo_state,
                           exception_message=exception_message)
示例#4
0
文件: views.py 项目: meerstein/rbtm
def check_request(request_data):
    """ Checks body part of request, if it is not empty and has JSON string try to load it to python object

    :arg: 'request_data' - body part of request, type is undetermined in common case
    :return: 
    """
    logger.info('Checking request...')
    if not request_data:
        logger.info('Request is empty!')
        return False, None, create_response(success=False, error='Request is empty')

    logger.info('Request is NOT empty! Checking request\'s JSON...')
    try:
        request_data_dict = json.loads(request_data)
    except TypeError:
        logger.info('Request has NOT JSON data!')
        return False, None, create_response(success=False, error='Request has not JSON data')
    else:
        logger.info('Request has JSON data!')
        return True, request_data_dict, ''
示例#5
0
def check_request(request_data):
    """ Checks body part of request, if it is not empty and has JSON string try to load it to python object

    :arg: 'request_data' - body part of request, type is undetermined in common case
    :return: 
    """
    logger.info('Checking request...')
    if not request_data:
        logger.info('Request is empty!')
        return False, None, create_response(success=False,
                                            error='Request is empty')

    logger.info('Request is NOT empty! Checking request\'s JSON...')
    try:
        request_data_dict = json.loads(request_data)
    except TypeError:
        logger.info('Request has NOT JSON data!')
        return False, None, create_response(success=False,
                                            error='Request has not JSON data')
    else:
        logger.info('Request has JSON data!')
        return True, request_data_dict, ''
示例#6
0
文件: views.py 项目: meerstein/rbtm
def experiment_start(tomo_num):
    logger.info('\n\nREQUEST: EXPERIMENT/START')
    tomograph = TOMOGRAPHS[tomo_num - 1]
    # tomo_num - 1, because in TOMOGRAPHS list numeration begins from 0

    success, data, response_if_fail = check_request(request.data)
    if not success:
        return response_if_fail

    logger.info('Checking generous format...')
    if not (('experiment parameters' in data.keys()) and ('exp_id' in data.keys())):
        logger.info('Incorrect format of keywords!')
        return create_response(success=False, error='Incorrect format of keywords')

    if not ((type(data['experiment parameters']) is dict) and (type(data['exp_id']) is unicode)):
        logger.info('Incorrect format of types!')
        return create_response(success=False, error='Incorrect format: incorrect types')
    logger.info('Generous format is normal!')

    exp_param = data['experiment parameters']
    exp_param['exp_id'] = data['exp_id']

    logger.info('Checking parameters...')
    success, error = check_and_prepare_exp_parameters(exp_param)
    if not success:
        logger.info(error)
        return create_response(success=success, error=error)
    logger.info('Parameters are normal!')

    tomo_state, exception_message = tomograph.tomo_state()
    if tomo_state == 'unavailable':
        return create_response(success=False, error="Could not connect with tomograph",
                               exception_message=exception_message)
    elif tomo_state == 'experiment':
        return create_response(success=False, error="On this tomograph experiment is running")
    elif tomo_state != 'ready':
        return create_response(success=False, error="Undefined tomograph state")

    logger.info('Sending to storage leading to prepare...')
    try:
        send_to_storage(STORAGE_EXP_START_URI, data=request.data)
    except ModExpError as e:
        e.log(exp_id=exp_param['exp_id'])
        return e.create_response()

    logger.info('Experiment begins!')
    if exp_param['advanced']:
        pass
        # thr = threading.Thread(target=carry_out_advanced_experiment, args=(tomograph, exp_param))
    else:
        thr = threading.Thread(target=tomograph.carry_out_simple_experiment, args=(exp_param,))
        thr.start()

    return create_response(True)
示例#7
0
文件: views.py 项目: meerstein/rbtm
def experiment_stop(tomo_num):
    logger.info('\n\nREQUEST: EXPERIMENT/STOP')
    tomograph = TOMOGRAPHS[tomo_num - 1]
    # tomo_num - 1, because in TOMOGRAPHS list numeration begins from 0

    # success, exp_stop_reason_txt, response_if_fail = check_request(request.data)
    # if not success:
    #    return response_if_fail

    # if not exp_stop_reason_txt:
    #    exp_stop_reason_txt = "unknown"
    exp_stop_reason_txt = "unknown"

    if tomograph.current_experiment is not None:
        tomograph.current_experiment.to_be_stopped = True
        tomograph.current_experiment.stop_exception = ModExpError(error=exp_stop_reason_txt, stop_msg=SOMEONE_STOP_MSG)

    return create_response(True)
示例#8
0
def experiment_stop(tomo_num):
    logger.info('\n\nREQUEST: EXPERIMENT/STOP')
    tomograph = TOMOGRAPHS[tomo_num - 1]
    # tomo_num - 1, because in TOMOGRAPHS list numeration begins from 0

    # success, exp_stop_reason_txt, response_if_fail = check_request(request.data)
    # if not success:
    #    return response_if_fail

    # if not exp_stop_reason_txt:
    #    exp_stop_reason_txt = "unknown"
    exp_stop_reason_txt = "unknown"

    if tomograph.current_experiment is not None:
        tomograph.current_experiment.to_be_stopped = True
        tomograph.current_experiment.stop_exception = ModExpError(
            error=exp_stop_reason_txt, stop_msg=SOMEONE_STOP_MSG)

    return create_response(True)
示例#9
0
def internal_server_error(exception):
    logger.exception(exception)
    return make_response(
        create_response(success=False, error='Internal Server Error'), 500)
示例#10
0
def not_found(exception):
    logger.exception(exception)
    return make_response(create_response(success=False, error='Not Found'),
                         404)
示例#11
0
def incorrect_format(exception):
    logger.exception(exception)
    return make_response(
        create_response(success=False, error='Incorrect Format'), 400)
示例#12
0
def experiment_start(tomo_num):
    logger.info('\n\nREQUEST: EXPERIMENT/START')
    tomograph = TOMOGRAPHS[tomo_num - 1]
    # tomo_num - 1, because in TOMOGRAPHS list numeration begins from 0

    success, data, response_if_fail = check_request(request.data)
    if not success:
        return response_if_fail

    logger.info('Checking generous format...')
    if not (('experiment parameters' in data.keys()) and
            ('exp_id' in data.keys())):
        logger.info('Incorrect format of keywords!')
        return create_response(success=False,
                               error='Incorrect format of keywords')

    if not ((type(data['experiment parameters']) is dict) and
            (type(data['exp_id']) is unicode)):
        logger.info('Incorrect format of types!')
        return create_response(success=False,
                               error='Incorrect format: incorrect types')
    logger.info('Generous format is normal!')

    exp_param = data['experiment parameters']
    exp_param['exp_id'] = data['exp_id']

    logger.info('Checking parameters...')
    success, error = check_and_prepare_exp_parameters(exp_param)
    if not success:
        logger.info(error)
        return create_response(success=success, error=error)
    logger.info('Parameters are normal!')

    tomo_state, exception_message = tomograph.tomo_state()
    if tomo_state == 'unavailable':
        return create_response(success=False,
                               error="Could not connect with tomograph",
                               exception_message=exception_message)
    elif tomo_state == 'experiment':
        return create_response(success=False,
                               error="On this tomograph experiment is running")
    elif tomo_state != 'ready':
        return create_response(success=False,
                               error="Undefined tomograph state")

    logger.info('Sending to storage leading to prepare...')
    try:
        send_to_storage(STORAGE_EXP_START_URI, data=request.data)
    except ModExpError as e:
        e.log(exp_id=exp_param['exp_id'])
        return e.create_response()

    logger.info('Experiment begins!')
    if exp_param['advanced']:
        pass
        # thr = threading.Thread(target=carry_out_advanced_experiment, args=(tomograph, exp_param))
    else:
        thr = threading.Thread(target=tomograph.carry_out_simple_experiment,
                               args=(exp_param, ))
        thr.start()

    return create_response(True)
示例#13
0
文件: views.py 项目: meerstein/rbtm
def internal_server_error(exception):
    logger.exception(exception)
    return make_response(create_response(success=False, error='Internal Server Error'), 500)
示例#14
0
文件: views.py 项目: meerstein/rbtm
def not_found(exception):
    logger.exception(exception)
    return make_response(create_response(success=False, error='Not Found'), 404)
示例#15
0
文件: views.py 项目: meerstein/rbtm
def incorrect_format(exception):
    logger.exception(exception)
    return make_response(create_response(success=False, error='Incorrect Format'), 400)
示例#16
0
文件: views.py 项目: meerstein/rbtm
def check_state(tomo_num):
    logger.info('\n\nREQUEST: CHECK STATE')
    tomograph = TOMOGRAPHS[tomo_num - 1]
    # tomo_num - 1, because in TOMOGRAPHS list numeration begins from 0
    tomo_state, exception_message = tomograph.tomo_state()
    return create_response(success=True, result=tomo_state, exception_message=exception_message)