|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | | -import warnings |
4 | | -from contextlib import suppress |
5 | 3 | from dataclasses import dataclass |
6 | 4 | from dataclasses import field |
7 | 5 | from pathlib import Path |
8 | | -from typing import Any |
9 | | -from typing import final |
10 | 6 |
|
11 | | -import django.template |
12 | 7 | from django.conf import settings |
13 | 8 |
|
14 | | -from django_bird import hookimpl |
15 | | - |
16 | 9 | from ._typing import override |
17 | 10 | from .utils import unique_ordered |
18 | 11 |
|
19 | 12 | DJANGO_BIRD_SETTINGS_NAME = "DJANGO_BIRD" |
20 | 13 |
|
| 14 | +DJANGO_BIRD_BUILTINS = "django_bird.templatetags.django_bird" |
| 15 | +DJANGO_BIRD_FINDER = "django_bird.staticfiles.BirdAssetFinder" |
| 16 | + |
21 | 17 |
|
22 | 18 | @dataclass |
23 | 19 | class AppSettings: |
24 | 20 | COMPONENT_DIRS: list[Path | str] = field(default_factory=list) |
25 | | - ENABLE_AUTO_CONFIG: bool = True |
26 | 21 | ENABLE_BIRD_ATTRS: bool = True |
27 | | - _configurator: AutoConfigurator = field(init=False) |
28 | | - |
29 | | - def __post_init__(self): |
30 | | - self._configurator = AutoConfigurator(self) |
31 | 22 |
|
32 | 23 | @override |
33 | 24 | def __getattribute__(self, __name: str) -> object: |
34 | 25 | user_settings = getattr(settings, DJANGO_BIRD_SETTINGS_NAME, {}) |
35 | 26 | return user_settings.get(__name, super().__getattribute__(__name)) |
36 | 27 |
|
37 | | - def autoconfigure(self) -> None: |
38 | | - if not self.ENABLE_AUTO_CONFIG: |
39 | | - return |
40 | | - |
41 | | - warnings.warn( |
42 | | - "Autoconfiguration of django-bird is deprecated and has been moved to the " |
43 | | - "django-bird-autoconf plugin. Please install the new plugin from PyPI in your " |
44 | | - "project if you wish to keep this behavior, as this will be removed from the " |
45 | | - "core library in a future version.", |
46 | | - DeprecationWarning, |
47 | | - stacklevel=2, |
48 | | - ) |
49 | | - self._configurator.autoconfigure() |
50 | | - |
51 | 28 | def get_component_directory_names(self): |
52 | 29 | return unique_ordered([*self.COMPONENT_DIRS, "bird"]) |
53 | 30 |
|
54 | 31 |
|
55 | | -@hookimpl |
56 | | -def ready(): |
57 | | - app_settings.autoconfigure() |
58 | | - |
59 | | - |
60 | | -DJANGO_BIRD_BUILTINS = "django_bird.templatetags.django_bird" |
61 | | -DJANGO_BIRD_FINDER = "django_bird.staticfiles.BirdAssetFinder" |
62 | | - |
63 | | - |
64 | | -@final |
65 | | -class AutoConfigurator: |
66 | | - def __init__(self, app_settings: AppSettings) -> None: |
67 | | - self.app_settings = app_settings |
68 | | - self._configured = False |
69 | | - |
70 | | - def autoconfigure(self) -> None: |
71 | | - self.configure_templates() |
72 | | - self.configure_staticfiles() |
73 | | - self._configured = True |
74 | | - |
75 | | - def configure_templates(self) -> None: |
76 | | - template_config = None |
77 | | - |
78 | | - for config in settings.TEMPLATES: |
79 | | - engine_name = config.get("NAME") or config["BACKEND"].split(".")[-2] |
80 | | - if engine_name == "django": |
81 | | - template_config = config |
82 | | - break |
83 | | - |
84 | | - if template_config is None: |
85 | | - return |
86 | | - |
87 | | - options = template_config.setdefault("OPTIONS", {}) |
88 | | - |
89 | | - self.configure_builtins(options) |
90 | | - |
91 | | - # Force re-evaluation of settings.TEMPLATES because EngineHandler caches it. |
92 | | - with suppress(AttributeError): # pragma: no cover |
93 | | - del django.template.engines.templates |
94 | | - django.template.engines._engines = {} # type:ignore[attr-defined] # pyright: ignore[reportAttributeAccessIssue] |
95 | | - |
96 | | - def configure_builtins(self, options: dict[str, Any]) -> None: |
97 | | - builtins = options.setdefault("builtins", []) |
98 | | - |
99 | | - builtins_already_configured = DJANGO_BIRD_BUILTINS in builtins |
100 | | - |
101 | | - if not builtins_already_configured: |
102 | | - builtins.append(DJANGO_BIRD_BUILTINS) |
103 | | - |
104 | | - def configure_staticfiles(self) -> None: |
105 | | - finders_already_configured = DJANGO_BIRD_FINDER in settings.STATICFILES_FINDERS |
106 | | - |
107 | | - if not finders_already_configured: |
108 | | - settings.STATICFILES_FINDERS.append(DJANGO_BIRD_FINDER) |
109 | | - |
110 | | - |
111 | 32 | app_settings = AppSettings() |
0 commit comments