Move settings to settings.py and read from a config file

This commit is contained in:
2021-12-12 21:59:27 +01:00
parent c8bf725780
commit 203525becb
4 changed files with 51 additions and 8 deletions

View File

@ -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()

View File

@ -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")

4
poetry.lock generated
View File

@ -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 = [

View File

@ -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"