Skip to content
This repository was archived by the owner on Nov 19, 2024. It is now read-only.

Commit b350c90

Browse files
authored
implement legacy logging callbacks (#96)
1 parent dc81b96 commit b350c90

File tree

3 files changed

+64
-47
lines changed

3 files changed

+64
-47
lines changed

cads_api_client/legacy_api_client.py

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,6 @@
1717
from .api_client import ApiClient
1818
from .processing import Remote, Results
1919

20-
LEGACY_KWARGS = [
21-
"full_stack",
22-
"delete",
23-
"retry_max",
24-
"sleep_max",
25-
"wait_until_complete",
26-
"info_callback",
27-
"warning_callback",
28-
"error_callback",
29-
"debug_callback",
30-
"metadata",
31-
"forget",
32-
"session",
33-
]
34-
3520
LOGGER = logging.getLogger(__name__)
3621
F = TypeVar("F", bound=Callable[..., Any])
3722

@@ -68,6 +53,15 @@ def __exit__(
6853
self.logger.removeHandler(handler)
6954

7055

56+
def _deprecated_warning(**kwargs: Any) -> None:
57+
if kwargs := {k: v for k, v in kwargs.items() if v is not None}:
58+
warnings.warn(
59+
f"The following parameters are deprecated: {kwargs}."
60+
" Set them to None to silence this warning.",
61+
UserWarning,
62+
)
63+
64+
7165
class LegacyApiClient(cdsapi.api.Client): # type: ignore[misc]
7266
def __init__(
7367
self,
@@ -78,31 +72,36 @@ def __init__(
7872
verify: bool | int | None = None,
7973
timeout: int = 60,
8074
progress: bool = True,
81-
*args: Any,
82-
**kwargs: Any,
75+
full_stack: None = None,
76+
delete: bool = False,
77+
retry_max: int = 500,
78+
sleep_max: float = 120,
79+
wait_until_complete: bool = True,
80+
info_callback: Callable[..., None] | None = None,
81+
warning_callback: Callable[..., None] | None = None,
82+
error_callback: Callable[..., None] | None = None,
83+
debug_callback: Callable[..., None] | None = None,
84+
metadata: None = None,
85+
forget: None = None,
86+
session: requests.Session | None = None,
8387
) -> None:
84-
kwargs.update(zip(LEGACY_KWARGS, args))
85-
if wrong_kwargs := set(kwargs) - set(LEGACY_KWARGS):
86-
raise ValueError(f"Wrong parameters: {wrong_kwargs}.")
88+
_deprecated_warning(full_stack=full_stack, metadata=metadata, forget=forget)
8789

8890
self.url, self.key, verify = cdsapi.api.get_url_key_verify(url, key, verify)
8991
self.verify = bool(verify)
9092
self.quiet = quiet
9193
self._debug = debug
9294
self.timeout = timeout
9395
self.progress = progress
94-
95-
self.sleep_max = kwargs.pop("sleep_max", 120)
96-
self.wait_until_complete = kwargs.pop("wait_until_complete", True)
97-
self.delete = kwargs.pop("delete", False)
98-
self.retry_max = kwargs.pop("retry_max", 500)
99-
self.session = kwargs.pop("session", requests.Session())
100-
if kwargs:
101-
warnings.warn(
102-
"This is a beta version."
103-
f" The following parameters have not been implemented yet: {kwargs}.",
104-
UserWarning,
105-
)
96+
self.delete = delete
97+
self.retry_max = retry_max
98+
self.sleep_max = sleep_max
99+
self.wait_until_complete = wait_until_complete
100+
self.info_callback = info_callback
101+
self.warning_callback = warning_callback
102+
self.error_callback = error_callback
103+
self.debug_callback = debug_callback
104+
self.session = requests.Session() if session is None else session
106105

107106
self.client = self.logging_decorator(ApiClient)(
108107
url=self.url,
@@ -173,15 +172,24 @@ def retrieve(
173172

174173
# Decorate legacy methods
175174
submitted.download = self.logging_decorator(submitted.download) # type: ignore[method-assign]
176-
submitted.log = self.logging_decorator(submitted.log) # type: ignore[method-assign]
175+
submitted.log = self.log # type: ignore[method-assign]
177176

178177
return submitted if target is None else submitted.download(target)
179178

180-
def log(self, *args: Any, **kwargs: Any) -> None:
179+
def log(self, level: int, *args: Any, **kwargs: Any) -> None:
181180
with LoggingContext(
182181
logger=LOGGER, quiet=self.quiet, debug=self._debug
183182
) as logger:
184-
logger.log(*args, **kwargs)
183+
if level == logging.INFO and self.info_callback is not None:
184+
self.info_callback(*args, **kwargs)
185+
elif level == logging.WARNING and self.warning_callback is not None:
186+
self.warning_callback(*args, **kwargs)
187+
elif level == logging.ERROR and self.error_callback is not None:
188+
self.error_callback(*args, **kwargs)
189+
elif level == logging.DEBUG and self.debug_callback is not None:
190+
self.debug_callback(*args, **kwargs)
191+
else:
192+
logger.log(level, *args, **kwargs)
185193

186194
def info(self, *args: Any, **kwargs: Any) -> None:
187195
self.log(logging.INFO, *args, **kwargs)

cads_api_client/processing.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,8 @@ def _from_rel_href(self, rel: str) -> Self | None:
223223
out = None
224224
return out
225225

226-
def log(self, *args: Any, **kwargs: Any) -> None:
227-
LOGGER.log(*args, **kwargs)
226+
def log(self, level: int, *args: Any, **kwargs: Any) -> None:
227+
LOGGER.log(level, *args, **kwargs)
228228

229229
def info(self, *args: Any, **kwargs: Any) -> None:
230230
self.log(logging.INFO, *args, **kwargs)
@@ -579,8 +579,8 @@ def reply(self) -> dict[str, Any]:
579579
reply.setdefault("request_id", self.request_uid)
580580
return reply
581581

582-
def log(self, *args: Any, **kwargs: Any) -> None:
583-
LOGGER.log(*args, **kwargs)
582+
def log(self, level: int, *args: Any, **kwargs: Any) -> None:
583+
LOGGER.log(level, *args, **kwargs)
584584

585585
def info(self, *args: Any, **kwargs: Any) -> None:
586586
self.log(logging.INFO, *args, **kwargs)

tests/integration_test_70_legacy_api_client.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -199,14 +199,6 @@ def test_legacy_api_client_kwargs(api_root_url: str, api_anon_key: str) -> None:
199199
assert client.client.session is session
200200

201201

202-
def test_legacy_api_client_error(
203-
api_root_url: str,
204-
api_anon_key: str,
205-
) -> None:
206-
with pytest.raises(ValueError, match="Wrong parameters: {'foo'}"):
207-
LegacyApiClient(url=api_root_url, key=api_anon_key, foo="bar")
208-
209-
210202
def test_legacy_api_client_logging(
211203
caplog: pytest.LogCaptureFixture, legacy_client: LegacyApiClient
212204
) -> None:
@@ -280,3 +272,20 @@ def test_legacy_api_client_remote(
280272
actual_target = remote.download(target)
281273
assert target == actual_target
282274
assert os.path.getsize(target) == 1
275+
276+
277+
def test_legacy_api_client_warning(
278+
api_root_url: str,
279+
api_anon_key: str,
280+
) -> None:
281+
with pytest.warns(
282+
UserWarning,
283+
match="deprecated: {'full_stack': 'a', 'metadata': 'b', 'forget': 'c'}",
284+
):
285+
LegacyApiClient(
286+
url=api_root_url,
287+
key=api_anon_key,
288+
full_stack="a", # type: ignore[arg-type]
289+
metadata="b", # type: ignore[arg-type]
290+
forget="c", # type: ignore[arg-type]
291+
)

0 commit comments

Comments
 (0)