Exemple #1
0
def on_engine_request(ibs, jobid, action, args, kwargs):
    """ Run whenever the engine recieves a message """
    # Start working
    if VERBOSE_JOBS:
        print('starting job=%r' % (jobid,))
    # Map actions to IBEISController calls here
    if action == 'helloworld':
        def helloworld(time_=0, *args, **kwargs):
            time.sleep(time_)
            retval = ('HELLO time_=%r ' % (time_,)) + ut.repr2((args, kwargs))
            return retval
        action_func = helloworld
    else:
        # check for ibs func
        action_func = getattr(ibs, action)
        if VERBOSE_JOBS:
            print('resolving action=%r to ibeis function=%r' % (action, action_func))
    try:
        result = action_func(*args, **kwargs)
        exec_status = 'ok'
    except Exception as ex:
        result = ut.formatex(ex, keys=['jobid'], tb=True)
        result = ut.strip_ansi(result)
        exec_status = 'exception'
    json_result = ut.to_json(result)
    engine_result = dict(
        exec_status=exec_status,
        json_result=json_result,
        jobid=jobid,
    )
    return engine_result
Exemple #2
0
def on_engine_request(ibs, jobid, action, args, kwargs):
    """ Run whenever the engine recieves a message """
    # Start working
    if VERBOSE_JOBS:
        print('starting job=%r' % (jobid, ))
    # Map actions to IBEISController calls here
    if action == 'helloworld':

        def helloworld(time_=0, *args, **kwargs):
            time.sleep(time_)
            retval = ('HELLO time_=%r ' % (time_, )) + ut.repr2((args, kwargs))
            return retval

        action_func = helloworld
    else:
        # check for ibs func
        action_func = getattr(ibs, action)
        if VERBOSE_JOBS:
            print('resolving action=%r to ibeis function=%r' %
                  (action, action_func))
    try:
        result = action_func(*args, **kwargs)
        exec_status = 'ok'
    except Exception as ex:
        result = ut.formatex(ex, keys=['jobid'], tb=True)
        result = ut.strip_ansi(result)
        exec_status = 'exception'
    json_result = ut.to_json(result)
    engine_result = dict(
        exec_status=exec_status,
        json_result=json_result,
        jobid=jobid,
    )
    return engine_result
