Add type hints and other small stuff

This commit is contained in:
2022-12-14 02:16:33 +01:00
parent 1fcb9ffbf8
commit 3dbe6ba79d
8 changed files with 65 additions and 55 deletions

View File

@ -30,7 +30,7 @@ from requests import Response
from discord_rss_bot.settings import get_reader
def send_to_discord(reader: Reader = None, feed=None, do_once=False) -> None:
def send_to_discord(custom_reader: Reader | None = None, feed=None, do_once=False) -> None:
"""
Send entries to Discord.
@ -45,8 +45,7 @@ def send_to_discord(reader: Reader = None, feed=None, do_once=False) -> None:
Response: The response from the webhook.
"""
# Get the default reader if we didn't get a custom one.
if reader is None:
reader = get_reader()
reader: Reader = get_reader() if custom_reader is None else custom_reader
# If we should get all entries, or just the entries from a specific feed.
if feed is None:

View File

@ -44,6 +44,7 @@ from reader import (
EntrySearchResult,
Feed,
FeedCounts,
Reader,
)
from starlette.templating import _TemplateResponse
from tomlkit.toml_document import TOMLDocument
@ -56,7 +57,7 @@ app: FastAPI = FastAPI()
app.mount("/static", StaticFiles(directory="discord_rss_bot/static"), name="static")
templates: Jinja2Templates = Jinja2Templates(directory="discord_rss_bot/templates")
reader = get_reader()
reader: Reader = get_reader()
def encode_url(url_to_quote: str) -> str:
@ -99,8 +100,8 @@ async def create_feed(feed_url: str = Form(), webhook_dropdown: str = Form()) ->
reader.set_entry_read(entry, True)
settings: TOMLDocument = read_settings_file()
webhook_url: str = str(settings["webhooks"][webhook_dropdown])
reader.set_tag(clean_feed_url, "webhook", webhook_url)
webhook_url: str = str(settings["webhooks"][webhook_dropdown]) # type: ignore
reader.set_tag(clean_feed_url, "webhook", webhook_url) # type: ignore
reader.get_tag(clean_feed_url, "webhook")
reader.update_search()
@ -112,8 +113,9 @@ def create_list_of_webhooks() -> list[dict[str, str]]:
"""List with webhooks."""
settings: TOMLDocument = read_settings_file()
list_of_webhooks: list[dict[str, str]] = []
for hook in settings["webhooks"]:
list_of_webhooks.append({"name": hook, "url": settings["webhooks"][hook]})
for hook in settings["webhooks"]: # type: ignore
list_of_webhooks.append({"name": hook, "url": settings["webhooks"][hook]}) # type: ignore
return list_of_webhooks
@ -230,7 +232,7 @@ async def remove_feed(feed_url: str = Form()) -> RedirectResponse:
reader.delete_feed(feed_url)
reader.update_search()
return RedirectResponse(url=f"/", status_code=303)
return RedirectResponse(url="/", status_code=303)
@app.get("/search", response_class=HTMLResponse)

View File

@ -6,7 +6,9 @@ from reader import EntrySearchResult, Feed, HighlightedString, Reader
from discord_rss_bot.settings import get_reader
def create_html_for_search_results(search_results: Iterable[EntrySearchResult], reader: Reader = None) -> str:
def create_html_for_search_results(
search_results: Iterable[EntrySearchResult], custom_reader: Reader | None = None
) -> str:
"""Create HTML for the search results.
Args:
@ -20,8 +22,7 @@ def create_html_for_search_results(search_results: Iterable[EntrySearchResult],
# TODO: We should also add <span> tags to the title.
# Get the default reader if we didn't get a custom one.
if reader is None:
reader = get_reader()
reader: Reader = get_reader() if custom_reader is None else custom_reader
html: str = ""
for result in search_results:

View File

@ -47,7 +47,7 @@ def create_settings_file(settings_file_location) -> None:
doc.add("database", database)
# Write the settings file
with open(settings_file_location, "w") as f:
with open(settings_file_location, "w", encoding="utf-8") as f:
f.write(doc.as_string())
@ -61,10 +61,7 @@ def get_db_location(custom_location: str = "") -> str:
The database location.
"""
# Use the custom location if it is provided.
if custom_location:
return custom_location
else:
return os.path.join(data_dir, "db.sqlite")
return custom_location or os.path.join(data_dir, "db.sqlite")
def read_settings_file(custom_location: str = "") -> TOMLDocument:
@ -77,10 +74,7 @@ def read_settings_file(custom_location: str = "") -> TOMLDocument:
dict: The settings file as a dict.
"""
# Use the custom location if it is provided.
if custom_location:
settings_location = custom_location
else:
settings_location = os.path.join(data_dir, "settings.toml")
settings_location: str = custom_location or os.path.join(data_dir, "settings.toml")
# Create the settings file if it doesn't exist.
if not os.path.exists(settings_location):