Skip to content

Commit 8fd6f74

Browse files
committed
update(chore): use typed dataclasses instead of dict
1 parent 72a2de7 commit 8fd6f74

File tree

2 files changed

+64
-38
lines changed

2 files changed

+64
-38
lines changed

mkdocs_rss_plugin/models.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,21 @@
55
# ##################################
66

77
# standard
8+
from dataclasses import dataclass, field
89
from datetime import datetime
910
from pathlib import Path
10-
from typing import NamedTuple, Optional
11+
from typing import Optional
1112

13+
# package modules
14+
from mkdocs_rss_plugin.__about__ import __title__, __version__
1215

1316
# ############################################################################
1417
# ########## Classes ###############
1518
# ##################################
16-
class PageInformation(NamedTuple):
19+
20+
21+
@dataclass
22+
class PageInformation:
1723
"""Data type to set and get page information in order to produce the RSS feed."""
1824

1925
abs_path: Optional[Path] = None
@@ -27,3 +33,22 @@ class PageInformation(NamedTuple):
2733
updated: Optional[datetime] = None
2834
url_comments: Optional[str] = None
2935
url_full: Optional[str] = None
36+
37+
38+
@dataclass
39+
class RssFeedBase:
40+
author: Optional[str] = None
41+
buildDate: Optional[str] = None
42+
copyright: Optional[str] = None
43+
description: Optional[str] = None
44+
entries: list[PageInformation] = field(default_factory=list)
45+
generator: str = f"{__title__} - v{__version__}"
46+
html_url: Optional[str] = None
47+
json_url: Optional[str] = None
48+
language: Optional[str] = None
49+
logo_url: Optional[str] = None
50+
pubDate: Optional[str] = None
51+
repo_url: Optional[str] = None
52+
rss_url: Optional[str] = None
53+
title: Optional[str] = None
54+
ttl: Optional[int] = None

mkdocs_rss_plugin/plugin.py

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
# standard library
88
import json
99
from copy import deepcopy
10+
from dataclasses import asdict
1011
from datetime import datetime
1112
from email.utils import formatdate
1213
from pathlib import Path
@@ -33,7 +34,7 @@
3334
from mkdocs_rss_plugin.integrations.theme_material_social_plugin import (
3435
IntegrationMaterialSocialCards,
3536
)
36-
from mkdocs_rss_plugin.models import PageInformation
37+
from mkdocs_rss_plugin.models import PageInformation, RssFeedBase
3738
from mkdocs_rss_plugin.util import Util
3839

