-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor
regex_replacer
to split up responsibilities.
- Loading branch information
Showing
9 changed files
with
117 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from dj_angles.mappers.django import map_autoescape, map_block, map_css, map_extends, map_image | ||
from dj_angles.mappers.include import map_include | ||
from dj_angles.mappers.mapper import get_tag_map | ||
from dj_angles.mappers.thirdparty import map_bird |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
from collections.abc import Callable | ||
from typing import Optional, Union | ||
|
||
from django.utils.module_loading import import_string | ||
|
||
from dj_angles.mappers import map_autoescape, map_block, map_css, map_extends, map_image, map_include | ||
from dj_angles.modules import is_module_available | ||
from dj_angles.settings import get_setting | ||
|
||
TAG_NAME_TO_DJANGO_TEMPLATE_TAG_MAP: Optional[dict[Optional[str], Union[Callable, str]]] = { | ||
"extends": map_extends, | ||
"block": map_block, | ||
"verbatim": "verbatim", | ||
"include": map_include, | ||
"comment": "comment", | ||
"#": "comment", | ||
"autoescape-on": map_autoescape, | ||
"autoescape-off": map_autoescape, | ||
"csrf-token": "csrf_token", | ||
"csrf": "csrf_token", | ||
"csrf-input": "csrf_token", | ||
"debug": "debug", | ||
"filter": "filter", | ||
"lorem": "lorem", | ||
"now": "now", | ||
"spaceless": "spaceless", | ||
"templatetag": "templatetag", | ||
"image": map_image, | ||
"css": map_css, | ||
} | ||
"""Default mappings for tag names to Django template tags.""" | ||
|
||
tag_map: Optional[dict[Optional[str], Union[Callable, str]]] = None | ||
|
||
|
||
def get_tag_map() -> Optional[dict[Optional[str], Union[Callable, str]]]: | ||
"""Get the complete tag map based on the default, dynamic, and settings mappers.""" | ||
|
||
global tag_map # noqa: PLW0603 | ||
|
||
if tag_map is None: | ||
tag_map = TAG_NAME_TO_DJANGO_TEMPLATE_TAG_MAP | ||
|
||
if tag_map is None: | ||
raise AssertionError("Invalid tag_map") | ||
|
||
# Add bird if installed | ||
if is_module_available("django_bird"): | ||
# Import here to avoid circular import | ||
from dj_angles.mappers import map_bird | ||
|
||
tag_map.update({"bird": map_bird}) | ||
|
||
# Add dynamic mappers if in settings | ||
mappers = get_setting("mappers", default={}) | ||
|
||
if not isinstance(mappers, dict): | ||
raise AssertionError("ANGLES.mappers must be a dictionary") | ||
|
||
tag_map.update(mappers) | ||
|
||
# Add default mapper if in settings, or fallback to the default mapper | ||
default_mapper = get_setting("default_mapper", "dj_angles.mappers.angles.default_mapper") | ||
|
||
# Add the default with a magic key of `None` | ||
tag_map.update({None: import_string(default_mapper)}) | ||
|
||
return tag_map | ||
|
||
|
||
def clear_tag_map() -> None: | ||
"""Clear the generated tag map so that it will be re-generated. Useful for tests.""" | ||
|
||
global tag_map # noqa: PLW0603 | ||
tag_map = None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from importlib.util import find_spec | ||
|
||
|
||
def is_module_available(module_name): | ||
"""Helper method to check if a module is available.""" | ||
|
||
return find_spec(module_name) is not None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters