"""Custom Warnings for tremana."""
from __future__ import annotations
import os
from contextlib import contextmanager
from typing import Any
from typing import Generator
from typing import Iterable
from warnings import catch_warnings
from warnings import filterwarnings
from tremana import __repo_short_url__
[docs]class TremanaBaseWarning(UserWarning):
"""Basewarning for expected possible errors with tremana."""
def __init__(
self,
*args: object,
msg: str,
origin_file: str | os.PathLike[str] | None = None,
append_msg: str | None = None,
) -> None: # noqa: D205, D400
"""
Parameters
----------
msg : str
Message to be printed.
origin_file : Union[str, os.PathLike], optional
Path to the file causing the warning, by default None
append_msg : Optional[str], optional
Message to be appended on ``msg``, by default None
.. # noqa: DAR101
"""
if origin_file:
msg = f"{msg}\n\nThis warning was caused processing:\n {str(origin_file)}"
if append_msg:
msg = f"{msg}\n\n{append_msg}"
super().__init__(f"{msg}", *args)
[docs]@contextmanager
def filter_tremana_warnings(
warning_types: Iterable[type[TremanaBaseWarning]], message: str = ""
) -> Generator[None, None, None]:
"""Contextmanager to filter all warnings defined in ``warning_types``.
Parameters
----------
warning_types : Iterable[Type[TremanaBaseWarning]]
Warnings to be filtered
message : str
Regular expression to filter warnings by.
.. # noqa: DAR301
.. # noqa: DAR101
"""
with catch_warnings():
for warning in warning_types:
filterwarnings("ignore", category=warning, message=f".+{message}")
yield
[docs]class TremanaSupressableWarning(TremanaBaseWarning):
"""Basewarning for supressable warnings.
This baseclass can be used to conveniently suppress all none critical warnings.
See Also
--------
filter_tremana_warnings
"""
def __init__(
self,
*args: object,
msg: str,
append_msg: str | None = None,
**kwargs: Any,
) -> None: # noqa: D205, D400
"""
Parameters
----------
msg : str
Message to be printed.
append_msg : Optional[str], optional
Message to be appended on ``msg``, by default None
.. # noqa: DAR101
"""
supress_msg = "If you want to suppress this warning please consult the documentation."
if append_msg:
supress_msg = f"{append_msg}\n\n{supress_msg}"
super().__init__(*args, msg=msg, append_msg=supress_msg, **kwargs)
[docs]class TremanaNotSupportedWarning(TremanaSupressableWarning):
"""Basewarning for not yet supported features."""
def __init__(self, *args: object, msg: str, **kwargs: Any) -> None: # noqa: D205, D400
"""
Parameters
----------
msg : str
Message to be printed.
.. # noqa: DAR101
"""
append_msg = f"If you needs this feature open a feature request at {__repo_short_url__}."
super().__init__(*args, msg=msg, append_msg=append_msg, **kwargs)
[docs]class TremanaParsingIgnoredSignalTypeWarning(TremanaNotSupportedWarning):
"""Warning for not supported signal types."""
def __init__(self, *args: object, signal_type: str, **kwargs: Any) -> None: # noqa: D205, D400
"""
Parameters
----------
signal_type : str
Value of the signal type.
.. # noqa: DAR101
"""
msg = f"Signals of type {signal_type!r} aren't used by tremana yet and will be ignored."
super().__init__(*args, msg=msg, **kwargs)