HPI/my/core/warnings.py
Dima Gerasimov 016f28250b general: initial flake8 checks (for now manual)
fix fairly uncontroversial stuff in my.core like
- line spacing, which isn't too annoying (e.g. unlike many inline whitespace checks that break vertical formatting)
- unused imports/variables
- too broad except
2022-06-05 22:28:38 +01:00

52 lines
No EOL
1.5 KiB
Python

'''
A helper to make warnings a bit more visible.
TODO ideally would be great to replace with some existing solution, or find a better way,
since who looks at the terminal output?
E.g. would be nice to propagate the warnings in the UI (it's even a subclass of Exception!)
'''
import sys
from typing import Optional
import warnings
import click
def _colorize(x: str, color: Optional[str]=None) -> str:
if color is None:
return x
if not sys.stdout.isatty():
return x
# click handles importing/initializing colorama if necessary
# on windows it installs it if necessary
# on linux/mac, it manually handles ANSI without needing termcolor
return click.style(x, fg=color)
def _warn(message: str, *args, color: Optional[str]=None, **kwargs) -> None:
stacklevel = kwargs.get('stacklevel', 1)
kwargs['stacklevel'] = stacklevel + 2 # +1 for this function, +1 for medium/high wrapper
warnings.warn(_colorize(message, color=color), *args, **kwargs)
def low(message: str, *args, **kwargs) -> None:
# kwargs['color'] = 'grey' # eh, grey is way too pale
_warn(message, *args, **kwargs)
def medium(message: str, *args, **kwargs) -> None:
kwargs['color'] = 'yellow'
_warn(message, *args, **kwargs)
def high(message: str, *args, **kwargs) -> None:
'''
Meant for deprecations, i.e. things that better get some user attention
'''
kwargs['color'] = 'red'
_warn(message, *args, **kwargs)
# NOTE: deprecated -- legacy import
from warnings import warn