Python tornado.util.ArgReplacer() Examples

The following are 15 code examples of tornado.util.ArgReplacer(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module tornado.util , or try the search function .
Example #1
Source File: auth.py    From viewfinder with Apache License 2.0 6 votes vote down vote up
def _auth_return_future(f):
    """Similar to tornado.concurrent.return_future, but uses the auth
    module's legacy callback interface.

    Note that when using this decorator the ``callback`` parameter
    inside the function will actually be a future.
    """
    replacer = ArgReplacer(f, 'callback')

    @functools.wraps(f)
    def wrapper(*args, **kwargs):
        future = TracebackFuture()
        callback, args, kwargs = replacer.replace(future, args, kwargs)
        if callback is not None:
            future.add_done_callback(
                functools.partial(_auth_future_to_callback, callback))
        f(*args, **kwargs)
        return future
    return wrapper 
Example #2
Source File: auth.py    From viewfinder with Apache License 2.0 6 votes vote down vote up
def _auth_return_future(f):
    """Similar to tornado.concurrent.return_future, but uses the auth
    module's legacy callback interface.

    Note that when using this decorator the ``callback`` parameter
    inside the function will actually be a future.
    """
    replacer = ArgReplacer(f, 'callback')

    @functools.wraps(f)
    def wrapper(*args, **kwargs):
        future = TracebackFuture()
        callback, args, kwargs = replacer.replace(future, args, kwargs)
        if callback is not None:
            future.add_done_callback(
                functools.partial(_auth_future_to_callback, callback))
        f(*args, **kwargs)
        return future
    return wrapper 
Example #3
Source File: auth.py    From tornado-zh with MIT License 5 votes vote down vote up
def _auth_return_future(f):
    """Similar to tornado.concurrent.return_future, but uses the auth
    module's legacy callback interface.

    Note that when using this decorator the ``callback`` parameter
    inside the function will actually be a future.
    """
    replacer = ArgReplacer(f, 'callback')

    @functools.wraps(f)
    def wrapper(*args, **kwargs):
        future = TracebackFuture()
        callback, args, kwargs = replacer.replace(future, args, kwargs)
        if callback is not None:
            future.add_done_callback(
                functools.partial(_auth_future_to_callback, callback))

        def handle_exception(typ, value, tb):
            if future.done():
                return False
            else:
                future.set_exc_info((typ, value, tb))
                return True
        with ExceptionStackContext(handle_exception):
            f(*args, **kwargs)
        return future
    return wrapper 
Example #4
Source File: util_test.py    From tornado-zh with MIT License 5 votes vote down vote up
def setUp(self):
        def function(x, y, callback=None, z=None):
            pass
        self.replacer = ArgReplacer(function, 'callback') 
Example #5
Source File: auth.py    From tornado-zh with MIT License 5 votes vote down vote up
def _auth_return_future(f):
    """Similar to tornado.concurrent.return_future, but uses the auth
    module's legacy callback interface.

    Note that when using this decorator the ``callback`` parameter
    inside the function will actually be a future.
    """
    replacer = ArgReplacer(f, 'callback')

    @functools.wraps(f)
    def wrapper(*args, **kwargs):
        future = TracebackFuture()
        callback, args, kwargs = replacer.replace(future, args, kwargs)
        if callback is not None:
            future.add_done_callback(
                functools.partial(_auth_future_to_callback, callback))

        def handle_exception(typ, value, tb):
            if future.done():
                return False
            else:
                future.set_exc_info((typ, value, tb))
                return True
        with ExceptionStackContext(handle_exception):
            f(*args, **kwargs)
        return future
    return wrapper 
Example #6
Source File: util_test.py    From tornado-zh with MIT License 5 votes vote down vote up
def setUp(self):
        def function(x, y, callback=None, z=None):
            pass
        self.replacer = ArgReplacer(function, 'callback') 
Example #7
Source File: cythonapp_test.py    From tornado-zh with MIT License 5 votes vote down vote up
def test_arg_replacer_function(self):
        replacer = ArgReplacer(cythonapp.function_with_args, 'two')
        args = (1, 'old', 3)
        kwargs = {}
        self.assertEqual(replacer.get_old_value(args, kwargs), 'old')
        self.assertEqual(replacer.replace('new', args, kwargs),
                         ('old', [1, 'new', 3], {})) 
Example #8
Source File: util_test.py    From viewfinder with Apache License 2.0 5 votes vote down vote up
def setUp(self):
        def function(x, y, callback=None, z=None):
            pass
        self.replacer = ArgReplacer(function, 'callback') 
Example #9
Source File: util_test.py    From viewfinder with Apache License 2.0 5 votes vote down vote up
def setUp(self):
        def function(x, y, callback=None, z=None):
            pass
        self.replacer = ArgReplacer(function, 'callback') 
Example #10
Source File: auth.py    From teleport with Apache License 2.0 5 votes vote down vote up
def _auth_return_future(f):
    """Similar to tornado.concurrent.return_future, but uses the auth
    module's legacy callback interface.

    Note that when using this decorator the ``callback`` parameter
    inside the function will actually be a future.

    .. deprecated:: 5.1
       Will be removed in 6.0.
    """
    replacer = ArgReplacer(f, 'callback')

    @functools.wraps(f)
    def wrapper(*args, **kwargs):
        future = Future()
        callback, args, kwargs = replacer.replace(future, args, kwargs)
        if callback is not None:
            warnings.warn("callback arguments are deprecated, use the returned Future instead",
                          DeprecationWarning)
            future.add_done_callback(
                wrap(functools.partial(_auth_future_to_callback, callback)))

        def handle_exception(typ, value, tb):
            if future.done():
                return False
            else:
                future_set_exc_info(future, (typ, value, tb))
                return True
        with ExceptionStackContext(handle_exception, delay_warning=True):
            f(*args, **kwargs)
        return future
    return wrapper 
Example #11
Source File: auth.py    From pySINDy with MIT License 5 votes vote down vote up
def _auth_return_future(f):
    """Similar to tornado.concurrent.return_future, but uses the auth
    module's legacy callback interface.

    Note that when using this decorator the ``callback`` parameter
    inside the function will actually be a future.

    .. deprecated:: 5.1
       Will be removed in 6.0.
    """
    replacer = ArgReplacer(f, 'callback')

    @functools.wraps(f)
    def wrapper(*args, **kwargs):
        future = Future()
        callback, args, kwargs = replacer.replace(future, args, kwargs)
        if callback is not None:
            warnings.warn("callback arguments are deprecated, use the returned Future instead",
                          DeprecationWarning)
            future.add_done_callback(
                wrap(functools.partial(_auth_future_to_callback, callback)))

        def handle_exception(typ, value, tb):
            if future.done():
                return False
            else:
                future_set_exc_info(future, (typ, value, tb))
                return True
        with ExceptionStackContext(handle_exception, delay_warning=True):
            f(*args, **kwargs)
        return future
    return wrapper 
Example #12
Source File: auth.py    From EventGhost with GNU General Public License v2.0 5 votes vote down vote up
def _auth_return_future(f):
    """Similar to tornado.concurrent.return_future, but uses the auth
    module's legacy callback interface.

    Note that when using this decorator the ``callback`` parameter
    inside the function will actually be a future.
    """
    replacer = ArgReplacer(f, 'callback')

    @functools.wraps(f)
    def wrapper(*args, **kwargs):
        future = TracebackFuture()
        callback, args, kwargs = replacer.replace(future, args, kwargs)
        if callback is not None:
            future.add_done_callback(
                functools.partial(_auth_future_to_callback, callback))

        def handle_exception(typ, value, tb):
            if future.done():
                return False
            else:
                future.set_exc_info((typ, value, tb))
                return True
        with ExceptionStackContext(handle_exception):
            f(*args, **kwargs)
        return future
    return wrapper 
Example #13
Source File: util_test.py    From EventGhost with GNU General Public License v2.0 5 votes vote down vote up
def setUp(self):
        def function(x, y, callback=None, z=None):
            pass
        self.replacer = ArgReplacer(function, 'callback') 
Example #14
Source File: concurrent.py    From teleport with Apache License 2.0 4 votes vote down vote up
def _non_deprecated_return_future(f, warn=False):
    # Allow auth.py to use this decorator without triggering
    # deprecation warnings. This will go away once auth.py has removed
    # its legacy interfaces in 6.0.
    replacer = ArgReplacer(f, 'callback')

    @functools.wraps(f)
    def wrapper(*args, **kwargs):
        future = Future()
        callback, args, kwargs = replacer.replace(
            lambda value=_NO_RESULT: future_set_result_unless_cancelled(future, value),
            args, kwargs)

        def handle_error(typ, value, tb):
            future_set_exc_info(future, (typ, value, tb))
            return True
        exc_info = None
        esc = ExceptionStackContext(handle_error, delay_warning=True)
        with esc:
            if not warn:
                # HACK: In non-deprecated mode (only used in auth.py),
                # suppress the warning entirely. Since this is added
                # in a 5.1 patch release and already removed in 6.0
                # I'm prioritizing a minimial change instead of a
                # clean solution.
                esc.delay_warning = False
            try:
                result = f(*args, **kwargs)
                if result is not None:
                    raise ReturnValueIgnoredError(
                        "@return_future should not be used with functions "
                        "that return values")
            except:
                exc_info = sys.exc_info()
                raise
        if exc_info is not None:
            # If the initial synchronous part of f() raised an exception,
            # go ahead and raise it to the caller directly without waiting
            # for them to inspect the Future.
            future.result()

        # If the caller passed in a callback, schedule it to be called
        # when the future resolves.  It is important that this happens
        # just before we return the future, or else we risk confusing
        # stack contexts with multiple exceptions (one here with the
        # immediate exception, and again when the future resolves and
        # the callback triggers its exception by calling future.result()).
        if callback is not None:
            warnings.warn("callback arguments are deprecated, use the returned Future instead",
                          DeprecationWarning)

            def run_callback(future):
                result = future.result()
                if result is _NO_RESULT:
                    callback()
                else:
                    callback(future.result())
            future_add_done_callback(future, wrap(run_callback))
        return future
    return wrapper 
Example #15
Source File: concurrent.py    From pySINDy with MIT License 4 votes vote down vote up
def _non_deprecated_return_future(f, warn=False):
    # Allow auth.py to use this decorator without triggering
    # deprecation warnings. This will go away once auth.py has removed
    # its legacy interfaces in 6.0.
    replacer = ArgReplacer(f, 'callback')

    @functools.wraps(f)
    def wrapper(*args, **kwargs):
        future = Future()
        callback, args, kwargs = replacer.replace(
            lambda value=_NO_RESULT: future_set_result_unless_cancelled(future, value),
            args, kwargs)

        def handle_error(typ, value, tb):
            future_set_exc_info(future, (typ, value, tb))
            return True
        exc_info = None
        esc = ExceptionStackContext(handle_error, delay_warning=True)
        with esc:
            if not warn:
                # HACK: In non-deprecated mode (only used in auth.py),
                # suppress the warning entirely. Since this is added
                # in a 5.1 patch release and already removed in 6.0
                # I'm prioritizing a minimial change instead of a
                # clean solution.
                esc.delay_warning = False
            try:
                result = f(*args, **kwargs)
                if result is not None:
                    raise ReturnValueIgnoredError(
                        "@return_future should not be used with functions "
                        "that return values")
            except:
                exc_info = sys.exc_info()
                raise
        if exc_info is not None:
            # If the initial synchronous part of f() raised an exception,
            # go ahead and raise it to the caller directly without waiting
            # for them to inspect the Future.
            future.result()

        # If the caller passed in a callback, schedule it to be called
        # when the future resolves.  It is important that this happens
        # just before we return the future, or else we risk confusing
        # stack contexts with multiple exceptions (one here with the
        # immediate exception, and again when the future resolves and
        # the callback triggers its exception by calling future.result()).
        if callback is not None:
            warnings.warn("callback arguments are deprecated, use the returned Future instead",
                          DeprecationWarning)

            def run_callback(future):
                result = future.result()
                if result is _NO_RESULT:
                    callback()
                else:
                    callback(future.result())
            future_add_done_callback(future, wrap(run_callback))
        return future
    return wrapper