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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
import warnings
from collections.abc import Iterator
from contextlib import contextmanager
class PystacClientWarning(UserWarning):
"""Base warning class"""
...
class NoConformsTo(PystacClientWarning):
"""Inform user when client does not have "conformsTo" set"""
def __str__(self) -> str:
return "Server does not advertise any conformance classes."
class DoesNotConformTo(PystacClientWarning):
"""Inform user when client does not conform to extension"""
def __str__(self) -> str:
return "Server does not conform to {}".format(", ".join(self.args))
class MissingLink(PystacClientWarning):
"""Inform user when link is not found"""
def __str__(self) -> str:
return "No link with rel='{}' could be found on this {}.".format(*self.args)
class FallbackToPystac(PystacClientWarning):
"""Inform user when falling back to pystac implementation"""
def __str__(self) -> str:
return "Falling back to pystac. This might be slow."
@contextmanager
def strict() -> Iterator[None]:
"""Context manager for raising all pystac-client warnings as errors
For more fine-grained control or to filter warnings in the whole
python session, use the :py:mod:`warnings` module directly.
Examples:
>>> from pystac_client import Client
>>> from pystac_client.warnings import strict
>>> with strict():
... Client.open("https://perfect-api.test")
For finer-grained control:
>>> import warnings
>>> from pystac_client import Client
>>> from pystac_client.warnings import MissingLink
>>> warnings.filterwarnings("error", category=FallbackToPystac)
>>> Client.open("https://imperfect-api.test")
"""
warnings.filterwarnings("error", category=PystacClientWarning)
try:
yield
finally:
warnings.filterwarnings("default", category=PystacClientWarning)
@contextmanager
def ignore() -> Iterator[None]:
"""Context manager for ignoring all pystac-client warnings
For more fine-grained control or to set filter warnings in the whole
python session, use the ``warnings`` module directly.
Examples:
>>> from pystac_client import Client
>>> from pystac_client.warnings import ignore
>>> with ignore():
... Client.open("https://perfect-api.test")
For finer-grained control:
>>> import warnings
>>> from pystac_client import Client
>>> from pystac_client.warnings import MissingLink
>>> warnings.filterwarnings("ignore", category=MissingLink)
>>> Client.open("https://imperfect-api.test")
"""
warnings.filterwarnings("ignore", category=PystacClientWarning)
try:
yield
finally:
warnings.filterwarnings("default", category=PystacClientWarning)
|