Skip to content

Commit 1f90357

Browse files
Add ready plugin hook and refactor library init to internal plugin (#198)
* Add `ready` plugin hook and refactor library init to internal plugin * update changelog
1 parent 38f82b3 commit 1f90357

File tree

6 files changed

+48
-8
lines changed

6 files changed

+48
-8
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ and this project attempts to adhere to [Semantic Versioning](https://semver.org/
1818

1919
## [Unreleased]
2020

21+
### Added
22+
23+
- Added new plugin hook `ready` for handling application initialization during Django's application ready phase.
24+
25+
### Changed
26+
27+
- **Internal**: Refactored application initialization to use internal plugin instead of direct calls in `AppConfig.ready()`.
28+
2129
## [0.16.0]
2230

2331
🚨 This release contains some breaking changes. See the Changed section for more information. 🚨

docs/plugins.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ django-bird uses a plugin system based on [pluggy](https://pluggy.readthedocs.io
2020
```
2121
````
2222

23+
````{py:function} ready(app_settings: django_bird.conf.AppSettings) -> None
24+
:canonical: django_bird.plugins.hookspecs.ready
25+
26+
```{autodoc2-docstring} django_bird.plugins.hookspecs.ready
27+
:parser: myst
28+
```
29+
````
30+
2331
````{py:function} register_asset_types(register_type: collections.abc.Callable[[django_bird.staticfiles.AssetType], None]) -> None
2432
:canonical: django_bird.plugins.hookspecs.register_asset_types
2533

src/django_bird/apps.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,8 @@ class DjangoBirdAppConfig(AppConfig):
1515

1616
@override
1717
def ready(self):
18-
from django_bird.components import components
1918
from django_bird.conf import app_settings
2019
from django_bird.plugins import pm
21-
from django_bird.staticfiles import asset_types
2220

23-
app_settings.autoconfigure()
24-
pm.hook.register_asset_types(register_type=asset_types.register_type)
25-
components.discover_components()
21+
for init_handler in pm.hook.ready(app_settings=app_settings):
22+
init_handler()

src/django_bird/conf.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,24 @@
1010
import django.template
1111
from django.conf import settings
1212

13+
from django_bird import hookimpl
14+
1315
from ._typing import override
1416
from .utils import unique_ordered
1517

16-
DJANGO_BIRD_SETTINGS_NAME = "DJANGO_BIRD"
1718

18-
DJANGO_BIRD_BUILTINS = "django_bird.templatetags.django_bird"
19-
DJANGO_BIRD_FINDER = "django_bird.staticfiles.BirdAssetFinder"
19+
@hookimpl
20+
def ready(app_settings: AppSettings):
21+
from .components import components
22+
from .plugins import pm
23+
from .staticfiles import asset_types
24+
25+
app_settings.autoconfigure()
26+
pm.hook.register_asset_types(register_type=asset_types.register_type)
27+
components.discover_components()
28+
29+
30+
DJANGO_BIRD_SETTINGS_NAME = "DJANGO_BIRD"
2031

2132

2233
@dataclass
@@ -44,6 +55,10 @@ def get_component_directory_names(self):
4455
return unique_ordered([*self.COMPONENT_DIRS, "bird"])
4556

4657

58+
DJANGO_BIRD_BUILTINS = "django_bird.templatetags.django_bird"
59+
DJANGO_BIRD_FINDER = "django_bird.staticfiles.BirdAssetFinder"
60+
61+
4762
@final
4863
class AutoConfigurator:
4964
def __init__(self, app_settings: AppSettings) -> None:

src/django_bird/plugins/hookspecs.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from pluggy import HookspecMarker
99

1010
if TYPE_CHECKING:
11+
from django_bird.conf import AppSettings
1112
from django_bird.staticfiles import Asset
1213
from django_bird.staticfiles import AssetType
1314

@@ -37,6 +38,16 @@ def get_template_directories() -> list[Path]:
3738
"""
3839

3940

41+
@hookspec
42+
def ready(app_settings: AppSettings) -> None:
43+
"""Called when the django-bird application is ready.
44+
45+
This hook is called during Django's application ready phase,
46+
allowing plugins to perform necessary setup like configuring
47+
settings, registering features, or any other initialization tasks.
48+
"""
49+
50+
4051
@hookspec
4152
def register_asset_types(register_type: Callable[[AssetType], None]) -> None:
4253
"""Register a new type of asset.

src/django_bird/plugins/manager.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
pm.load_setuptools_entrypoints("django_bird")
1313

1414
DEFAULT_PLUGINS: list[str] = [
15+
"django_bird.conf",
1516
"django_bird.staticfiles",
1617
"django_bird.templates",
1718
]

0 commit comments

Comments
 (0)