From 203525becbbe4e485d1d9053fde85d0746d5e6d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Hells=C3=A9n?= Date: Sun, 12 Dec 2021 21:59:27 +0100 Subject: [PATCH] Move settings to settings.py and read from a config file --- discord_rss_bot/main.py | 10 ++++----- discord_rss_bot/settings.py | 44 +++++++++++++++++++++++++++++++++++++ poetry.lock | 4 ++-- pyproject.toml | 1 + 4 files changed, 51 insertions(+), 8 deletions(-) create mode 100644 discord_rss_bot/settings.py diff --git a/discord_rss_bot/main.py b/discord_rss_bot/main.py index d051645..7c52175 100644 --- a/discord_rss_bot/main.py +++ b/discord_rss_bot/main.py @@ -1,14 +1,12 @@ -import os -import sys - import typer from dhooks import Webhook from reader import FeedExistsError, make_reader -reader = make_reader("db.sqlite") +from discord_rss_bot.settings import Settings -app = typer.Typer() -hook = Webhook("") +app = typer.Typer() # For CLI (https://typer.tiangolo.com/) +hook = Webhook(Settings.webhook_url) # For Webhooks (https://github.com/kyb3r/dhooks) +reader = make_reader(Settings.db_file) # For RSS (https://github.com/lemon24/reader) @app.command() diff --git a/discord_rss_bot/settings.py b/discord_rss_bot/settings.py new file mode 100644 index 0000000..aaec826 --- /dev/null +++ b/discord_rss_bot/settings.py @@ -0,0 +1,44 @@ +import configparser +import os +import sys + +from platformdirs import user_data_dir + + +class Settings: + data_dir = user_data_dir( + appname="discord-rss-bot", # The name of application. + appauthor="TheLovinator", # The name of the app author or distributing body for this application + roaming=True, # Whether to use the roaming appdata directory on Windows + ) + + # Create the data directory if it doesn't exist + os.makedirs(data_dir, exist_ok=True) + + # Store the database file in the data directory + db_file = os.path.join(data_dir, "db.sqlite") + + # Store the config in the data directory + config_location = os.path.join(data_dir, "config.conf") + + if not os.path.isfile(config_location): + # TODO: Add config for db_file and config_location + print("No config file found, creating one...") + with open(config_location, "w") as config_file: + config = configparser.ConfigParser() + config.add_section("config") + config.set( + "config", + "webhook_url", + "https://discord.com/api/webhooks/1234/567890/ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz", + ) + + config.write(config_file) + sys.exit(f"Please edit the config file at {config_location}") + + # Read the config file + config = configparser.ConfigParser() + config.read(config_location) + + # Get the webhook url from the config file + webhook_url = config.get("config", "webhook_url") diff --git a/poetry.lock b/poetry.lock index c5f79fd..628b3e1 100644 --- a/poetry.lock +++ b/poetry.lock @@ -323,7 +323,7 @@ python-versions = ">=2.6" name = "platformdirs" version = "2.4.0" description = "A small Python module for determining appropriate platform-specific dirs, e.g. a \"user data dir\"." -category = "dev" +category = "main" optional = false python-versions = ">=3.6" @@ -560,7 +560,7 @@ multidict = ">=4.0" [metadata] lock-version = "1.1" python-versions = "^3.9" -content-hash = "dc5257dac12ba191e440acf7ab8ee7ce0f0f4b6ad421f0a8f7b618544f7c616a" +content-hash = "7c241013c8d3fb868ab328cb15e7e1a20908ffae5f455d90286135f89170dcfa" [metadata.files] aiohttp = [ diff --git a/pyproject.toml b/pyproject.toml index 6ee3ea7..1be471b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -9,6 +9,7 @@ python = "^3.9" reader = "^2.6" typer = "^0.4.0" dhooks = "^1.1.4" +platformdirs = "^2.4.0" [tool.poetry.dev-dependencies] pytest = "^5.2"