-
Notifications
You must be signed in to change notification settings - Fork 2
Fix real_patterns mutation targeting undeclared extension point #106
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
Merged
wpak-ai
merged 2 commits into
cppalliance:develop
from
whisper67265:fix/url-pattern-mutation
Jun 11, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,113 @@ | ||
| # SPDX-FileCopyrightText: 2026 Andrew Zhang <whisper67265@outlook.com> | ||
| # | ||
| # SPDX-License-Identifier: BSL-1.0 | ||
|
|
||
| """Adapter for registering Boost endpoint routes on Weblate's URL pattern list.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import importlib.metadata | ||
| import logging | ||
| from functools import lru_cache | ||
| from types import ModuleType | ||
|
|
||
| from django.urls import URLResolver, include, path | ||
| from packaging.version import InvalidVersion, Version | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| _MIN_WEBLATE_VERSION = Version("2026.5") | ||
| _BOOST_ENDPOINT_PREFIX = "boost-endpoint/" | ||
|
|
||
|
|
||
| class WeblateUrlLayoutError(RuntimeError): | ||
| """Raised when ``weblate.urls`` lacks the layout this plugin expects.""" | ||
|
|
||
|
|
||
| def _weblate_version() -> str: | ||
| try: | ||
| return importlib.metadata.version("Weblate") | ||
| except importlib.metadata.PackageNotFoundError: | ||
| return "unknown" | ||
|
|
||
|
|
||
| def _assert_weblate_url_layout(wl_urls: ModuleType) -> None: | ||
| """Verify Weblate exposes the ``real_patterns`` list before mutation.""" | ||
| version = _weblate_version() | ||
| if not hasattr(wl_urls, "real_patterns"): | ||
| msg = ( | ||
| "weblate.urls.real_patterns is missing; " | ||
| f"Weblate {version} URL layout is incompatible with cppa-weblate-plugin" | ||
| ) | ||
| raise WeblateUrlLayoutError(msg) | ||
| if not isinstance(wl_urls.real_patterns, list): | ||
| msg = ( | ||
| "weblate.urls.real_patterns is not a list; " | ||
| f"Weblate {version} URL layout is incompatible with cppa-weblate-plugin" | ||
| ) | ||
| raise WeblateUrlLayoutError(msg) | ||
| if version == "unknown": | ||
| msg = ( | ||
| "Weblate package version could not be determined; " | ||
| "cppa-weblate-plugin cannot verify minimum version compatibility" | ||
| ) | ||
| raise WeblateUrlLayoutError(msg) | ||
| try: | ||
| parsed = Version(version) | ||
| except InvalidVersion as exc: | ||
| msg = ( | ||
| f"Weblate version string {version!r} could not be parsed; " | ||
| "cppa-weblate-plugin cannot verify minimum version compatibility" | ||
| ) | ||
| raise WeblateUrlLayoutError(msg) from exc | ||
| if parsed < _MIN_WEBLATE_VERSION: | ||
| msg = ( | ||
| f"Weblate {version} is below the minimum supported version " | ||
| f"{_MIN_WEBLATE_VERSION}; cppa-weblate-plugin requires Weblate " | ||
| f"{_MIN_WEBLATE_VERSION} or newer" | ||
| ) | ||
| raise WeblateUrlLayoutError(msg) | ||
|
|
||
|
|
||
| def _boost_endpoint_route() -> URLResolver: | ||
| return path( | ||
| _BOOST_ENDPOINT_PREFIX, | ||
| include(("boost_weblate.endpoint.urls", "boost_endpoint")), | ||
| ) | ||
|
|
||
|
|
||
| def _route_already_registered(real_patterns: list) -> bool: | ||
| return any( | ||
| str(getattr(entry, "pattern", "")) == _BOOST_ENDPOINT_PREFIX | ||
| for entry in real_patterns | ||
| ) | ||
|
|
||
|
|
||
| @lru_cache(maxsize=1) | ||
| def register_boost_endpoint_urls() -> None: | ||
| """Append Boost endpoint routes to ``weblate.urls.real_patterns`` once. | ||
|
|
||
| Weblate builds ``urlpatterns`` from module-level ``real_patterns``. Appending | ||
| before the ``URL_PREFIX`` wrapper keeps routes under prefix configuration. | ||
|
|
||
| Idempotent via ``lru_cache``; safe to call from ``AppConfig.ready()``. | ||
|
|
||
| Note: if ``weblate.urls`` is not importable at call time, the no-op result | ||
| is still cached; retrying after the module becomes importable requires an | ||
| explicit ``register_boost_endpoint_urls.cache_clear()`` call. | ||
| """ | ||
| try: | ||
| import weblate.urls as wl_urls # noqa: PLC0415 | ||
| except ModuleNotFoundError as exc: | ||
| logger.debug( | ||
| "boost_weblate.endpoint: skipping URL registration (import error: %s)", | ||
| exc, | ||
| ) | ||
| return | ||
|
|
||
| _assert_weblate_url_layout(wl_urls) | ||
|
|
||
| if _route_already_registered(wl_urls.real_patterns): | ||
| return | ||
|
|
||
| wl_urls.real_patterns.append(_boost_endpoint_route()) |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.