Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release 0.5.1 #56

Merged
merged 4 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ An opinionated Discord bot framework inspired by Django and built on
top of [Hikari](https://github.com/hikari-py/hikari), [Tanjun](https://github.com/FasterSpeeding/Tanjun),
[Ormar](https://github.com/collerek/ormar), and [Alembic](https://alembic.sqlalchemy.org/en/latest/).

Atsume is in alpha and breaking changes should be expected. If you have any feedback or advice, feel free
to find me in the [Hikari Discord](https://discord.gg/Jx4cNGG).


## Features

Expand Down
8 changes: 4 additions & 4 deletions atsume/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from atsume.db.manager import database
from atsume.component.manager import manager as component_manager
from atsume.component import Component, ComponentConfig
from atsume.extensions.loader import attach_extensions, load_module_setting
from atsume.extensions.loader import attach_extensions, load_module_class
from atsume.utils import module_to_path


Expand Down Expand Up @@ -69,9 +69,9 @@ def initialize_discord() -> typing.Tuple[hikari.GatewayBot, tanjun.Client]:
)

if settings.VOICE_COMPONENT:
bot._voice = load_module_setting(
"VOICE_COMPONENT", hikari.impl.VoiceComponentImpl
)
bot._voice = load_module_class(
settings.VOICE_COMPONENT, hikari.impl.VoiceComponentImpl
)(bot)

global_commands = not settings.DEBUG and settings.GLOBAL_COMMANDS

Expand Down
14 changes: 12 additions & 2 deletions atsume/extensions/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def attach_extensions(client: tanjun.Client) -> None:
extensions.update(settings.EXTENSIONS)
extensions.update(ATSUME_EXTENSIONS)
for module_path in extensions:
func = load_module_setting(module_path, ExtensionCallable)
func = load_module_func(module_path, ExtensionCallable)
func(client)


Expand All @@ -36,7 +36,17 @@ def __init__(self, module_path: str):
T = typing.TypeVar("T")


def load_module_setting(module_path: str, return_type: typing.Type[T]) -> T:
def load_module_func(module_path: str, return_type: typing.Type[T]) -> T:
path = module_path.split(".")
try:
module = importlib.import_module(".".join(path[:-1]))
func = getattr(module, path[-1])
except (ModuleNotFoundError, AttributeError):
raise ModulePathNotFound(module_path)
return typing.cast(T, func)


def load_module_class(module_path: str, return_type: T) -> T:
path = module_path.split(".")
try:
module = importlib.import_module(".".join(path[:-1]))
Expand Down
51 changes: 5 additions & 46 deletions atsume/extensions/timer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

logger = logging.getLogger(__name__)


CallableArgs = typing.TypeVar("CallableArgs")
CallableKwargs = typing.TypeVar("CallableKwargs")

Expand Down Expand Up @@ -179,52 +178,12 @@ async def close(self) -> None:
task._cancel(unregister=False)


class CacheTracker:
def __init__(self) -> None:
self.count = 0
self.client: typing.Optional[tanjun.abc.Client] = None
self.callback: typing.Optional[typing.Callable[..., None]] = None

def start(
self, client: tanjun.abc.Client, callback: typing.Callable[..., None]
) -> None:
self.client = client
self.callback = callback
assert self.client.events is not None
self.client.events.subscribe(hikari.events.ShardReadyEvent, self.on_ready)
self.client.events.subscribe(
hikari.events.MemberChunkEvent, self.on_member_chunk
)

async def on_ready(self, event: hikari.events.ShardReadyEvent) -> None:
self.count += len(event.unavailable_guilds)

async def on_member_chunk(self, event: hikari.events.MemberChunkEvent) -> None:
self.count -= 1
if self.count == 0:
await self.finish()

async def finish(self) -> None:
assert self.client is not None
assert self.client.events is not None
self.client.events.unsubscribe(hikari.events.ShardReadyEvent, self.on_ready)
self.client.events.unsubscribe(
hikari.events.MemberChunkEvent, self.on_member_chunk
)

assert self.callback is not None
self.callback()


_cache_tracker = CacheTracker()


def hook_extension(c: tanjun.Client) -> None:
@c.with_client_callback(tanjun.ClientCallbackNames.STARTING)
async def on_starting(client: alluka.Injected[tanjun.abc.Client]) -> None:
timer = Timer()
client.set_type_dependency(Timer, timer)
_cache_tracker.start(client, timer._start)
c.set_type_dependency(Timer, Timer())

@c.with_client_callback(tanjun.ClientCallbackNames.STARTED)
async def on_started(timer: alluka.Injected[Timer]) -> None:
timer._start()

@c.with_client_callback(tanjun.ClientCallbackNames.CLOSING)
async def on_closing(timer: alluka.Injected[Timer]) -> None:
Expand Down
63 changes: 60 additions & 3 deletions docs/atsume_reference/migrations.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,69 @@
# Migrations

## Formatting Migrations
Atsume integrates [Alembic](https://alembic.sqlalchemy.org/en/latest/index.html)
for performing semi-automatic database migrations. A migration is a
database operation that takes us from a previous definition of the
models to the current.

Migrations are automatically formatted if you have the `black` package installed. You can do
:::warning
Atsume's migrations are still under heavy development! While
it can handle many simple tasks fine, it may not always catch
more complicated model modifications. It's a good idea to always
double-check the generated migration files before running them.
:::

## Limitations

Atsume's automatic generation of migrations largely shares the same list of
limitations as Alembic. This notably includes:

- Changes to table, column, or constraint name
- Change of a column's `server_default`

To create migrations for these changes, they must be created manually, which is covered later.

Extending Alembic to add support for automatically generating these changes is
on the roadmap.

## Commands

### Making Migrations

Migrations can be created with the `makemigrations` command.

```shell
python manage.py makemigrations
```

This will make new migrations for any changes in any currently loaded apps.

You can also specify to limit to just a single app.

```shell
python manage.py makemigrations -c my_component
```

### Upgrading and downgrading migrations

You can apply all generated migrations with the `upgrade` command.

```shell
python manage.py upgrade
```

You can also specify a specific app or revision number to upgrade to.

```shell
python manage.py upgrade my_component
```


## Auto-formatting Migrations

Migrations are automatically formatted if you have the `black` package installed.

```shell
pip install black
```

to enable it.

Empty file added docs/atsume_reference/models.md
Empty file.
1 change: 1 addition & 0 deletions docs/tutorial/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ tutorial_1
tutorial_2
tutorial_3
tutorial_4
tutorial_5

```
2 changes: 1 addition & 1 deletion docs/tutorial/tutorial_4.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Working With The Database
# Database Models and Queries

Atsume integrates the [Ormar](https://collerek.github.io/ormar/) library for handling
database operations. Ormar is an ORM, so it works through defining your
Expand Down
11 changes: 11 additions & 0 deletions docs/tutorial/tutorial_5.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Database Migrations

Database migrations are operations that help us transition from a previous
state of the database to a new one. In Atsume, migrations can be automatically
generated as you create and update your database models.

## Adding a foreign key and inspecting the migration

## Renaming a field

## Downgrading
Loading