From 287e29dfd19c2ea447d40f6326e68fb894d51cf3 Mon Sep 17 00:00:00 2001 From: duckduckgrayduck <102841251+duckduckgrayduck@users.noreply.github.com> Date: Sat, 2 Dec 2023 11:12:23 -0600 Subject: [PATCH] Fixed r! and lazy formatting for logs --- documentcloud/base.py | 2 +- documentcloud/client.py | 4 ++-- documentcloud/documents.py | 34 ++++++++++++++++++++++------------ 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/documentcloud/base.py b/documentcloud/base.py index ed6eae3..feae7ce 100644 --- a/documentcloud/base.py +++ b/documentcloud/base.py @@ -33,7 +33,7 @@ def __init__( ] def __repr__(self): - return f"" # pragma: no cover + return f"" # pragma: no cover def __str__(self): return f"[{', '.join(str(r) for r in self.results)}]" diff --git a/documentcloud/client.py b/documentcloud/client.py index 0c8f8d4..e91dfc5 100644 --- a/documentcloud/client.py +++ b/documentcloud/client.py @@ -126,7 +126,7 @@ def user_id(self): def _request(self, method, url, raise_error=True, **kwargs): """Generic method to make API requests""" # pylint: disable=method-hidden - logger.info(f"request: {method} - {url} - {kwargs}") + logger.info("request: %s - %s - %s", method, url, kwargs) set_tokens = kwargs.pop("set_tokens", True) full_url = kwargs.pop("full_url", False) @@ -142,7 +142,7 @@ def _request(self, method, url, raise_error=True, **kwargs): response = requests_retry_session(session=self.session).request( method, url, timeout=self.timeout, **kwargs ) - logger.debug(f"response: {response.status_code} - {response.content}") + logger.debug("response: %s - %s", response.status_code, response.content) if response.status_code == requests.codes.FORBIDDEN and set_tokens: self._set_tokens() # track set_tokens to not enter an infinite loop diff --git a/documentcloud/documents.py b/documentcloud/documents.py index 9202e74..983cb52 100644 --- a/documentcloud/documents.py +++ b/documentcloud/documents.py @@ -394,7 +394,9 @@ def upload_directory(self, path, handle_errors=False, extensions=".pdf", **kwarg path_list = self._collect_files(path, extensions) logger.info( - f"Upload directory on {path}: Found {len(path_list)} files to upload" + "Upload directory on %s: Found %d files to upload", + path, + len(path_list) ) # Upload all the files using the bulk API to reduce the number @@ -405,7 +407,7 @@ def upload_directory(self, path, handle_errors=False, extensions=".pdf", **kwarg # Grouper will put None's on the end of the last group file_paths = [p for p in file_paths if p is not None] - logger.info(f"Uploading group {i + 1}:\n" + "\n".join(file_paths)) + logger.info("Uploading group %d:\n%s", i + 1, "\n".join(file_paths)) # Create the documents logger.info("Creating the documents...") @@ -430,8 +432,9 @@ def upload_directory(self, path, handle_errors=False, extensions=".pdf", **kwarg except (APIError, RequestException) as exc: if handle_errors: logger.info( - f"Error creating the following documents: {exc}\n" - + "\n".join(file_paths) + "Error creating the following documents: %s\n%s", + exc, + "\n".join(file_paths) ) continue else: @@ -442,7 +445,7 @@ def upload_directory(self, path, handle_errors=False, extensions=".pdf", **kwarg obj_list.extend(create_json) presigned_urls = [j["presigned_url"] for j in create_json] for url, file_path in zip(presigned_urls, file_paths): - logger.info(f"Uploading {file_path} to S3...") + logger.info("Uploading %s to S3...", file_path) try: response = requests_retry_session().put( url, data=open(file_path, "rb").read() @@ -451,7 +454,9 @@ def upload_directory(self, path, handle_errors=False, extensions=".pdf", **kwarg except (APIError, RequestException) as exc: if handle_errors: logger.info( - f"Error uploading the following document: {exc} {file_path}" + "Error uploading the following document: %s %s", + exc, + file_path ) continue else: @@ -465,8 +470,9 @@ def upload_directory(self, path, handle_errors=False, extensions=".pdf", **kwarg except (APIError, RequestException) as exc: if handle_errors: logger.info( - f"Error creating the following documents: {exc}\n" - + "\n".join(file_paths) + "Error creating the following documents: %s\n%s", + exc, + "\n".join(file_paths) ) continue else: @@ -489,7 +495,11 @@ def upload_urls(self, url_list, handle_errors=False, **kwargs): # Grouper will put None's on the end of the last group url_group = [url for url in url_group if url is not None] - logger.info("Uploading group {}: {}".format(i + 1, "\n".join(url_group))) + logger.info( + "Uploading group %d: %s", + i + 1, + "\n".join(url_group) + ) # Create the documents logger.info("Creating the documents...") @@ -510,9 +520,9 @@ def upload_urls(self, url_list, handle_errors=False, **kwargs): except (APIError, RequestException) as exc: if handle_errors: logger.info( - "Error creating the following documents: " - + str(exc) - + "\n".join(url_group) + "Error creating the following documents: %s\n%s", + str(exc), + "\n".join(url_group) ) continue else: