<commit_before>#
# growler/middleware_chain.py
#
"""
"""

from inspect import signature
from collections import namedtuple


class MiddlewareChain:
    """
    Class handling the storage and retreival of growler middleware functions.
    """

    MiddlewareTuple = namedtuple('MiddlewareTuple', ['func',
                                                     'path',
                                                     'mask',
                                                     'is_errorhandler',
                                                     'is_subchain',
                                                     ])

    def __init__(self):
        self.mw_list = []

    def __call__(self, method, path):
        """
        Generator yielding the middleware which matches the provided path.

        :param path: url path of the request.
        :type path: str
        """
        error_handler_stack = []
        err = None
        for mw in self.mw_list:
            method_matches = method & mw.mask
            path_matches = path.startswith(mw.path)

            if not (method_matches and path_matches):
                continue

            if mw.is_subchain:
                subchain = mw.func(method, path[len(mw.path):])
                for sub_mw in subchain:
                    err = yield sub_mw.func
                    if err:
                        sub_mw.send(err)

            elif mw.is_errorhandler:
                error_handler_stack.append(mw.func)

            else:
                err = yield mw.func

            if err:
                break

        if err:
            for errhandler in reversed(error_handler_stack):
                new_error = yield errhandler
                if new_error:
                    pass

    def add(self, method_mask, path, func):
        """
        Add a function to the middleware chain, listening on func
        """
        is_err = len(signature(func).parameters) == 3
        is_subchain = isinstance(func, MiddlewareChain)
        tup = self.MiddlewareTuple(func=func,
                                   mask=method_mask,
                                   path=path,
                                   is_errorhandler=is_err,
                                   is_subchain=is_subchain,)
        self.mw_list.append(tup)

    def __contains__(self, func):
        return any(mw.func is func for mw in self.mw_list)

    def last_router(self):
        from router import Router
        if not isinstance(self.mw_list[-1].func, Router):
            self.add(0xFFF, '/', Router())
        return self.mw_list[-1].func
<commit_msg>middleware_chain: Moved MiddlewareTuple to outside the class definition<commit_after>#
# growler/middleware_chain.py
#
"""
"""

from inspect import signature
from collections import namedtuple

MiddlewareTuple = namedtuple('MiddlewareTuple', ['func',
                                                 'path',
                                                 'mask',
                                                 'is_errorhandler',
                                                 'is_subchain',
                                                 ])


class MiddlewareChain:
    """
    Class handling the storage and retreival of growler middleware functions.
    """

    def __init__(self):
        self.mw_list = []

    def __call__(self, method, path):
        """
        Generator yielding the middleware which matches the provided path.

        :param path: url path of the request.
        :type path: str
        """
        error_handler_stack = []
        err = None
        for mw in self.mw_list:
            method_matches = method & mw.mask
            path_matches = path.startswith(mw.path)

            if not (method_matches and path_matches):
                continue

            if mw.is_subchain:
                subchain = mw.func(method, path[len(mw.path):])
                for sub_mw in subchain:
                    err = yield sub_mw.func
                    if err:
                        sub_mw.send(err)

            elif mw.is_errorhandler:
                error_handler_stack.append(mw.func)

            else:
                err = yield mw.func

            if err:
                break

        if err:
            for errhandler in reversed(error_handler_stack):
                new_error = yield errhandler
                if new_error:
                    pass

    def add(self, method_mask, path, func):
        """
        Add a function to the middleware chain, listening on func
        """
        is_err = len(signature(func).parameters) == 3
        is_subchain = isinstance(func, MiddlewareChain)
        tup = MiddlewareTuple(func=func,
                              mask=method_mask,
                              path=path,
                              is_errorhandler=is_err,
                              is_subchain=is_subchain,)
        self.mw_list.append(tup)

    def __contains__(self, func):
        return any(mw.func is func for mw in self.mw_list)

    def last_router(self):
        from router import Router
        if not isinstance(self.mw_list[-1].func, Router):
            self.add(0xFFF, '/', Router())
        return self.mw_list[-1].func

{'fraction_non_alphanumeric': 0.07655502392344497, 'fraction_numerical': 0.0015311004784688996, 'mean_word_length': 1.269214068606166, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1654151', 'n_tokens_mistral': 1350, 'n_tokens_neox': 1262, 'n_words': 333}