From b2efaf340e3eb528cadc218afac2e793f28e3696 Mon Sep 17 00:00:00 2001 From: Flare Starfall Date: Fri, 19 Sep 2025 10:36:35 +0200 Subject: [PATCH] Create requirements, config-sample, and config reader --- .gitignore | 2 ++ app.py | 7 +++++++ config-sample.toml | 16 ++++++++++++++++ requirements.txt | 4 ++++ starfall/config.py | 29 +++++++++++++++++++++++++++++ 5 files changed, 58 insertions(+) create mode 100644 app.py create mode 100644 config-sample.toml create mode 100644 requirements.txt create mode 100644 starfall/config.py diff --git a/.gitignore b/.gitignore index aa5d4b2..b006529 100644 --- a/.gitignore +++ b/.gitignore @@ -174,3 +174,5 @@ cython_debug/ # option (not recommended) you can uncomment the following to ignore the entire idea folder. #.idea/ +# ---> StarfallBot +config.toml \ No newline at end of file diff --git a/app.py b/app.py new file mode 100644 index 0000000..089f7aa --- /dev/null +++ b/app.py @@ -0,0 +1,7 @@ +from starfall.config import Config + +if __name__ == "__main__": + try: + Config.load() + except FileNotFoundError: + pass diff --git a/config-sample.toml b/config-sample.toml new file mode 100644 index 0000000..302e007 --- /dev/null +++ b/config-sample.toml @@ -0,0 +1,16 @@ +# == StarfallBot configuration file == + +[web] +# What port the web server uses. +port = 5000 +# Connection database url. +# sqlite is only production safe to a point - consult the SQLAlchemy +# documentation to see how to set up other databases. +database_url = "sqlite:///starfallbot.db" + +[discord] +token = "YOUR BOT TOKEN HERE" + +[twitch] +oauth_token = "oauth:TWITCH OAUTH TOKEN" +channel = "TWITCH CHANNEL NAME" \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..df0de29 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +flask +flask-sqlalchemy +discord-py-interactions +twitchio \ No newline at end of file diff --git a/starfall/config.py b/starfall/config.py new file mode 100644 index 0000000..74e4b5f --- /dev/null +++ b/starfall/config.py @@ -0,0 +1,29 @@ +import os +import tomllib +from typing import Annotated, Optional + + +class Config: + data: Annotated[Optional[dict], + "Configuration imported from config.toml"] = None + + @classmethod + def load(cls): + """Loads the contents of the config.toml file into the class. + + Raises: + FileNotFoundError: If the file does not exist. + """ + if not cls.file_exists(): + raise FileNotFoundError("config.toml does not exist") + with open("config.toml", "r") as f: + cls.data = tomllib.load(f) + + @classmethod + def file_exists(cls) -> bool: + """Whether the config.toml file exists or needs to be created. + + Returns: + bool: True if path exists and is a file, False otherwise. + """ + return os.path.exists("config.toml") and os.path.isfile("config.toml")