forked from stapi-spec/pystapi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwarns.py
More file actions
96 lines (66 loc) · 2.73 KB
/
warns.py
File metadata and controls
96 lines (66 loc) · 2.73 KB
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 PystapiClientWarning(UserWarning):
"""Base warning class"""
...
class NoConformsTo(PystapiClientWarning):
"""Inform user when client does not have "conformsTo" set"""
def __str__(self) -> str:
return "Server does not advertise any conformance classes."
class DoesNotConformTo(PystapiClientWarning):
"""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(PystapiClientWarning):
"""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 FallbackToPystapi(PystapiClientWarning):
"""Inform user when falling back to Pystapi implementation"""
def __str__(self) -> str:
return "Falling back to Pystapi. This might be slow."
@contextmanager
def strict() -> Iterator[None]:
"""Context manager for raising all Pystapi-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 pystapi_client import Client
>>> from pystapi_client.warnings import strict
>>> with strict():
... Client.open("https://perfect-api.test")
For finer-grained control:
>>> import warnings
>>> from pystapi_client import Client
>>> from pystapi_client.warnings import MissingLink
>>> warnings.filterwarnings("error", category=FallbackToPystapi)
>>> Client.open("https://imperfect-api.test")
"""
warnings.filterwarnings("error", category=PystapiClientWarning)
try:
yield
finally:
warnings.filterwarnings("default", category=PystapiClientWarning)
@contextmanager
def ignore() -> Iterator[None]:
"""Context manager for ignoring all pystapi-client warnings
For more fine-grained control or to set filter warnings in the whole
python session, use the ``warnings`` module directly.
Examples:
>>> from pystapi_client import Client
>>> from pystapi_client.warnings import ignore
>>> with ignore():
... Client.open("https://perfect-api.test")
For finer-grained control:
>>> import warnings
>>> from pystapi_client import Client
>>> from pystapi_client.warnings import MissingLink
>>> warnings.filterwarnings("ignore", category=MissingLink)
>>> Client.open("https://imperfect-api.test")
"""
warnings.filterwarnings("ignore", category=PystapiClientWarning)
try:
yield
finally:
warnings.filterwarnings("default", category=PystapiClientWarning)