# Path: irisett/log.py
# def configure_logging(logtype: str, logfilename: Optional[str]=None, debug_logging: bool=False,
#                       rotate_length: int=1000000, max_rotated_files: int=250) -> None:
# def msg(logmsg: str, section: Optional[str]=None) -> None:
# def debug(logmsg: str, section: Optional[str]=None) -> None:
#     def log_msg(self, logmsg: str) -> None:
#     def log_debug(self, logmsg: str) -> None:
# class LoggingMixin:
# 
# Path: irisett/stats.py
# def get_section(section: Optional[str]) -> Dict[str, float]:
# def set(var: str, value: float, section: Optional[str] = None) -> None:
# def inc(var: str, section: Optional[str] = None) -> None:
# def dec(var: str, section: Optional[str] = None) -> None:
# def get_stats() -> Dict[str, float]:
# 
# Path: irisett/webmgmt/errors.py
# class WebMgmtError(Exception):
# class InvalidData(WebMgmtError):
# class PermissionDenied(WebMgmtError):
# class NotFound(WebMgmtError):
# class MissingLogin(WebMgmtError):
# 
# Path: irisett/errors.py
# class IrisettError(Exception):
#     def __str__(self) -> str:
#         if len(self.args) == 1:
#             ret = self.args[0]
#         else:
#             ret = str(self.__class__.__name__)
#         return ret


from typing import Optional, Callable, Any
from aiohttp import web
from irisett import (
    log,
    stats,
)
from irisett.webmgmt import (
    errors,
)
from irisett.errors import IrisettError
import base64
import binascii
    return middleware_handler


# noinspection PyUnusedLocal
async def error_handler_middleware_factory(app: web.Application, handler: Any) -> Callable:
    """Error handling middle.

    Catch errors raised in web views and try to return a corresponding
    HTTP error code.
    """
    async def middleware_handler(request: web.Request) -> web.Response:
        errcode = None
        errmsg = None
        ret = None
        headers = {}
        try:
            ret = await handler(request)
        except errors.NotFound as e:
            errcode = 404
            errmsg = str(e) or 'not found'
        except errors.PermissionDenied as e:
            errcode = 401
            errmsg = str(e) or 'permission denied'
        except errors.MissingLogin as e:
            errcode = 401
            errmsg = str(e) or 'permission denied'
            headers['WWW-Authenticate'] = 'Basic realm="Restricted"'
        except errors.InvalidData as e:
            errcode = 400
            errmsg = str(e) or 'invalid data'

# Path: irisett/log.py
# def configure_logging(logtype: str, logfilename: Optional[str]=None, debug_logging: bool=False,
#                       rotate_length: int=1000000, max_rotated_files: int=250) -> None:
# def msg(logmsg: str, section: Optional[str]=None) -> None:
# def debug(logmsg: str, section: Optional[str]=None) -> None:
#     def log_msg(self, logmsg: str) -> None:
#     def log_debug(self, logmsg: str) -> None:
# class LoggingMixin:
# 
# Path: irisett/stats.py
# def get_section(section: Optional[str]) -> Dict[str, float]:
# def set(var: str, value: float, section: Optional[str] = None) -> None:
# def inc(var: str, section: Optional[str] = None) -> None:
# def dec(var: str, section: Optional[str] = None) -> None:
# def get_stats() -> Dict[str, float]:
# 
# Path: irisett/webmgmt/errors.py
# class WebMgmtError(Exception):
# class InvalidData(WebMgmtError):
# class PermissionDenied(WebMgmtError):
# class NotFound(WebMgmtError):
# class MissingLogin(WebMgmtError):
# 
# Path: irisett/errors.py
# class IrisettError(Exception):
#     def __str__(self) -> str:
#         if len(self.args) == 1:
#             ret = self.args[0]
#         else:
#             ret = str(self.__class__.__name__)
#         return ret


# Path: irisett/webmgmt/middleware.py
from typing import Optional, Callable, Any
from aiohttp import web
from irisett import (
    log,
    stats,
)
from irisett.webmgmt import (
    errors,
)
from irisett.errors import IrisettError
import base64
import binascii

    return middleware_handler


# noinspection PyUnusedLocal
async def error_handler_middleware_factory(app: web.Application, handler: Any) -> Callable:
    """Error handling middle.

    Catch errors raised in web views and try to return a corresponding
    HTTP error code.
    """
    async def middleware_handler(request: web.Request) -> web.Response:
        errcode = None
        errmsg = None
        ret = None
        headers = {}
        try:
            ret = await handler(request)
        except errors.NotFound as e:
            errcode = 404
            errmsg = str(e) or 'not found'
        except errors.PermissionDenied as e:
            errcode = 401
            errmsg = str(e) or 'permission denied'
        except errors.MissingLogin as e:
            errcode = 401
            errmsg = str(e) or 'permission denied'
            headers['WWW-Authenticate'] = 'Basic realm="Restricted"'
        except errors.InvalidData as e:
            errcode = 400
            errmsg = str(e) or 'invalid data'

        except errors.WebMgmtError as e: