HEX
Server: Apache/2.4.46 (Win64) OpenSSL/1.1.1j PHP/8.4.25
System: Windows NT DESKTOP-4TAV2RJ 10.0 build 19045 (Windows 10) AMD64
User: fred (0)
PHP: 8.4.25
Disabled: NONE
Upload Files
File: C:/Users/fred/anaconda3/Lib/site-packages/anaconda_navigator/utils/anaconda_solvers/utilities.py
# -*- coding: utf8 -*-

"""Helper utilities to catch anaconda-client issues."""

from __future__ import annotations

__all__ = ['catch_and_notify']

import collections.abc
import contextlib
import warnings

import binstar_client

from anaconda_navigator.utils import notifications


@contextlib.contextmanager
def catch_and_notify(*, reraise: bool = False) -> collections.abc.Iterator[None]:
    """
    Catch exceptions/warnings and init :class:`~MessageBoxInformation` that will be passed to the notification queue.

    The dialog will not be passed to the notification queue if `once` is set to `True` and dialog is already queued.
    """
    try:
        warns: list[warnings.WarningMessage]
        with warnings.catch_warnings(record=True) as warns:
            warnings.filterwarnings('ignore', category=DeprecationWarning)
            yield

        warning: warnings.WarningMessage
        for warning in warns:
            notifications.NOTIFICATION_QUEUE.push(
                message=str(warning.message),  # pylint: disable=no-member
                caption='Anaconda warning',
                tags=('anaconda', 'warning')
            )

    except binstar_client.BinstarError as error:
        notifications.NOTIFICATION_QUEUE.push(
            message=str(error.message),  # pylint: disable=no-member
            caption='Anaconda error',
            tags=('anaconda', 'warning')
        )
        if reraise:
            raise