def make_middleware_chain(middlewares, endpoint, render, preprovided): """ Expects de-duplicated and conflict-free middleware/endpoint/render functions. """ _next_exc_msg = "argument 'next' reserved for middleware use only (%r)" if 'next' in get_arg_names(endpoint): raise NameError(_next_exc_msg % endpoint) if 'next' in get_arg_names(render): raise NameError(_next_exc_msg % render) req_avail = set(preprovided) - set(['next', 'context']) req_sigs = [(mw.request, mw.provides) for mw in middlewares if mw.request] req_funcs, req_provides = zip(*req_sigs) or ((), ()) req_all_provides = set(itertools.chain.from_iterable(req_provides)) ep_avail = req_avail | req_all_provides ep_sigs = [(mw.endpoint, mw.endpoint_provides) for mw in middlewares if mw.endpoint] ep_funcs, ep_provides = zip(*ep_sigs) or ((), ()) ep_chain, ep_args, ep_unres = make_chain(ep_funcs, ep_provides, endpoint, ep_avail) if ep_unres: raise NameError("unresolved endpoint middleware arguments: %r" % list(ep_unres)) rn_avail = ep_avail | set(['context']) rn_sigs = [(mw.render, mw.render_provides) for mw in middlewares if mw.render] rn_funcs, rn_provides = zip(*rn_sigs) or ((), ()) rn_chain, rn_args, rn_unres = make_chain(rn_funcs, rn_provides, render, rn_avail) if rn_unres: raise NameError("unresolved render middleware arguments: %r" % list(rn_unres)) req_args = (ep_args | rn_args) - set(['context']) req_func = _create_request_inner(ep_chain, rn_chain, req_args, ep_args, rn_args) req_chain, req_chain_args, req_unres = make_chain(req_funcs, req_provides, req_func, req_avail) if req_unres: raise NameError("unresolved request middleware arguments: %r" % list(req_unres)) return req_chain
def arguments(self): args = [] for func_name in ('request', 'endpoint', 'render'): func = getattr(self, func_name, None) if func: args.extend(get_arg_names(func)) return set(args)
def requires(self): reqs = [] for func_name in ('request', 'endpoint', 'render'): func = getattr(self, func_name, None) if func: reqs.extend(get_arg_names(func, True)) unique_reqs = set(reqs) unique_reqs.discard('next') return list(unique_reqs)
def check_middleware(mw): for f_name in ('request', 'endpoint', 'render'): func = getattr(mw, f_name, None) if not func: continue if not callable(func): raise TypeError('expected %s.%s to be a function' % (mw.name, f_name)) if not get_arg_names(func)[0] == 'next': raise TypeError("middleware functions must take argument" " 'next' as the first parameter (%s.%s)" % (mw.name, f_name))
def __init__(self, rule_str, endpoint, render_arg=None, *a, **kw): super(Route, self).__init__(rule_str, *a, endpoint=endpoint, **kw) self._middlewares = [] self._resources = {} self._bound_apps = [] self.endpoint_args = get_arg_names(endpoint) self._execute = None self._render = None self._render_factory = None self.render_arg = render_arg if callable(render_arg): self._render = render_arg