def decorator(func): robj = route.Route(path, method, func, options) hooks.call_hooks('route', 'setup', robj) self.routes.append(robj) @functools.wraps(func) def closure(*args, **kwargs): req = request.Request(robj, args, kwargs) ctx = {'request': req} hooks.call_hooks('route', 'context', ctx) with context.Context(**ctx): hooks.call_hooks('route', 'enter', req) try: self._do_auth(req) req.body, req.query = self._load_args(req) self.logger.info("Calling: %s:%s(%r, %r)", robj.mname, robj.fname, args, kwargs) req.ret = robj.func(*req.args, **req.kwargs) except Exception as eobj: req.exception = eobj hooks.call_hooks('route', 'exception', req) if not req.exception_handled: exhandler.run_handler(req) finally: hooks.call_hooks('route', 'exit', req) return req.formatter() closure.route = robj return closure
def test_call_hooks(self): called = {'count': 0, 'arg': None} @hooks.hook('foo', 'bar') def test(a): called['count'] += 1 called['arg'] = a hooks.call_hooks('foo', 'bar', 'zuz') self.assertEqual(called['count'], 1) self.assertEqual(called['arg'], 'zuz')
def _default_formatter(req): hooks.call_hooks('route', 'formatter', req) if not isinstance(req.ret, (list, tuple)): req.ret = (req.ret,) if req.total is None: req.total = len(req.ret) return {'data': req.ret, 'total': req.total}
def closure(*args, **kwargs): req = request.Request(robj, args, kwargs) ctx = {'request': req} hooks.call_hooks('route', 'context', ctx) with context.Context(**ctx): hooks.call_hooks('route', 'enter', req) try: self._do_auth(req) req.body, req.query = self._load_args(req) self.logger.info("Calling: %s:%s(%r, %r)", robj.mname, robj.fname, args, kwargs) req.ret = robj.func(*req.args, **req.kwargs) except Exception as eobj: req.exception = eobj hooks.call_hooks('route', 'exception', req) if not req.exception_handled: exhandler.run_handler(req) finally: hooks.call_hooks('route', 'exit', req) return req.formatter()