Skip to content

Commit

Permalink
switch to uv, fix typing issues
Browse files Browse the repository at this point in the history
  • Loading branch information
AiroPi committed Aug 31, 2024
1 parent c4bd67b commit 5e95d0b
Show file tree
Hide file tree
Showing 16 changed files with 847 additions and 140 deletions.
20 changes: 11 additions & 9 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
# syntax=docker/dockerfile-upstream:master-labs

FROM python:3.12.0-alpine as build
COPY --from=ghcr.io/astral-sh/uv:latest /uv /bin/uv
WORKDIR /app
RUN python -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
# RUN python -m venv /opt/venv
# ENV PATH="/opt/venv/bin:$PATH"
COPY ./resources ./
RUN --mount=type=cache,target=/var/cache/apk/ \
--mount=type=cache,target=/root/.cache/pip \
--mount=type=bind,source=requirements.txt,target=requirements.txt \
--mount=type=cache,target=/root/.cache/uv \
--mount=type=bind,source=uv.lock,target=uv.lock \
--mount=type=bind,source=pyproject.toml,target=pyproject.toml \
--mount=type=bind,source=./bin/msgfmt.py,target=./msgfmt.py \
: \
&& apk add gcc musl-dev linux-headers \
&& pip install -U pip \
&& pip install -U -r requirements.txt \
&& uv sync --no-dev --locked \
&& python ./msgfmt.py ./locale/**/LC_MESSAGES/*.po \
&& :

FROM python:3.12.0-alpine as base
# https://docs.docker.com/reference/dockerfile/#copy---parents
COPY --parents --from=build /opt/venv /app/locale/**/LC_MESSAGES/*.mo /
COPY --parents --from=build /app/.venv /app/locale/**/LC_MESSAGES/*.mo /
WORKDIR /app
COPY ./src ./
COPY --parents ./alembic.ini ./alembic ./
ENV PATH="/opt/venv/bin:$PATH"
ENV PATH="/app/.venv/bin:$PATH"
ENV PYTHONUNBUFFERED=0


Expand All @@ -31,7 +32,8 @@ CMD ["/bin/sh", "-c", "alembic upgrade head && python ./main.py run --sync -c ./


FROM base as debug
COPY --from=ghcr.io/astral-sh/uv:latest /uv /bin/uv
ENV DEBUG=1
ENV LOG_LEVEL=DEBUG
RUN pip install debugpy
RUN uv pip install debugpy
CMD ["/bin/sh", "-c", "alembic upgrade head && python -Xfrozen_modules=off -m debugpy --wait-for-client --listen 0.0.0.0:5678 ./main.py run -c ./config.toml"]
5 changes: 5 additions & 0 deletions compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ version: '3'

services:
mybot:
hostname: mybot
init: true
image: airopi/mybot:stable
build:
Expand All @@ -23,6 +24,10 @@ services:
condition: service_healthy
volumes:
- ./config.toml:/app/config.toml
expose:
- 8080
ports:
- 8080:8080

database:
hostname: database
Expand Down
23 changes: 23 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
[project]
name = "MyBot"
version = "2.0b3"
dependencies = [
"wheel",
"asyncpg",
"sqlalchemy[asyncio]",
"typing_extensions",
"discord.py",
"psutil",
"alembic",
"two048",
"topggpy==2.0.0a0",
"lingua-language-detector",
"aiohttp",
"python-dateutil",
"typer",
]
requires-python = ">=3.12"

[tool.uv]
dev-dependencies = ["tox", "pyright", "pip-tools", "debugpy", "ruff"]

[tool.tox]
legacy_tox_ini = """
[tox]
Expand Down
5 changes: 0 additions & 5 deletions requirements.dev.in

This file was deleted.

59 changes: 0 additions & 59 deletions requirements.dev.txt

This file was deleted.

13 changes: 0 additions & 13 deletions requirements.in

This file was deleted.

72 changes: 32 additions & 40 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,39 +1,35 @@
#
# This file is autogenerated by pip-compile with Python 3.12
# by the following command:
#
# pip-compile
#
aiohttp==3.9.3
# This file was autogenerated by uv via the following command:
# uv pip compile pyproject.toml -o requirements.txt
aiohappyeyeballs==2.4.0
# via aiohttp
aiohttp==3.10.5
# via
# -r requirements.in
# mybot (pyproject.toml)
# discord-py
# topggpy
aiosignal==1.3.1
# via aiohttp
alembic==1.13.1
# via -r requirements.in
alembic==1.13.2
# via mybot (pyproject.toml)
asyncpg==0.29.0
# via -r requirements.in
attrs==23.2.0
# via mybot (pyproject.toml)
attrs==24.2.0
# via aiohttp
click==8.1.7
# via typer
colorama==0.4.6
# via typer
discord-py==2.3.2
# via -r requirements.in
discord-py==2.4.0
# via mybot (pyproject.toml)
frozenlist==1.4.1
# via
# aiohttp
# aiosignal
greenlet==3.0.3
# via sqlalchemy
idna==3.6
idna==3.8
# via yarl
lingua-language-detector==1.3.4
# via -r requirements.in
mako==1.3.2
lingua-language-detector==2.0.2
# via mybot (pyproject.toml)
mako==1.3.5
# via alembic
markdown-it-py==3.0.0
# via rich
Expand All @@ -45,39 +41,35 @@ multidict==6.0.5
# via
# aiohttp
# yarl
numpy==1.26.4
# via lingua-language-detector
psutil==5.9.8
# via -r requirements.in
pygments==2.17.2
psutil==6.0.0
# via mybot (pyproject.toml)
pygments==2.18.0
# via rich
python-dateutil==2.9.0.post0
# via -r requirements.in
regex==2023.12.25
# via lingua-language-detector
rich==13.7.1
# via mybot (pyproject.toml)
rich==13.8.0
# via typer
shellingham==1.5.4
# via typer
six==1.16.0
# via python-dateutil
sqlalchemy[asyncio]==2.0.28
sqlalchemy==2.0.32
# via
# -r requirements.in
# mybot (pyproject.toml)
# alembic
topggpy==2.0.0a0
# via -r requirements.in
# via mybot (pyproject.toml)
two048==1.0.2
# via -r requirements.in
typer[all]==0.9.0
# via -r requirements.in
typing-extensions==4.10.0
# via mybot (pyproject.toml)
typer==0.12.5
# via mybot (pyproject.toml)
typing-extensions==4.12.2
# via
# -r requirements.in
# mybot (pyproject.toml)
# alembic
# sqlalchemy
# typer
wheel==0.43.0
# via -r requirements.in
yarl==1.9.4
wheel==0.44.0
# via mybot (pyproject.toml)
yarl==1.9.6
# via aiohttp
4 changes: 2 additions & 2 deletions src/cogs/game/minesweeper/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def check_playable(self) -> None:
self.play.disabled = False

@ui.select(
cls=ui.Select,
cls=ui.Select[Self],
placeholder="Select the row",
options=[],
)
Expand All @@ -94,7 +94,7 @@ async def row(self, inter: Interaction, value: ui.Select[Self]) -> None:
await inter.response.edit_message(view=self)

@ui.select(
cls=ui.Select,
cls=ui.Select[Self],
placeholder="Select the column",
options=[],
)
Expand Down
2 changes: 1 addition & 1 deletion src/cogs/poll/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ async def callback(self, inter: Interaction, poll_type: db.PollType) -> None:
creation_date=inter.created_at,
)

poll_menu_from_type = {
poll_menu_from_type: dict[db.PollType, type[PollModal]] = {
db.PollType.CHOICE: ChoicesPollModal,
db.PollType.BOOLEAN: PollModal,
db.PollType.OPINION: PollModal,
Expand Down
11 changes: 10 additions & 1 deletion src/cogs/poll/vote_menus.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ async def __init__(self, cog: PollCog, poll: db.Poll | None = None):
# _silent because this view is added to persistent views.
self.vote.label = _("Vote", _silent=True)

if poll is not None:
self.add_item(
ui.Button(
style=discord.ButtonStyle.url,
label=_("Results", _silent=True),
url=f"http://localhost:8000/poll/{poll.message_id}",
)
)

def get_current_votes(self, poll: db.Poll) -> dict[int, tuple[Interaction, ui.View]]:
return self.cog.current_votes.setdefault(poll.id, {})

Expand Down Expand Up @@ -98,7 +107,7 @@ async def vote(self, inter: discord.Interaction, button: ui.Button[Self]):
await current_votes[inter.user.id][0].delete_original_response()
current_votes[inter.user.id] = (inter, self)

vote_menu_types = {
vote_menu_types: dict[db.PollType, type[VoteMenu]] = {
db.PollType.CHOICE: ChoicePollVote,
db.PollType.BOOLEAN: BooleanPollVote,
db.PollType.OPINION: OpinionPollVote,
Expand Down
4 changes: 2 additions & 2 deletions src/core/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ def __getattribute__(self, name: str) -> Any:

def define_config(config_path: Path | str | None = None, **kwargs: Any):
if config_path:
with open(config_path, encoding="utf-8") as f:
kwargs |= tomllib.load(f.buffer)
with open(config_path, "rb", encoding="utf-8") as f:
kwargs |= tomllib.load(f)

Config(**kwargs) # it is a singleton, so it will directly affect the instance.
Config.set_as_defined()
Expand Down
2 changes: 1 addition & 1 deletion src/core/db/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import logging

from sqlalchemy import select as select
from sqlalchemy import Integer as Integer, String as String, cast as cast, func as func, select as select
from sqlalchemy.orm import selectinload as selectinload

from .tables import (
Expand Down
2 changes: 1 addition & 1 deletion src/core/db/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class PremiumType(enum.Enum):


class Base(MappedAsDataclass, AsyncAttrs, DeclarativeBase):
type_annotation_map: ClassVar = {
type_annotation_map: ClassVar[dict[Any, Any]] = {
bool: BOOLEAN,
int: INTEGER,
}
Expand Down
Loading

0 comments on commit 5e95d0b

Please sign in to comment.