Skip to content

Commit

Permalink
feat(app): allow log level configuration; format imports with ruff; u…
Browse files Browse the repository at this point in the history
…se Typer for CLI argument processing
  • Loading branch information
hwittenborn committed Jul 23, 2024
1 parent b96a958 commit 63cfe0d
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 22 deletions.
1 change: 0 additions & 1 deletion citadel/globals.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

# Logger
logging.basicConfig(
level=logging.INFO,
format="%(message)s",
datefmt="[%X]",
handlers=[RichHandler(markup=True)],
Expand Down
52 changes: 33 additions & 19 deletions citadel/main.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
import os
import sys
import logging
from enum import Enum
from typing import Annotated

import discord
import typer
from discord import app_commands
from dotenv import load_dotenv

from citadel import commands
from citadel.globals import LOGGER

APP = typer.Typer()
GUILD_ID = discord.Object(id=1262512524541296640)


class LogLevel(str, Enum):
"""The log level used for the application."""

DEBUG = "DEBUG"
INFO = "INFO"
WARNING = "WARNING"
ERROR = "ERROR"
CRITICAL = "CRITICAL"


class CitadelClient(discord.Client):
"""The Citadel Discord Client."""

Expand All @@ -27,28 +40,29 @@ async def setup_hook(self) -> None:
await self.tree.sync(guild=GUILD_ID)


def main() -> int:
"""Program entrypoint."""
load_dotenv()
token = os.environ.get("CITADEL_DISCORD_TOKEN")

if not token:
LOGGER.error(
"No Discord client secret was detected. Set the [green]`CITADEL_DISCORD_TOKEN`[/] environment variable to "
"the secret's value.",
)
LOGGER.error(
"If needed, the variable can also be set in a file named [green]`.env`[/] in the current directory.",
)
return os.EX_NOINPUT
@APP.command()
def main(
discord_token: Annotated[str, typer.Argument(envvar="CITADEL_DISCORD_TOKEN")],
log_level: Annotated[LogLevel, typer.Argument(case_sensitive=False, envvar="LOG_LEVEL")] = LogLevel.INFO,
) -> None:
"""Citadel Discord bot.
Environment variables can be set via the command-line, or in a file named `.env`.
"""
LOGGER.setLevel(logging.getLevelName(log_level.value))

# Set up the client, add commands, and start.
client = CitadelClient()
client.tree.add_command(commands.hello)
client.run(token)

return os.EX_OK
client.run(discord_token)


def entrypoint() -> None:
"""Entrypoint of the application, used when running `./main.py` and for the Poetry config."""
load_dotenv()
APP()


if __name__ == "__main__":
sys.exit(main())
entrypoint()
55 changes: 54 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ rich = "^13.7.1"
mypy = "^1.11.0"
python-dotenv = "^1.0.1"
discord-py = "^2.4.0"
typer = "^0.12.3"

[tool.poetry.dev-dependencies]
ruff = "~0.5.0"
pre-commit = "~3.7.1"

[tool.poetry.scripts]
citadel = "citadel.main:main"
citadel = "citadel.main:entrypoint"

[tool.ruff]
line-length = 119
Expand Down Expand Up @@ -49,3 +50,4 @@ ignore = [
"ANN101",
"ANN102",
]
extend-select = ["I"]

0 comments on commit 63cfe0d

Please sign in to comment.