aboutsummaryrefslogtreecommitdiff
path: root/webmentions_ssg/tasks/extension.py
diff options
context:
space:
mode:
authorDennis Fink2026-11-16 09:27:29 +0100
committerDennis Fink2026-11-16 09:27:29 +0100
commit85b10c38f39732421a230d938adddc7805d0e001 (patch)
tree396f29962fac45245c6f1486e188ad905588659d /webmentions_ssg/tasks/extension.py
parenteb404d9a1a328278c92753e65100579e7ad98e48 (diff)
downloadwebmentions-ssg-85b10c38f39732421a230d938adddc7805d0e001.tar.gz
webmentions-ssg-85b10c38f39732421a230d938adddc7805d0e001.zip
style(core): reorder module declarations
Group globals and data classes near module imports, keep primary classes and functions together, and move implementation helpers after their public callers. Reorder Alembic setup so configuration happens after the required helpers are defined, without changing runtime behavior.
Diffstat (limited to 'webmentions_ssg/tasks/extension.py')
-rw-r--r--webmentions_ssg/tasks/extension.py144
1 files changed, 72 insertions, 72 deletions
diff --git a/webmentions_ssg/tasks/extension.py b/webmentions_ssg/tasks/extension.py
index 3f172f4..a878e16 100644
--- a/webmentions_ssg/tasks/extension.py
+++ b/webmentions_ssg/tasks/extension.py
@@ -16,78 +16,6 @@ from huey.api import TaskWrapper
from .json_serializer import TaggedJSONSerializer
-def _redis_class_and_options(
- parsed: SplitResult,
-) -> tuple[type[BaseHuey], dict[str, Any]]:
- """Select a Redis Huey flavour while retaining the underlying URL scheme.
-
- :param parsed: Parsed Redis backend URL, including any flavour suffix.
- :returns: Huey class and constructor options for the requested Redis flavour.
- :raises RuntimeError: If the Redis backend flavour is unsupported.
- """
- from huey import (
- PriorityRedisExpireHuey,
- PriorityRedisHuey,
- RedisExpireHuey,
- RedisHuey,
- )
-
- scheme = parsed.scheme.lower()
- base_scheme, separator, flavour = scheme.partition("+")
- backends = {
- "": RedisHuey,
- "priority": PriorityRedisHuey,
- "expire": RedisExpireHuey,
- "priority+expire": PriorityRedisExpireHuey,
- }
- if flavour not in backends or (separator and not flavour):
- raise RuntimeError(f"Unsupported HUEY_URL scheme: {scheme!r}")
-
- fixed_url = urlunsplit(parsed._replace(scheme=base_scheme))
- return backends[flavour], {"url": fixed_url}
-
-
-def _file_class_and_options(
- url: str, scheme: str
-) -> tuple[type[BaseHuey], dict[str, Any]]:
- """Extract a relative or absolute SQLite/file storage path.
-
- :param url: Backend URL containing the storage filename or path.
- :param scheme: Selected file or SQLite backend scheme.
- :returns: Huey class and constructor options for the file-backed queue.
- :raises RuntimeError: If the storage URL is malformed or omits the path.
- """
- from huey import FileHuey, SqliteHuey
-
- if scheme == "sqlite":
- backend, option, description, example = (
- SqliteHuey,
- "filename",
- "SQLite Huey",
- "sqlite:///huey.db (relative) or sqlite:////var/huey.db (absolute)",
- )
- missing = "SQLite Huey URL must include a database path."
- else:
- backend, option, description, example = (
- FileHuey,
- "path",
- "File Huey",
- "file:///huey-queue (relative) or file:////var/huey-queue (absolute)",
- )
- missing = "File Huey URL must include a directory path."
-
- prefix = f"{scheme}:///"
- if not url.startswith(prefix):
- raise RuntimeError(f"{description} URLs must look like {example}")
-
- # Four slashes retain the initial '/' of an absolute path; three do not.
- path = url.removeprefix(prefix)
- if not path:
- raise RuntimeError(missing)
-
- return backend, {option: path}
-
-
class _DeferredTask:
"""Stable public task handle which follows the extension's current backend.
@@ -328,3 +256,75 @@ class FlaskHuey:
return BlackHoleHuey, {}
raise RuntimeError(f"Unsupported HUEY_URL scheme: {scheme!r}")
+
+
+def _redis_class_and_options(
+ parsed: SplitResult,
+) -> tuple[type[BaseHuey], dict[str, Any]]:
+ """Select a Redis Huey flavour while retaining the underlying URL scheme.
+
+ :param parsed: Parsed Redis backend URL, including any flavour suffix.
+ :returns: Huey class and constructor options for the requested Redis flavour.
+ :raises RuntimeError: If the Redis backend flavour is unsupported.
+ """
+ from huey import (
+ PriorityRedisExpireHuey,
+ PriorityRedisHuey,
+ RedisExpireHuey,
+ RedisHuey,
+ )
+
+ scheme = parsed.scheme.lower()
+ base_scheme, separator, flavour = scheme.partition("+")
+ backends = {
+ "": RedisHuey,
+ "priority": PriorityRedisHuey,
+ "expire": RedisExpireHuey,
+ "priority+expire": PriorityRedisExpireHuey,
+ }
+ if flavour not in backends or (separator and not flavour):
+ raise RuntimeError(f"Unsupported HUEY_URL scheme: {scheme!r}")
+
+ fixed_url = urlunsplit(parsed._replace(scheme=base_scheme))
+ return backends[flavour], {"url": fixed_url}
+
+
+def _file_class_and_options(
+ url: str, scheme: str
+) -> tuple[type[BaseHuey], dict[str, Any]]:
+ """Extract a relative or absolute SQLite/file storage path.
+
+ :param url: Backend URL containing the storage filename or path.
+ :param scheme: Selected file or SQLite backend scheme.
+ :returns: Huey class and constructor options for the file-backed queue.
+ :raises RuntimeError: If the storage URL is malformed or omits the path.
+ """
+ from huey import FileHuey, SqliteHuey
+
+ if scheme == "sqlite":
+ backend, option, description, example = (
+ SqliteHuey,
+ "filename",
+ "SQLite Huey",
+ "sqlite:///huey.db (relative) or sqlite:////var/huey.db (absolute)",
+ )
+ missing = "SQLite Huey URL must include a database path."
+ else:
+ backend, option, description, example = (
+ FileHuey,
+ "path",
+ "File Huey",
+ "file:///huey-queue (relative) or file:////var/huey-queue (absolute)",
+ )
+ missing = "File Huey URL must include a directory path."
+
+ prefix = f"{scheme}:///"
+ if not url.startswith(prefix):
+ raise RuntimeError(f"{description} URLs must look like {example}")
+
+ # Four slashes retain the initial '/' of an absolute path; three do not.
+ path = url.removeprefix(prefix)
+ if not path:
+ raise RuntimeError(missing)
+
+ return backend, {option: path}