from .base import DebugPanel
from ..utils import STATIC_ROUTE_NAME

__all__ = ['MiddlewaresDebugPanel']
_ = lambda x: x


class MiddlewaresDebugPanel(DebugPanel):
    """
    A panel to display the middlewares used by your Pyramid application.
    """
    name = 'Middlewares'
    has_content = True
    template = 'middlewares.dbtmako'
    title = _('Middlewares')
    nav_title = title

    def __init__(self, request):
        super().__init__(request)
        if not request.app.middlewares:
            self.has_content = False
            self.is_active = False
        else:
            self.populate(request)

    def populate(self, request):
        # TODO: fix this works only for functions and classes
        middlewares = [t.__name__ for t in request.app.middlewares]
        self.data = {'middlewares': middlewares}

    def render_vars(self, request):
        static_path = self._request.app.router[STATIC_ROUTE_NAME]\
            .url(filename='')
        return {'static_path': static_path}

from .base import DebugPanel
from ..utils import STATIC_ROUTE_NAME

__all__ = ['MiddlewaresDebugPanel']
_ = lambda x: x


class MiddlewaresDebugPanel(DebugPanel):
    """
    A panel to display the middlewares used by your Pyramid application.
    """
    name = 'Middlewares'
    has_content = True
    template = 'middlewares.dbtmako'
    title = _('Middlewares')
    nav_title = title

    def __init__(self, request):
        super().__init__(request)
        if not request.app.middlewares:
            self.has_content = False
            self.is_active = False
        else:
            self.populate(request)

    def populate(self, request):
        middleware_names = []
        for m in request.app.middlewares:
            if hasattr(m, '__name__'):
                # name for regular functions
                middleware_names.append(m.__name__)
            else:
                middleware_names.append(m.__repr__())
        self.data = {'middlewares': middleware_names}

    def render_vars(self, request):
        static_path = self._request.app.router[STATIC_ROUTE_NAME]\
            .url(filename='')
        return {'static_path': static_path}

use __repr__ if __name__ attr is not accesible in middlewares panel