3940
# ############################################################################
@@ -81,8 +82,8 @@ def on_startup(
8182

8283
self.pages_to_filter: list[PageInformation] = []
8384
# prepare output feeds
84-
self.feed_created: dict = {}
85-
self.feed_updated: dict = {}
85+
self.feed_created: RssFeedBase = RssFeedBase()
86+
self.feed_updated: RssFeedBase = RssFeedBase()
8687

8788
def on_config(self, config: MkDocsConfig) -> MkDocsConfig:
8889
"""The config event is the first event called on build and
@@ -139,30 +140,30 @@ def on_config(self, config: MkDocsConfig) -> MkDocsConfig:
139140
self.tpl_folder = DEFAULT_TEMPLATE_FOLDER
140141

141142
# start a feed dictionary using global config vars
142-
base_feed = {
143-
"author": config.site_author or None,
144-
"buildDate": formatdate(get_build_timestamp()),
145-
"copyright": config.copyright,
146-
"description": (
143+
base_feed = RssFeedBase(
144+
author=config.site_author or None,
145+
buildDate=formatdate(get_build_timestamp()),
146+
copyright=config.copyright,
147+
description=(
147148
self.config.feed_description
148149
if self.config.feed_description
149150
else config.site_description
150151
),
151-
"entries": [],
152-
"generator": f"{__title__} - v{__version__}",
153-
"html_url": self.util.get_site_url(mkdocs_config=config),
154-
"language": self.util.guess_locale(mkdocs_config=config),
155-
"pubDate": formatdate(get_build_timestamp()),
156-
"repo_url": config.repo_url,
157-
"title": (
152+
entries=[],
153+
generator=f"{__title__} - v{__version__}",
154+
html_url=self.util.get_site_url(mkdocs_config=config),
155+
language=self.util.guess_locale(mkdocs_config=config),
156+
pubDate=formatdate(get_build_timestamp()),
157+
repo_url=config.repo_url,
158+
title=(
158159
self.config.feed_title if self.config.feed_title else config.site_name
159160
),
160-
"ttl": self.config.feed_ttl,
161-
}
161+
ttl=self.config.feed_ttl,
162+
)
162163

163164
# feed image
164165
if self.config.image:
165-
base_feed["logo_url"] = self.config.image
166+
base_feed.logo_url = self.config.image
166167

167168
# pattern to match pages included in output
168169
self.match_path_pattern = re_compile(self.config.match_path)
@@ -224,29 +225,29 @@ def on_config(self, config: MkDocsConfig) -> MkDocsConfig:
224225
self.feed_updated = deepcopy(base_feed)
225226

226227
# final feed url
227-
if base_feed.get("html_url"):
228+
if base_feed.html_url:
228229
# concatenate both URLs
229-
self.feed_created["rss_url"] = (
230-
base_feed.get("html_url") + self.config.feeds_filenames.rss_created
230+
self.feed_created.rss_url = (
231+
base_feed.html_url + self.config.feeds_filenames.rss_created
231232
)
232-
self.feed_updated["rss_url"] = (
233-
base_feed.get("html_url") + self.config.feeds_filenames.rss_updated
233+
self.feed_updated.rss_url = (
234+
base_feed.html_url + self.config.feeds_filenames.rss_updated
234235
)
235-
self.feed_created["json_url"] = (
236-
base_feed.get("html_url") + self.config.feeds_filenames.json_created
236+
self.feed_created.json_url = (
237+
base_feed.html_url + self.config.feeds_filenames.json_created
237238
)
238-
self.feed_updated["json_url"] = (
239-
base_feed.get("html_url") + self.config.feeds_filenames.json_updated
239+
self.feed_updated.json_url = (
240+
base_feed.html_url + self.config.feeds_filenames.json_updated
240241
)
241242
else:
242243
logger.error(
243244
"The variable `site_url` is not set in the MkDocs "
244245
"configuration file whereas a URL is mandatory to publish. "
245246
"See: https://validator.w3.org/feed/docs/rss2.html#requiredChannelElements"
246247
)
247-
self.feed_created["rss_url"] = self.feed_updated["json_url"] = (
248-
self.feed_updated["rss_url"]
249-
) = self.feed_updated["json_url"] = None
248+
self.feed_created.rss_url = self.feed_updated.json_url = (
249+
self.feed_updated.rss_url
250+
) = self.feed_updated.json_url = None
250251

251252
# ending event
252253
return config
@@ -371,7 +372,7 @@ def on_post_build(self, config: config_options.Config) -> None:
371372
)
372373

373374
# created items
374-
self.feed_created.get("entries").extend(
375+
self.feed_created.entries.extend(
375376
self.util.filter_pages(
376377
pages=self.pages_to_filter,
377378
attribute="created",
@@ -380,7 +381,7 @@ def on_post_build(self, config: config_options.Config) -> None:
380381
)
381382

382383
# updated items
383-
self.feed_updated.get("entries").extend(
384+
self.feed_updated.entries.extend(
384385
self.util.filter_pages(
385386
pages=self.pages_to_filter,
386387
attribute="updated",
@@ -419,7 +420,7 @@ def on_post_build(self, config: config_options.Config) -> None:
419420
# write feeds to files stripping out spaces and new lines
420421
with out_feed_created.open(mode="w", encoding="UTF8") as fifeed_created:
421422
prev_char = ""
422-
for char in template.render(feed=self.feed_created):
423+
for char in template.render(feed=asdict(self.feed_created)):
423424
if char == "\n":
424425
continue
425426
if char == " " and prev_char == " ":
@@ -429,7 +430,7 @@ def on_post_build(self, config: config_options.Config) -> None:
429430
fifeed_created.write(char)
430431

431432
with out_feed_updated.open(mode="w", encoding="UTF8") as fifeed_updated:
432-
for char in template.render(feed=self.feed_updated):
433+
for char in template.render(feed=asdict(self.feed_updated)):
433434
if char == "\n":
434435
prev_char = char
435436
continue
@@ -443,14 +444,14 @@ def on_post_build(self, config: config_options.Config) -> None:
443444
if self.config.json_feed_enabled:
444445
with out_json_created.open(mode="w", encoding="UTF8") as fp:
445446
json.dump(
446-
self.util.feed_to_json(self.feed_created),
447+
self.util.feed_to_json(asdict(self.feed_created)),
447448
fp,
448449
indent=4 if self.config.pretty_print else None,
449450
)
450451

451452
with out_json_updated.open(mode="w", encoding="UTF8") as fp:
452453
json.dump(
453-
self.util.feed_to_json(self.feed_updated, updated=True),
454+
self.util.feed_to_json(asdict(self.feed_updated), updated=True),
454455
fp,
455456
indent=4 if self.config.pretty_print else None,
456457
)

0 commit comments

Comments
 (0)