Skip to content

Commit ca478e4

Browse files
authored
improve(logs): enhance some logs and type hints (#293)
2 parents 6e0e6eb + 83cba75 commit ca478e4

File tree

1 file changed

+31
-27
lines changed

1 file changed

+31
-27
lines changed

mkdocs_rss_plugin/util.py

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
# 3rd party
1818
import markdown
1919
from git import GitCommandError, GitCommandNotFound, InvalidGitRepositoryError, Repo
20-
from mkdocs.config.config_options import Config
20+
from mkdocs.config.defaults import MkDocsConfig
2121
from mkdocs.plugins import get_plugin_logger
2222
from mkdocs.structure.pages import Page
2323
from mkdocs.utils import get_build_datetime
@@ -162,11 +162,11 @@ def get_value_from_dot_key(self, data: dict, dot_key: str | bool) -> Any:
162162
def get_file_dates(
163163
self,
164164
in_page: Page,
165-
source_date_creation: str = "git",
166-
source_date_update: str = "git",
167-
meta_datetime_format: str = "%Y-%m-%d %H:%M",
168-
meta_default_timezone: str = "UTC",
169-
meta_default_time: datetime | None = None,
165+
source_date_creation: str,
166+
source_date_update: str,
167+
meta_datetime_format: str,
168+
meta_default_timezone: str,
169+
meta_default_time: datetime,
170170
) -> tuple[datetime, datetime]:
171171
"""Extract creation and update dates from page metadata (yaml frontmatter) or
172172
git log for given file.
@@ -189,6 +189,7 @@ def get_file_dates(
189189
:return: tuple of timestamps (creation date, last commit date)
190190
:rtype: Tuple[datetime, datetime]
191191
"""
192+
logger.debug(f"Extracting dates for {in_page.file.src_uri}")
192193
# empty vars
193194
dt_created = dt_updated = None
194195
if meta_default_time is None:
@@ -233,13 +234,13 @@ def get_file_dates(
233234
)
234235

235236
if isinstance(dt_updated, str):
236-
logger.info(
237-
f"Update date of {in_page.file.abs_src_path} is an "
238-
f"a character string: {dt_updated} ({type(dt_updated)})"
237+
logger.debug(
238+
f"Update date of {in_page.file.abs_src_path} is a "
239+
f"character string: {dt_updated} ({type(dt_updated)})"
239240
)
240241

241242
elif dt_updated is None:
242-
logger.info(
243+
logger.debug(
243244
f"Update date of {in_page.file.abs_src_path} is an "
244245
f"unrecognized type: {dt_updated} ({type(dt_updated)})"
245246
)
@@ -287,8 +288,6 @@ def get_file_dates(
287288
dt_updated = set_datetime_zoneinfo(
288289
datetime.fromtimestamp(float(dt_updated)), meta_default_timezone
289290
)
290-
else:
291-
pass
292291

293292
# results
294293
if all([dt_created, dt_updated]):
@@ -297,25 +296,31 @@ def get_file_dates(
297296
dt_updated,
298297
)
299298
elif dt_created:
300-
logger.info(
301-
f"Updated date could not be retrieved for page: "
302-
f"{in_page.file.abs_src_path}. Maybe it has never been committed yet?"
299+
log_msg = (
300+
"Updated date could not be retrieved for page: "
301+
f"{in_page.file.abs_src_path}. Fallback to build date."
303302
)
303+
if self.use_git:
304+
log_msg += "Maybe it has never been committed yet?"
305+
logger.debug(log_msg)
304306
return (
305307
dt_created,
306308
get_build_datetime(),
307309
)
308310
elif dt_updated:
309-
logger.info(
310-
f"Creation date could not be retrieved for page: "
311-
f"{in_page.file.abs_src_path}. Maybe it has never been committed yet?"
311+
log_msg = (
312+
"Creation date could not be retrieved for page: "
313+
f"{in_page.file.abs_src_path}. Fallback to build date."
312314
)
315+
if self.use_git:
316+
log_msg += "Maybe it has never been committed yet?"
317+
logger.debug(log_msg)
313318
return (
314319
get_build_datetime(),
315320
dt_updated,
316321
)
317322
else:
318-
logging.warning(
323+
logger.info(
319324
f"Dates could not be retrieved for page: {in_page.file.abs_src_path}."
320325
)
321326
return (
@@ -363,7 +368,7 @@ def get_authors_from_meta(self, in_page: Page) -> tuple[str] | None:
363368

364369
def get_categories_from_meta(
365370
self, in_page: Page, categories_labels: Iterable
366-
) -> tuple:
371+
) -> list | None:
367372
"""Returns category from page meta.
368373
369374
:param in_page: input page to parse
@@ -372,7 +377,7 @@ def get_categories_from_meta(
372377
:type categories_labels: Iterable
373378
374379
:return: found categories
375-
:rtype: tuple
380+
:rtype: list
376381
"""
377382
if not categories_labels:
378383
return None
@@ -384,8 +389,6 @@ def get_categories_from_meta(
384389
output_categories.extend(in_page.meta.get(category_label))
385390
elif isinstance(in_page.meta.get(category_label), str):
386391
output_categories.append(in_page.meta.get(category_label))
387-
else:
388-
pass
389392
else:
390393
continue
391394
return sorted(output_categories)
@@ -426,7 +429,7 @@ def get_date_from_meta(
426429
date=date_metatag_value, time=meta_default_time.time()
427430
)
428431
else:
429-
logger.info(
432+
logger.debug(
430433
f"Incompatible date type: {type(date_metatag_value)}. It must be: "
431434
"date, datetime or str (complying with defined strftime format)."
432435
)
@@ -551,6 +554,7 @@ def get_image(self, in_page: Page, base_url: str) -> tuple[str, str, int] | None
551554
)
552555

553556
if img_local_path.is_file():
557+
logger.debug("Local image already exists. Using it to get its length.")
554558
img_length = img_local_path.stat().st_size
555559
else:
556560
logger.debug(
@@ -585,7 +589,7 @@ def get_image(self, in_page: Page, base_url: str) -> tuple[str, str, int] | None
585589
# return final tuple
586590
return (img_url, mime_type, img_length)
587591

588-
def get_local_image_length(self, page_path: str, path_to_append: str) -> int:
592+
def get_local_image_length(self, page_path: str, path_to_append: str) -> int | None:
589593
"""Calculates local image size in octets.
590594
591595
Args:
@@ -653,7 +657,7 @@ def get_remote_image_length(
653657
return int(img_length)
654658

655659
@staticmethod
656-
def get_site_url(mkdocs_config: Config) -> str | None:
660+
def get_site_url(mkdocs_config: MkDocsConfig) -> str | None:
657661
"""Extract site URL from MkDocs configuration and enforce the behavior to ensure \
658662
returning a str with length > 0 or None. If exists, it adds an ending slash.
659663
@@ -679,7 +683,7 @@ def get_site_url(mkdocs_config: Config) -> str | None:
679683

680684
return site_url
681685

682-
def guess_locale(self, mkdocs_config: Config) -> str | None:
686+
def guess_locale(self, mkdocs_config: MkDocsConfig) -> str | None:
683687
"""Extract language code from MkDocs or Theme configuration.
684688
685689
:param mkdocs_config: configuration object

0 commit comments

Comments
 (0)