Use database instead of config file

This commit is contained in:
2022-12-16 14:36:44 +01:00
parent ad61275724
commit ee3ce2016c
6 changed files with 191 additions and 85 deletions

View File

@ -17,40 +17,12 @@ import os
from platformdirs import user_data_dir
from reader import Reader, make_reader
from tomlkit import comment, document, parse, table
from tomlkit.items import Table
from tomlkit.toml_document import TOMLDocument
logging.basicConfig(level=logging.DEBUG, format="[%(asctime)s] [%(funcName)s:%(lineno)d] %(message)s")
data_dir: str = user_data_dir(appname="discord_rss_bot", appauthor="TheLovinator", roaming=True)
os.makedirs(data_dir, exist_ok=True)
def create_settings_file(settings_file_location) -> None:
"""Create the settings file if it doesn't exist.
Args:
settings_file_location: The location of the settings file.
Returns:
None
"""
webhooks: Table = table()
webhooks.add(comment('"First webhook" = "https://discord.com/api/webhooks/1234567890/abcdefghijklmnopqrstuvwxyz"'))
webhooks.add(comment('"Second webhook" = "https://discord.com/api/webhooks/1234567890/abcdefghijklmnopqrstuvwxyz"'))
database: Table = table()
database.add(comment('"location" = "/path/to/database/file"'))
doc: TOMLDocument = document()
doc.add("webhooks", webhooks)
doc.add("database", database)
# Write the settings file
with open(settings_file_location, "w", encoding="utf-8") as f:
f.write(doc.as_string())
def get_db_location(custom_location: str = "") -> str:
"""Where we store the database file.
@ -64,27 +36,6 @@ def get_db_location(custom_location: str = "") -> str:
return custom_location or os.path.join(data_dir, "db.sqlite")
def read_settings_file(custom_location: str = "") -> TOMLDocument:
"""Read the settings file and return the settings as a dict.
Args:
custom_location: The name of the settings file, defaults to settings.toml.
Returns:
dict: The settings file as a dict.
"""
# Use the custom location if it is provided.
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):
create_settings_file(settings_location)
# Read the settings file and return it as a dict.
with open(settings_location, encoding="utf-8") as f:
return parse(f.read())
def get_reader(custom_location: str = "") -> Reader:
"""Get the reader.