Add type hints

This commit is contained in:
2022-12-05 19:12:23 +01:00
parent 849cb17e95
commit 4c5faa2181
3 changed files with 46 additions and 40 deletions

View File

@ -21,14 +21,16 @@ import os
from pathlib import Path
from platformdirs import user_data_dir
from reader import make_reader
from tomlkit import TOMLDocument, comment, document, parse, table
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")
logger = logging.getLogger(__name__)
logger: logging.Logger = logging.getLogger(__name__)
# For get_data_dir()
data_directory = user_data_dir(appname="discord_rss_bot", appauthor="TheLovinator", roaming=True)
data_directory: str = user_data_dir(appname="discord_rss_bot", appauthor="TheLovinator", roaming=True)
def create_settings_file(settings_file) -> None:
@ -37,18 +39,16 @@ def create_settings_file(settings_file) -> None:
# [webhooks]
# Both options are commented out by default.
webhooks = table()
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"')
)
webhooks.add(comment('"Second webhook" = "https://discord.com/api/webhooks/1234567890/abcdefghijklmnopqrstuvwxyz"'))
# [database]
# Option is commented out by default.
database = table()
database: Table = table()
database.add(comment('"location" = "/path/to/database/file"'))
doc = document()
doc: TOMLDocument = document()
doc.add("webhooks", webhooks)
doc.add("database", database)
@ -74,7 +74,7 @@ def get_data_dir(data_dir: str = data_directory) -> Path:
logger.info(f"Using custom data directory: {data_dir}")
# Use the environment variable if it exists instead of the default app dir.
data_dir = os.getenv("DATA_DIR") or data_dir
data_dir: str = os.getenv("DATA_DIR") or data_dir
logger.debug(f"Data directory: {data_dir}")
@ -97,11 +97,11 @@ def get_db_file(custom_db_name: str = "db.sqlite") -> Path:
logger.info(f"Using custom database file: {custom_db_name}")
# Store the database file in the data directory
data_dir = get_data_dir()
data_dir: Path = get_data_dir()
db_location: Path = Path(os.path.join(data_dir, custom_db_name))
# Use the environment variable if it exists instead of the default db name.
db_file = os.getenv("DATABASE_LOCATION") or db_location
db_file: str | Path = os.getenv("DATABASE_LOCATION") or db_location
logger.debug(f"Database file: {db_file}")
return Path(db_file)
@ -120,11 +120,11 @@ def read_settings_file(custom_settings_name: str = "settings.toml") -> TOMLDocum
logger.info(f"Using custom name for settings file: {custom_settings_name}")
# Store the database file in the data directory
data_dir = get_data_dir()
data_dir: Path = get_data_dir()
settings_file_location: Path = Path(os.path.join(data_dir, custom_settings_name))
# Use the environment variable if it exists instead of the default db name.
settings_file = os.getenv("SETTINGS_FILE_LOCATION") or settings_file_location
settings_file: str | Path = os.getenv("SETTINGS_FILE_LOCATION") or settings_file_location
logger.debug(f"Settings file: {settings_file}")
# Create the settings file if it doesn't exist
@ -132,10 +132,10 @@ def read_settings_file(custom_settings_name: str = "settings.toml") -> TOMLDocum
create_settings_file(settings_file)
with open(settings_file, encoding="utf-8") as f:
data = parse(f.read())
data: TOMLDocument = parse(f.read())
logger.debug(f"Contents of settings file: {data}")
return data
reader = make_reader(str(get_db_file()))
reader: Reader = make_reader(str(get_db_file()))