Skip to content

Commit

Permalink
Fixed r! and lazy formatting for logs
Browse files Browse the repository at this point in the history
  • Loading branch information
duckduckgrayduck committed Dec 2, 2023
1 parent 0a79cbc commit 287e29d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
2 changes: 1 addition & 1 deletion documentcloud/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def __init__(
]

def __repr__(self):
return f"<APIResults: {self.results}>" # pragma: no cover
return f"<APIResults: {self.results!r}>" # pragma: no cover

def __str__(self):
return f"[{', '.join(str(r) for r in self.results)}]"
Expand Down
4 changes: 2 additions & 2 deletions documentcloud/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
Expand Down
34 changes: 22 additions & 12 deletions documentcloud/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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...")
Expand All @@ -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:
Expand All @@ -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()
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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...")
Expand All @@ -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:
Expand Down

0 comments on commit 287e29d

Please sign in to comment.