Exemple #3
0
                def translated_call(**kwargs):
                    def html_newlines(text):
                        r = '<br />\n'
                        text = text.replace(' ', '&nbsp;')
                        text = text.replace('\r\n', r).replace('\n\r', r).replace('\r', r).replace('\n', r)
                        return text

                    __format__ = False  # Default __format__ value
                    ignore_cookie_set = False
                    try:
                        #print('Processing: %r with args: %r and kwargs: %r' % (func, args, kwargs, ))
                        # Pipe web input into Python web call
                        kwargs2 = _process_input(flask.request.args)
                        kwargs3 = _process_input(flask.request.form)
                        try:
                            # kwargs4 = _process_input(flask.request.get_json())
                            kwargs4 = ut.from_json(flask.request.data)
                        except:
                            kwargs4 = {}
                        kwargs.update(kwargs2)
                        kwargs.update(kwargs3)
                        kwargs.update(kwargs4)
                        jQuery_callback = None
                        if 'callback' in kwargs and 'jQuery' in kwargs['callback']:
                            jQuery_callback = str(kwargs.pop('callback', None))
                            kwargs.pop('_', None)

                        #print('KWARGS:  %s' % (kwargs, ))
                        #print('COOKIES: %s' % (request.cookies, ))
                        __format__ = request.cookies.get('__format__', None)
                        __format__ = kwargs.pop('__format__', __format__)
                        if __format__ is not None:
                            __format__ = str(__format__).lower()
                            ignore_cookie_set = __format__ in ['onetime', 'true']
                            __format__ = __format__ in ['true', 'enabled', 'enable']

                        resp_tup = translate_ibeis_webcall(func, **kwargs)
                        rawreturn, success, code, message = resp_tup
                    except WebException as webex:
                        ut.printex(webex)
                        rawreturn = webex.get_rawreturn(
                            DEBUG_PYTHON_STACK_TRACE_JSON_RESPONSE)
                        success = False
                        code = webex.code
                        message = webex.message
                        jQuery_callback = None
                    except Exception as ex:
                        ut.printex(ex)
                        rawreturn = ''
                        if DEBUG_PYTHON_STACK_TRACE_JSON_RESPONSE:
                            rawreturn = str(traceback.format_exc())
                        success = False
                        code = 500
                        errmsg = str(ex)
                        message = 'API error, Python Exception thrown: %s' % (errmsg)
                        if "'int' object is not iterable" in message:
                            rawreturn = (
                                'HINT: the input for this call is most likely '
                                'expected to be a list.  Try adding a comma at '
                                'the end of the input (to cast the conversion '
                                'into a list) or encapsualte the input with '
                                '[].')
                        jQuery_callback = None

                    #print('RECEIVED FORMAT: %r' % (__format__, ))

                    if __format__:
                        # Hack for readable error messages
                        webreturn = translate_ibeis_webreturn(
                            rawreturn, success, code, message, jQuery_callback)
                        webreturn = ut.repr3(ut.from_json(webreturn), strvals=True)

                        try:
                            from ansi2html import Ansi2HTMLConverter
                            conv = Ansi2HTMLConverter()
                            webreturn = conv.convert(webreturn)
                        except ImportError as ex:
                            ut.printex(ex, 'pip install ansi2html', iswarning=True)
                            webreturn = ut.strip_ansi(webreturn)
                            webreturn = '<p><samp>\n' + html_newlines(webreturn) + '\n</samp></p>'
                            webreturn = '<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-8">\n' + webreturn

                        def get_func_href(funcname):
                            url = 'http://' + request.environ['HTTP_HOST'] + flask.url_for(funcname) + '?__format__=True'
                            return '<a href="{url}">{url}</a>'.format(url=url)

                        if not success:
                            webreturn += '<pre>See logs for details: %s</pre>' % get_func_href('get_current_log_text')
                            webreturn += '<pre>Might also look into db_info: %s</pre>' % get_func_href('get_dbinfo')
                    else:
                        webreturn = translate_ibeis_webreturn(
                            rawreturn, success, code, message, jQuery_callback)
                        webreturn = ut.strip_ansi(webreturn)

                    resp = flask.make_response(webreturn, code)
                    if not ignore_cookie_set:
                        if __format__:
                            resp.set_cookie('__format__', 'enabled')
                        else:
                            resp.set_cookie('__format__', '', expires=0)

                    return resp
