-
Notifications
You must be signed in to change notification settings - Fork 320
/
warnings.py
31 lines (25 loc) · 1.08 KB
/
warnings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import os
import warnings
MISSING_PACKAGE_WARNINGS = {}
def warn_missing_pkg(pkg_name: str, pypi_name: str = None, extra_text: str = None, stdout_func=warnings.warn):
"""
Template for warning on missing packages, show them just once.
Args:
pkg_name: name of missing package
pypi_name: in case that package name differ from PyPI name
extra_text: additional text after the base warning
stdout_func: define used function for streaming warning, use `warnings.warn` or `logging.warning`
Returns:
number of warning calls
"""
global MISSING_PACKAGE_WARNINGS
if pkg_name not in MISSING_PACKAGE_WARNINGS:
extra_text = os.linesep + extra_text if extra_text else ''
if not pypi_name:
pypi_name = pkg_name
stdout_func(f'You want to use `{pkg_name}` which is not installed yet,'
f' install it with `pip install {pypi_name}`.' + extra_text)
MISSING_PACKAGE_WARNINGS[pkg_name] = 1
else:
MISSING_PACKAGE_WARNINGS[pkg_name] += 1
return MISSING_PACKAGE_WARNINGS[pkg_name]