From ce802830c37565d325ba2ecc060443550abb1955 Mon Sep 17 00:00:00 2001 From: Dennis Fink Date: Sun, 25 Oct 2026 22:26:40 +0100 Subject: refactor(core): simplify configuration keys Remove the WEBMENTIONS_SSG prefix from application-specific configuration settings and update validators, tasks, and tests to use the shorter names. --- tests/forms/test_validators.py | 6 +++--- tests/tasks/test_notifications.py | 10 +++++----- tests/tasks/test_receiver.py | 12 +++++------- tests/tasks/test_scanner.py | 6 +++--- tests/tasks/test_sender.py | 4 ++-- webmentions_ssg/config.py | 16 ++++++++-------- webmentions_ssg/forms/validators.py | 5 +---- webmentions_ssg/tasks/consumer.py | 4 +--- webmentions_ssg/tasks/notifications.py | 2 +- webmentions_ssg/tasks/receiver.py | 10 +++------- webmentions_ssg/tasks/scanner.py | 15 ++++++--------- webmentions_ssg/tasks/sender.py | 8 +++----- 12 files changed, 41 insertions(+), 57 deletions(-) diff --git a/tests/forms/test_validators.py b/tests/forms/test_validators.py index c63ee54..6e26039 100644 --- a/tests/forms/test_validators.py +++ b/tests/forms/test_validators.py @@ -46,21 +46,21 @@ def test_not_equal_to_rejects_unknown_field() -> None: def test_allowed_hostname_accepts_configured_hostname(app: Flask) -> None: - app.config["WEBMENTIONS_SSG_ALLOWED_HOSTNAMES"] = ["dennisfink.me"] + app.config["ALLOWED_HOSTNAMES"] = ["dennisfink.me"] form = URLForm(MultiDict({"url": "https://dennisfink.me/blog/example/"})) with app.app_context(): AllowedHostname()(form, form.url) def test_allowed_hostname_rejects_unconfigured_hostname(app: Flask) -> None: - app.config["WEBMENTIONS_SSG_ALLOWED_HOSTNAMES"] = ["dennisfink.me"] + app.config["ALLOWED_HOSTNAMES"] = ["dennisfink.me"] form = URLForm(MultiDict({"url": "https://example.com/post"})) with app.app_context(), pytest.raises(ValidationError, match="Invalid input"): AllowedHostname()(form, form.url) def test_allowed_hostname_uses_custom_message(app: Flask) -> None: - app.config["WEBMENTIONS_SSG_ALLOWED_HOSTNAMES"] = ["dennisfink.me"] + app.config["ALLOWED_HOSTNAMES"] = ["dennisfink.me"] form = URLForm(MultiDict({"url": "https://example.com/post"})) with ( app.app_context(), diff --git a/tests/tasks/test_notifications.py b/tests/tasks/test_notifications.py index 6069d79..fa6ef79 100644 --- a/tests/tasks/test_notifications.py +++ b/tests/tasks/test_notifications.py @@ -15,7 +15,7 @@ from webmentions_ssg.models import ReceivedWebmention def test_sends_email_for_verified_webmention(app: Flask) -> None: from webmentions_ssg.tasks.notifications import send_webmention_notification - app.config["WEBMENTIONS_SSG_NOTIFICATION_EMAIL"] = "owner@example.com" + app.config["NOTIFICATION_EMAIL"] = "owner@example.com" identifier = uuid.uuid7() with app.app_context(): @@ -43,7 +43,7 @@ def test_sends_email_for_verified_webmention(app: Flask) -> None: def test_skips_unverified_webmentions(app: Flask, status: str) -> None: from webmentions_ssg.tasks.notifications import send_webmention_notification - app.config["WEBMENTIONS_SSG_NOTIFICATION_EMAIL"] = "owner@example.com" + app.config["NOTIFICATION_EMAIL"] = "owner@example.com" identifier = uuid.uuid7() with app.app_context(): @@ -66,7 +66,7 @@ def test_skips_unverified_webmentions(app: Flask, status: str) -> None: def test_skips_when_notifications_disabled(app: Flask) -> None: from webmentions_ssg.tasks.notifications import send_webmention_notification - app.config["WEBMENTIONS_SSG_NOTIFICATION_EMAIL"] = None + app.config["NOTIFICATION_EMAIL"] = None identifier = uuid.uuid7() with app.app_context(): @@ -89,7 +89,7 @@ def test_skips_when_notifications_disabled(app: Flask) -> None: def test_skips_unknown_webmention(app: Flask) -> None: from webmentions_ssg.tasks.notifications import send_webmention_notification - app.config["WEBMENTIONS_SSG_NOTIFICATION_EMAIL"] = "owner@example.com" + app.config["NOTIFICATION_EMAIL"] = "owner@example.com" with app.app_context(): with patch("webmentions_ssg.tasks.notifications.mail.send") as send: @@ -115,7 +115,7 @@ def test_queues_notification_after_verification( ) -> None: from webmentions_ssg.tasks import receiver - app.config["WEBMENTIONS_SSG_NOTIFICATION_EMAIL"] = recipient + app.config["NOTIFICATION_EMAIL"] = recipient identifier = uuid.uuid7() with app.app_context(): diff --git a/tests/tasks/test_receiver.py b/tests/tasks/test_receiver.py index 15289ac..f4ab09f 100644 --- a/tests/tasks/test_receiver.py +++ b/tests/tasks/test_receiver.py @@ -206,9 +206,7 @@ def test_fetch_source_returns_response_and_body( "User-Agent": f"{receiver.APP_NAME}/{receiver.VERSION} ReceivedWebmention", } assert options["follow_redirects"] is True - assert options["max_redirects"] == app.config.get( - "WEBMENTIONS_SSG_MAX_REDIRECTS", 20 - ) + assert options["max_redirects"] == app.config.get("MAX_REDIRECTS", 20) assert options["trust_env"] is False assert options["event_hooks"] == {"request": [receiver.ensure_public_request]} @@ -265,7 +263,7 @@ def test_fetch_source_propagates_network_error( def test_fetch_source_rejects_declared_oversized_body( httpx_client: Mock, app: Flask, receiver: ModuleType ) -> None: - app.config["WEBMENTIONS_SSG_MAX_SOURCE_BYTES"] = 10 + app.config["MAX_SOURCE_BYTES"] = 10 set_stream_response( httpx_client, @@ -288,7 +286,7 @@ def test_fetch_source_rejects_declared_oversized_body( def test_fetch_source_rejects_streamed_oversized_body( httpx_client: Mock, app: Flask, receiver: ModuleType ) -> None: - app.config["WEBMENTIONS_SSG_MAX_SOURCE_BYTES"] = 10 + app.config["MAX_SOURCE_BYTES"] = 10 class BodyStream(httpx.SyncByteStream): def __iter__(self): @@ -471,7 +469,7 @@ def test_verify_webmention_persists_final_state( case _: raise AssertionError(f"Unexpected outcome: {outcome}") - app.config["WEBMENTIONS_SSG_NOTIFICATION_EMAIL"] = "owner@example.com" + app.config["NOTIFICATION_EMAIL"] = "owner@example.com" with patch.object(receiver, "send_webmention_notification") as enqueue: receiver.verify_webmention.call_local(identifier) @@ -502,7 +500,7 @@ def test_verify_webmention_persists_retryable_failure_and_reraises( source_mentions_target.side_effect = exception - app.config["WEBMENTIONS_SSG_NOTIFICATION_EMAIL"] = "owner@example.com" + app.config["NOTIFICATION_EMAIL"] = "owner@example.com" with ( patch.object(receiver, "send_webmention_notification") as enqueue, diff --git a/tests/tasks/test_scanner.py b/tests/tasks/test_scanner.py index 3de5e16..5823e9e 100644 --- a/tests/tasks/test_scanner.py +++ b/tests/tasks/test_scanner.py @@ -91,8 +91,8 @@ def write_post( def configure_scanner(app: Flask, root: Path, *, base_url: str | None = None) -> None: - app.config["WEBMENTIONS_SSG_SOURCE_DIRECTORY"] = str(root) - app.config["WEBMENTIONS_SSG_SOURCE_BASE_URL"] = base_url + app.config["SOURCE_DIRECTORY"] = str(root) + app.config["SOURCE_BASE_URL"] = base_url def test_parse_entry_returns_microformats_entry(scanner_module: ModuleType) -> None: @@ -1646,7 +1646,7 @@ def test_changed_source_url_is_rejected( def test_scan_sources_returns_when_source_directory_is_not_configured( app: Flask, scanner_module: ModuleType ) -> None: - app.config["WEBMENTIONS_SSG_SOURCE_DIRECTORY"] = None + app.config["SOURCE_DIRECTORY"] = None run_scan(app, scanner_module) diff --git a/tests/tasks/test_sender.py b/tests/tasks/test_sender.py index 6e33887..6a9afc1 100644 --- a/tests/tasks/test_sender.py +++ b/tests/tasks/test_sender.py @@ -988,7 +988,7 @@ def test_endpoint_from_html_ignores_elements_without_rel_and_non_string_href( def test_read_target_body_rejects_declared_oversized_body( app: Flask, sender_module: ModuleType ) -> None: - app.config["WEBMENTIONS_SSG_MAX_TARGET_BYTES"] = 10 + app.config["MAX_TARGET_BYTES"] = 10 response = httpx.Response(200, headers={"Content-Length": "11"}, content=b"x" * 11) @@ -1017,7 +1017,7 @@ def test_read_target_body_ignores_invalid_content_length( def test_read_target_body_rejects_streamed_oversized_body( app: Flask, sender_module: ModuleType ) -> None: - app.config["WEBMENTIONS_SSG_MAX_TARGET_BYTES"] = 10 + app.config["MAX_TARGET_BYTES"] = 10 class BodyStream(httpx.SyncByteStream): def __iter__(self): diff --git a/webmentions_ssg/config.py b/webmentions_ssg/config.py index afe7126..cbdd614 100644 --- a/webmentions_ssg/config.py +++ b/webmentions_ssg/config.py @@ -19,10 +19,10 @@ class DefaultConfig: LOGFILE_BACKUP_COUNT = 10 SQLALCHEMY_DATABASE_URI = "sqlite:///webmentions-ssg.db" - WEBMENTIONS_SSG_SOURCE_DIRECTORY = None - WEBMENTIONS_SSG_SOURCE_BASE_URL = None - WEBMENTIONS_SSG_SCANNER_SCHEDULE = "0 * * * *" - WEBMENTIONS_SSG_NOTIFICATION_EMAIL = None + SOURCE_DIRECTORY = None + BASE_URL = None + SCANNER_SCHEDULE = "0 * * * *" + NOTIFICATION_EMAIL = None MAIL_SERVER = "localhost" MAIL_PORT = 25 @@ -58,8 +58,8 @@ class TestingConfig(DefaultConfig): HUEY_URL = "memory://" - WEBMENTIONS_SSG_ALLOWED_HOSTNAMES = {"dennisfink.me"} + ALLOWED_HOSTNAMES = {"dennisfink.me"} - WEBMENTIONS_SSG_MAX_REDIRECTS = 20 - WEBMENTIONS_SSG_MAX_SOURCE_BYTES = 1_000_000 - WEBMENTIONS_SSG_REQUEST_TIMEOUT = 5.0 + MAX_REDIRECTS = 20 + MAX_SOURCE_BYTES = 1_000_000 + REQUEST_TIMEOUT = 5.0 diff --git a/webmentions_ssg/forms/validators.py b/webmentions_ssg/forms/validators.py index 761dc46..990d91a 100644 --- a/webmentions_ssg/forms/validators.py +++ b/webmentions_ssg/forms/validators.py @@ -83,10 +83,7 @@ class AllowedHostname: :param field: Field containing the URL to validate. :raises ValidationError: If the URL hostname is not configured as allowed. """ - if ( - urlsplit(field.data).hostname - in current_app.config["WEBMENTIONS_SSG_ALLOWED_HOSTNAMES"] - ): + if urlsplit(field.data).hostname in current_app.config["ALLOWED_HOSTNAMES"]: return raise ValidationError(self.message or field.gettext("Invalid input.")) diff --git a/webmentions_ssg/tasks/consumer.py b/webmentions_ssg/tasks/consumer.py index d71f2fa..3ba2638 100644 --- a/webmentions_ssg/tasks/consumer.py +++ b/webmentions_ssg/tasks/consumer.py @@ -21,9 +21,7 @@ def create_consumer() -> Huey: from . import notifications, receiver, scanner, sender # noqa: F401 - schedule = crontab( - *app.config["WEBMENTIONS_SSG_SCANNER_SCHEDULE"].split(), strict=True - ) + schedule = crontab(*app.config["SCANNER_SCHEDULE"].split(), strict=True) HUEY.periodic_task(schedule)(scanner.scan_sources) return HUEY.huey diff --git a/webmentions_ssg/tasks/notifications.py b/webmentions_ssg/tasks/notifications.py index 473216b..04c968a 100644 --- a/webmentions_ssg/tasks/notifications.py +++ b/webmentions_ssg/tasks/notifications.py @@ -16,7 +16,7 @@ from ..models import ReceivedWebmention @huey.task(retries=2, retry_delay=60) def send_webmention_notification(webmention_uuid: uuid.UUID) -> None: """Send an email when a received webmention has been verified.""" - recipient = current_app.config.get("WEBMENTIONS_SSG_NOTIFICATION_EMAIL") + recipient = current_app.config.get("NOTIFICATION_EMAIL") if not recipient: return diff --git a/webmentions_ssg/tasks/receiver.py b/webmentions_ssg/tasks/receiver.py index e99eea3..8c9c05a 100644 --- a/webmentions_ssg/tasks/receiver.py +++ b/webmentions_ssg/tasks/receiver.py @@ -137,11 +137,9 @@ def fetch_source(source_url: str) -> tuple[httpx.Response, bytes]: "Accept": "text/html, application/xhtml+xml;q=0.9, text/plain;q=0.8", "User-Agent": f"{APP_NAME}/{VERSION} ReceivedWebmention", }, - timeout=httpx.Timeout( - current_app.config.get("WEBMENTIONS_SSG_REQUEST_TIMEOUT", 5.0) - ), + timeout=httpx.Timeout(current_app.config.get("REQUEST_TIMEOUT", 5.0)), follow_redirects=True, - max_redirects=current_app.config.get("WEBMENTIONS_SSG_MAX_REDIRECTS", 20), + max_redirects=current_app.config.get("MAX_REDIRECTS", 20), trust_env=False, event_hooks={"request": [ensure_public_request]}, ) as client, @@ -158,9 +156,7 @@ def fetch_source(source_url: str) -> tuple[httpx.Response, bytes]: else: raise VerificationError(f"Source returned HTTP {status}") - max_source_bytes = current_app.config.get( - "WEBMENTIONS_SSG_MAX_SOURCE_BYTES", 1_000_000 - ) + max_source_bytes = current_app.config.get("MAX_SOURCE_BYTES", 1_000_000) if (content_length := response.headers.get("Content-Length")) is not None: try: diff --git a/webmentions_ssg/tasks/scanner.py b/webmentions_ssg/tasks/scanner.py index 17898ba..1980abb 100644 --- a/webmentions_ssg/tasks/scanner.py +++ b/webmentions_ssg/tasks/scanner.py @@ -79,7 +79,7 @@ def canonical_url( if base_url is None: raise SourceScanError( "Document contains a relative canonical URL, but " - "WEBMENTIONS_SSG_SOURCE_BASE_URL is not configured" + "SOURCE_BASE_URL is not configured" ) resolved = urljoin(source_url_for_path(relative_path, base_url), href) @@ -292,8 +292,7 @@ def scan_source_file( if source_url is None: if base_url is None: raise SourceScanError( - "Document has no canonical URL and " - "WEBMENTIONS_SSG_SOURCE_BASE_URL is not configured" + "Document has no canonical URL and SOURCE_BASE_URL is not configured" ) source_url = source_url_for_path(relative_path, base_url) @@ -410,7 +409,7 @@ def scan_sources() -> None: :raises RuntimeError: If the configured source directory or base URL is invalid. """ - directory = current_app.config.get("WEBMENTIONS_SSG_SOURCE_DIRECTORY") + directory = current_app.config.get("SOURCE_DIRECTORY") if directory is None: return @@ -420,18 +419,16 @@ def scan_sources() -> None: if not root.is_dir(): raise RuntimeError(f"Source directory is not a directory: {root}") - base_url = current_app.config.get("WEBMENTIONS_SSG_SOURCE_BASE_URL") + base_url = current_app.config.get("SOURCE_BASE_URL") if base_url is not None: if not is_http_url(base_url): - raise RuntimeError( - "WEBMENTIONS_SSG_SOURCE_BASE_URL must be an absolute HTTP or HTTPS URL" - ) + raise RuntimeError("SOURCE_BASE_URL must be an absolute HTTP or HTTPS URL") base_url = f"{base_url.rstrip('/')}/" ignored_hostnames = tuple( pattern.lower().rstrip(".") - for pattern in current_app.config.get("WEBMENTIONS_SSG_IGNORED_HOSTNAMES", ()) + for pattern in current_app.config.get("IGNORED_HOSTNAMES", ()) ) sources_by_path = { diff --git a/webmentions_ssg/tasks/sender.py b/webmentions_ssg/tasks/sender.py index 1d22ad9..3b79448 100644 --- a/webmentions_ssg/tasks/sender.py +++ b/webmentions_ssg/tasks/sender.py @@ -175,7 +175,7 @@ def read_target_body(response: httpx.Response) -> bytes: :raises PermanentSenderError: If the document exceeds the configured maximum size. """ - max_bytes = current_app.config.get("WEBMENTIONS_SSG_MAX_TARGET_BYTES", 1_000_000) + max_bytes = current_app.config.get("MAX_TARGET_BYTES", 1_000_000) if (content_length := response.headers.get("Content-Length")) is not None: try: @@ -324,11 +324,9 @@ def send_webmention(webmention_uuid: uuid.UUID) -> None: try: with httpx.Client( headers={"User-Agent": (f"{APP_NAME}/{VERSION} SentWebmention")}, - timeout=httpx.Timeout( - current_app.config.get("WEBMENTIONS_SSG_REQUEST_TIMEOUT", 5.0) - ), + timeout=httpx.Timeout(current_app.config.get("REQUEST_TIMEOUT", 5.0)), follow_redirects=True, - max_redirects=current_app.config.get("WEBMENTIONS_SSG_MAX_REDIRECTS", 20), + max_redirects=current_app.config.get("MAX_REDIRECTS", 20), trust_env=False, event_hooks={"request": [ensure_public_request]}, ) as client: -- cgit v1.3.1