Exemple #4
0
                def translated_call(**kwargs):
                    def html_newlines(text):
                        r = '<br />\n'
                        text = text.replace(' ', '&nbsp;')
                        text = text.replace('\r\n', r).replace('\n\r', r).replace('\r', r).replace('\n', r)
                        return text

                    __format__ = False  # Default __format__ value
                    ignore_cookie_set = False
                    try:
                        #print('Processing: %r with args: %r and kwargs: %r' % (func, args, kwargs, ))
                        # Pipe web input into Python web call
                        kwargs2 = _process_input(flask.request.args)
                        kwargs3 = _process_input(flask.request.form)
                        kwargs.update(kwargs2)
                        kwargs.update(kwargs3)
                        jQuery_callback = None
                        if 'callback' in kwargs and 'jQuery' in kwargs['callback']:
                            jQuery_callback = str(kwargs.pop('callback', None))
                            kwargs.pop('_', None)

                        #print('KWARGS:  %s' % (kwargs, ))
                        #print('COOKIES: %s' % (request.cookies, ))
                        __format__ = request.cookies.get('__format__', None)
                        __format__ = kwargs.pop('__format__', __format__)
                        if __format__ is not None:
                            __format__ = str(__format__).lower()
                            ignore_cookie_set = __format__ in ['onetime', 'true']
                            __format__ = __format__ in ['true', 'enabled', 'enable']

                        resp_tup = translate_ibeis_webcall(func, **kwargs)
                        rawreturn, success, code, message = resp_tup
                    except WebException as webex:
                        ut.printex(webex)
                        rawreturn = webex.get_rawreturn(
                            DEBUG_PYTHON_STACK_TRACE_JSON_RESPONSE)
                        success = False
                        code = webex.code
                        message = webex.message
                        jQuery_callback = None
                    except Exception as ex:
                        ut.printex(ex)
                        rawreturn = ''
                        if DEBUG_PYTHON_STACK_TRACE_JSON_RESPONSE:
                            rawreturn = str(traceback.format_exc())
                        success = False
                        code = 500
                        errmsg = str(ex)
                        message = 'API error, Python Exception thrown: %s' % (errmsg)
                        if "'int' object is not iterable" in message:
                            rawreturn = (
                                'HINT: the input for this call is most likely '
                                'expected to be a list.  Try adding a comma at '
                                'the end of the input (to cast the conversion '
                                'into a list) or encapsualte the input with '
                                '[].')
                        jQuery_callback = None

                    #print('RECEIVED FORMAT: %r' % (__format__, ))

                    if __format__:
                        # Hack for readable error messages
                        webreturn = translate_ibeis_webreturn(
                            rawreturn, success, code, message, jQuery_callback)
                        webreturn = ut.repr3(ut.from_json(webreturn), strvals=True)

                        try:
                            from ansi2html import Ansi2HTMLConverter
                            conv = Ansi2HTMLConverter()
                            webreturn = conv.convert(webreturn)
                        except ImportError as ex:
                            ut.printex(ex, 'pip install ansi2html', iswarning=True)
                            webreturn = ut.strip_ansi(webreturn)
                            webreturn = '<p><samp>\n' + html_newlines(webreturn) + '\n</samp></p>'
                            webreturn = '<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-8">\n' + webreturn

                        def get_func_href(funcname):
                            url = 'http://' + request.environ['HTTP_HOST'] + flask.url_for(funcname) + '?__format__=True'
                            return '<a href="{url}">{url}</a>'.format(url=url)

                        if not success:
                            webreturn += '<pre>See logs for details: %s</pre>' % get_func_href('get_current_log_text')
                            webreturn += '<pre>Might also look into db_info: %s</pre>' % get_func_href('get_dbinfo')
                    else:
                        webreturn = translate_ibeis_webreturn(
                            rawreturn, success, code, message, jQuery_callback)
                        webreturn = ut.strip_ansi(webreturn)

                    resp = flask.make_response(webreturn, code)
                    if not ignore_cookie_set:
                        if __format__:
                            resp.set_cookie('__format__', 'enabled')
                        else:
                            resp.set_cookie('__format__', '', expires=0)

                    return resp
