Skip to content

omnipy.util.contexts

CLASS DESCRIPTION
LastErrorHolder
PrintExceptionContext
FUNCTION DESCRIPTION
hold_and_reset_prev_attrib_value
nothing
setup_and_teardown_callback_context
ATTRIBUTE DESCRIPTION
print_exception

print_exception module-attribute

print_exception = PrintExceptionContext()

LastErrorHolder

Bases: AbstractContextManager

METHOD DESCRIPTION
__init__
raise_derived
Source code in src/omnipy/util/contexts.py
class LastErrorHolder(AbstractContextManager):
    def __init__(self):
        self._last_error = None

    def __enter__(self):
        ...

    def __exit__(self, exc_type, exc_val, exc_tb):
        if exc_val is not None:
            self._last_error = exc_val
        return True

    def raise_derived(self, exc: Exception):
        if self._last_error is not None:
            raise exc from self._last_error
        else:
            raise exc

__init__

__init__()
Source code in src/omnipy/util/contexts.py
def __init__(self):
    self._last_error = None

raise_derived

raise_derived(exc: Exception)
Source code in src/omnipy/util/contexts.py
def raise_derived(self, exc: Exception):
    if self._last_error is not None:
        raise exc from self._last_error
    else:
        raise exc

PrintExceptionContext

Bases: AbstractContextManager

Source code in src/omnipy/util/contexts.py
class PrintExceptionContext(AbstractContextManager):
    def __enter__(self):
        ...

    def __exit__(self, exc_type, exc_val, exc_tb):
        print(f'{exc_type.__name__}: {str(exc_val).splitlines()[0]}', end='')
        return True

hold_and_reset_prev_attrib_value

hold_and_reset_prev_attrib_value(
    obj: object, attr_name: str, copy_attr: bool = False
) -> Iterator[None]
Source code in src/omnipy/util/contexts.py
@contextmanager
def hold_and_reset_prev_attrib_value(
    obj: object,
    attr_name: str,
    copy_attr: bool = False,
) -> Iterator[None]:
    attr_value = getattr(obj, attr_name)
    prev_value = deepcopy(attr_value) if copy_attr else attr_value

    try:
        yield
    finally:
        setattr(obj, attr_name, prev_value)

nothing

nothing(*args, **kwds) -> Iterator[None]
Source code in src/omnipy/util/contexts.py
@contextmanager
def nothing(*args, **kwds) -> Iterator[None]:
    yield None

setup_and_teardown_callback_context

setup_and_teardown_callback_context(
    *,
    setup_func: Callable[..., _ValT] | None = None,
    setup_func_args: tuple[object, ...] = (),
    setup_func_kwargs: dict[str, object] = {},
    exception_func: Callable[..., None] | None = None,
    exception_func_args: tuple[object, ...] = (),
    exception_func_kwargs: dict[str, object] = {},
    teardown_func: Callable[..., None] | None = None,
    teardown_func_args: tuple[object, ...] = (),
    teardown_func_kwargs: dict[str, object] = {},
) -> Iterator[_ValT | None]
Source code in src/omnipy/util/contexts.py
@contextmanager
def setup_and_teardown_callback_context(
    *,
    setup_func: Callable[..., _ValT] | None = None,
    setup_func_args: tuple[object, ...] = (),
    setup_func_kwargs: dict[str, object] = {},
    exception_func: Callable[..., None] | None = None,
    exception_func_args: tuple[object, ...] = (),
    exception_func_kwargs: dict[str, object] = {},
    teardown_func: Callable[..., None] | None = None,
    teardown_func_args: tuple[object, ...] = (),
    teardown_func_kwargs: dict[str, object] = {},
) -> Iterator[_ValT | None]:
    setup_val: _ValT | None = None
    if setup_func is not None:
        setup_val = setup_func(*setup_func_args, **setup_func_kwargs)
    try:
        yield setup_val
    except Exception:
        if exception_func is not None:
            exception_func(*exception_func_args, **exception_func_kwargs)
        raise
    finally:
        if teardown_func is not None:
            teardown_func(*teardown_func_args, **teardown_func_kwargs)