Source code for pcapkit.utilities.warnings

# -*- coding: utf-8 -*-
"""User Defined Warnings
===========================

.. module:: pcapkit.utilities.warnings

:mod:`pcapkit.warnings` refined built-in warnings.

"""
import warnings
from typing import TYPE_CHECKING

from pcapkit.utilities.exceptions import stacklevel as stacklevel_calculator
from pcapkit.utilities.logging import DEVMODE, VERBOSE, logger

if TYPE_CHECKING:
    from typing import Any, Optional, Type, Union

__all__ = [
    'warn',

    # UserWarning
    'BaseWarning',
    # ImportWarning
    'FormatWarning', 'EngineWarning', 'InvalidVendorWarning',
    # RuntimeWarning
    'FileWarning', 'LayerWarning', 'ProtocolWarning', 'AttributeWarning',
    'DevModeWarning', 'VendorRequestWarning', 'VendorRuntimeWarning',
    'UnknownFieldWarning', 'RegistryWarning', 'SchemaWarning', 'InfoWarning',
    'SeekWarning', 'ExtractionWarning',
    # ResourceWarning
    'DPKTWarning', 'ScapyWarning', 'PySharkWarning', 'EmojiWarning',
    'VendorWarning',
    # DeprecationWarning
    'DeprecatedFormatWarning',
]


[docs] def warn(message: 'Union[str, Warning]', category: 'Type[Warning]', stacklevel: 'Optional[int]' = None) -> 'None': """Wrapper function of :func:`warnings.warn`. Args: message: Warning message. category: Warning category. stacklevel: Warning stack level. """ if stacklevel is None: stacklevel = stacklevel_calculator() logger.warning(message, exc_info=VERBOSE, stack_info=VERBOSE, stacklevel=stacklevel) warnings.warn(message, category, stacklevel)
############################################################################## # BaseWarning (abc of warnings) session. ##############################################################################
[docs] class BaseWarning(UserWarning): """Base warning class of all kinds.""" def __init__(self, *args: 'Any', **kwargs: 'Any') -> 'None': # pylint: disable=useless-super-delegation # log warning if DEVMODE: if VERBOSE: logger.warning(str(self), exc_info=self, stack_info=True, stacklevel=stacklevel_calculator()) else: logger.warning(str(self)) else: warnings.simplefilter('ignore', type(self)) # warnings.simplefilter('default') super().__init__(*args, **kwargs)
############################################################################## # ImportWarning session. ##############################################################################
[docs] class FormatWarning(BaseWarning, ImportWarning): """Warning on unknown format(s)."""
[docs] class EngineWarning(BaseWarning, ImportWarning): """Unsupported extraction engine."""
[docs] class InvalidVendorWarning(BaseWarning, ImportWarning): """Vendor CLI invalid updater."""
############################################################################## # RuntimeWarning session. ##############################################################################
[docs] class FileWarning(BaseWarning, RuntimeWarning): """Warning on file(s)."""
[docs] class LayerWarning(BaseWarning, RuntimeWarning): """Unrecognised layer."""
[docs] class ProtocolWarning(BaseWarning, RuntimeWarning): """Unrecognised protocol."""
[docs] class AttributeWarning(BaseWarning, RuntimeWarning): """Unsupported attribute."""
[docs] class DevModeWarning(BaseWarning, RuntimeWarning): """Run in development mode."""
[docs] class VendorRequestWarning(BaseWarning, RuntimeWarning): """Vendor request connection failed."""
[docs] class VendorRuntimeWarning(BaseWarning, RuntimeWarning): """Vendor failed during runtime."""
[docs] class UnknownFieldWarning(BaseWarning, RuntimeWarning): """Unknown field."""
[docs] class RegistryWarning(BaseWarning, RuntimeWarning): """Registry warning."""
[docs] class SchemaWarning(BaseWarning, RuntimeWarning): """Schema warning."""
[docs] class InfoWarning(BaseWarning, RuntimeWarning): """Info class warning."""
[docs] class SeekWarning(BaseWarning, RuntimeWarning): """Seek operation warning."""
[docs] class ExtractionWarning(BaseWarning, RuntimeWarning): """Extraction warning."""
############################################################################## # ResourceWarning session. ##############################################################################
[docs] class DPKTWarning(BaseWarning, ResourceWarning): """Warnings on DPKT usage."""
[docs] class ScapyWarning(BaseWarning, ResourceWarning): """Warnings on Scapy usage."""
[docs] class PySharkWarning(BaseWarning, ResourceWarning): """Warnings on PyShark usage."""
[docs] class EmojiWarning(BaseWarning, ResourceWarning): """Warnings on Emoji usage."""
[docs] class VendorWarning(BaseWarning, ResourceWarning): """Warnings on vendor usage."""
############################################################################## # DeprecationWarning session. ##############################################################################
[docs] class DeprecatedFormatWarning(BaseWarning, DeprecationWarning): """Warning on deprecated formats."""