Exemple #5
0
                def translated_call(**kwargs):
                    def html_newlines(text):
                        r = '<br />\n'
                        text = text.replace(' ', '&nbsp;')
                        text = (
                            text.replace('\r\n', r)
                            .replace('\n\r', r)
                            .replace('\r', r)
                            .replace('\n', r)
                        )
                        return text

                    __format__ = False  # Default __format__ value
                    ignore_cookie_set = False
                    try:
                        # logger.info('Processing: %r with args: %r and kwargs: %r' % (func, args, kwargs, ))
                        # Pipe web input into Python web call
                        kwargs2 = _process_input(flask.request.args)
                        kwargs3 = _process_input(flask.request.form)
                        try:
                            # kwargs4 = _process_input(flask.request.get_json())
                            kwargs4 = ut.from_json(flask.request.data)
                        except Exception:
                            kwargs4 = {}
                        kwargs.update(kwargs2)
                        kwargs.update(kwargs3)
                        kwargs.update(kwargs4)

                        # Update the request object to include the final rectified inputs for possible future reference
                        flask.request.processed = ut.to_json(kwargs)

                        jQuery_callback = None
                        if 'callback' in kwargs and 'jQuery' in kwargs['callback']:
                            jQuery_callback = str(kwargs.pop('callback', None))
                            kwargs.pop('_', None)

                        # logger.info('KWARGS:  %s' % (kwargs, ))
                        # logger.info('COOKIES: %s' % (request.cookies, ))
                        __format__ = request.cookies.get('__format__', None)
                        __format__ = kwargs.pop('__format__', __format__)
                        if __format__ is not None:
                            __format__ = str(__format__).lower()
                            ignore_cookie_set = __format__ in ['onetime', 'true']
                            __format__ = __format__ in ['true', 'enabled', 'enable']

                        from wbia.web.app import PROMETHEUS

                        if PROMETHEUS:
                            exclude_tag_list = [
                                '/api/test/heartbeat/',
                                '/v0.1/wildbook/status/',
                                '/v0.1/vulcan/status/',
                            ]
                            tag = request.url_rule.rule
                            if tag not in exclude_tag_list:
                                ibs = flask.current_app.ibs
                                ibs.prometheus_increment_api(tag)

                        resp_tup = translate_wbia_webcall(func, **kwargs)
                        rawreturn, success, code, message = resp_tup
                    except WebException as webex:
                        # ut.printex(webex)
                        logger.info('CAUGHT2: %r' % (webex,))
                        rawreturn = webex.get_rawreturn(
                            DEBUG_PYTHON_STACK_TRACE_JSON_RESPONSE
                        )
                        success = False
                        code = webex.code
                        message = webex.message
                        jQuery_callback = None
                    except Exception as ex:
                        logger.info('CAUGHT2: %r' % (ex,))
                        # ut.printex(ex)
                        rawreturn = None
                        if DEBUG_PYTHON_STACK_TRACE_JSON_RESPONSE:
                            rawreturn = str(traceback.format_exc())
                        success = False
                        code = 500
                        message = str(ex)
                        # errmsg = str(ex)
                        # message = 'API error, Python Exception thrown: %s' % (errmsg)
                        if "'int' object is not iterable" in message:
                            rawreturn = """
                            HINT: the input for this call is most likely expected to be a list.
                            Try adding a comma at the end of the input (to cast the conversion into a list) or encapsulate the input with [].
                            """
                        jQuery_callback = None

                    # logger.info('RECEIVED FORMAT: %r' % (__format__, ))

                    if __format__:
                        # Hack for readable error messages
                        webreturn = translate_wbia_webreturn(
                            rawreturn, success, code, message, jQuery_callback
                        )
                        webreturn = ut.repr3(ut.from_json(webreturn), strvals=True)

                        try:
                            from ansi2html import Ansi2HTMLConverter

                            conv = Ansi2HTMLConverter()
                            webreturn = conv.convert(webreturn)
                        except ImportError as ex:
                            ut.printex(ex, 'pip install ansi2html', iswarning=True)
                            webreturn = ut.strip_ansi(webreturn)
                            webreturn = (
                                '<p><samp>\n' + html_newlines(webreturn) + '\n</samp></p>'
                            )
                            webreturn = (
                                '<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-8">\n'
                                + webreturn
                            )

                        def get_func_href(funcname):
                            url = (
                                'http://'
                                + request.environ['HTTP_HOST']
                                + flask.url_for(funcname)
                                + '?__format__=True'
                            )
                            return '<a href="{url}">{url}</a>'.format(url=url)

                        if not success:
                            webreturn += (
                                '<pre>See logs for details: %s</pre>'
                                % get_func_href('get_current_log_text')
                            )
                            webreturn += (
                                '<pre>Might also look into db_info: %s</pre>'
                                % get_func_href('get_dbinfo')
                            )
                    else:
                        webreturn = translate_wbia_webreturn(
                            rawreturn, success, code, message, jQuery_callback
                        )
                        webreturn = ut.strip_ansi(webreturn)

                    resp = flask.make_response(webreturn, code)
                    resp.status_code = code

                    if not __format__:
                        resp.headers['Content-Type'] = 'application/json; charset=utf-8'
                        resp.headers['mimetype'] = 'application/json'

                    if not ignore_cookie_set:
                        if __format__:
                            resp.set_cookie('__format__', 'enabled')
                        else:
                            resp.set_cookie('__format__', '', expires=0)

                    return resp