mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-08 11:46:35 +00:00
TC (flake8-type-checking): typing-only imports moved into
``if TYPE_CHECKING:`` blocks across 8 source files via auto-fix
(117 hits). Reduces runtime import surface and rules out circular
imports for type-only deps. Project-wide ``from __future__ import
annotations`` (FA, already enabled) keeps annotations as strings,
so the moved imports never need to load at runtime.
N (pep8-naming): 32 hits handled.
Renames (semantic + lint):
- ``isSmallerThenMaximumBackupSize`` -> ``is_larger_than_maximum_backup_size``
(snake_case + fixes both the typo "Then -> Than" and the
semantic mismatch: the function returns ``current > maximum``)
- ``isDirectoryDeletable`` / ``deleteVersion`` /
``getAmountOfIteration`` / ``deleteIteration`` -> snake_case
- 8 test functions with ALL-CAPS env-var fragments
(``test_..._DOMAIN_PRIMARY_...`` etc.) -> snake_case
- Mock alias ``VH`` -> ``vh`` in test_host_vars.py
Exception class renames (``N818`` Error suffix):
- ``TimeLimitExceededException`` -> ``TimeLimitExceededError``
- ``AttemptException`` -> ``AttemptError``
- ``PathNotFound`` -> ``PathNotFoundError`` (propagated across
4 callers under tests/integration/lookups/config/)
- ``_ExitJson`` / ``_FailJson`` -> ``*_Error``
Refactor:
- ``METADATA = {...}`` local constant in two functions of
``plugins/filter/invokable_paths.py`` lifted to module-level
``_METADATA_KEYS``.
- ``from typing import TYPE_CHECKING`` in
``tasks/utils/migrate_meta_layout/__main__.py`` moved above
the ``sys.path.insert`` hack to satisfy E402 (collateral from
the TC auto-fix that landed it after the path mutation).
Per-file ignores in pyproject.toml for the legitimate cases the
rule cannot accept:
- ``pull-specific-host.py`` -> ``N999`` (Ansible-invoked role-level
script, hyphenated filename pinned by the surrounding stack).
- 4 matrix-deploy files -> ``N806`` for the ``include_R`` /
``raw_include_R`` round-index notation; the uppercase ``R``
here mirrors the formula in the docstring rather than naming
a constant.
- ``plugins/filter/value_generator.py`` -> ``N806`` for the
lazy-loaded class binding ``ValueGenerator =
_value_generator_cls()``, which retains the imported class
identifier per PEP 8.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
133 lines
4.5 KiB
Python
133 lines
4.5 KiB
Python
"""Role-local lookup plugin for web-app-fediwall.
|
|
|
|
{{ lookup('fediwall_active', want_path) }}
|
|
|
|
For every Mastodon-API-compatible Fediverse sibling listed under the
|
|
SPOT ``meta/services.yml.fediwall.fediverse_siblings`` that is also
|
|
active on the current host (i.e. its application_id is in
|
|
``group_names``), return one entry per ``want_path``:
|
|
|
|
siblings -> the active sibling application_ids themselves
|
|
domains -> each sibling's primary domain (canonical[0])
|
|
url_bases -> each sibling's TLS ``url.base``
|
|
(= the same value ``lookup('tls', s, 'url.base')`` returns)
|
|
|
|
Used by:
|
|
meta/server.yml -> connect-src CSP whitelist (url_bases)
|
|
templates/wall-config.json.j2 -> default ``servers`` list (domains)
|
|
|
|
Sharing one lookup keeps the active-set logic in a single SPOT and
|
|
mirrors the ``tls`` lookup's positional ``want`` API.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import TYPE_CHECKING, Any
|
|
|
|
from ansible.errors import AnsibleError
|
|
from ansible.plugins.loader import lookup_loader
|
|
from ansible.plugins.lookup import LookupBase
|
|
|
|
if TYPE_CHECKING:
|
|
from collections.abc import Callable, Iterable
|
|
|
|
_APPLICATION_ID = "web-app-fediwall"
|
|
_SIBLINGS_CONFIG_PATH = "services.fediwall.fediverse_siblings"
|
|
_VALID_WANTS = ("siblings", "domains", "url_bases")
|
|
|
|
|
|
def select_active(
|
|
siblings: Iterable[str] | None,
|
|
group_names: Iterable[str] | None,
|
|
) -> list[str]:
|
|
"""Return the subset of `siblings` whose names appear in `group_names`.
|
|
|
|
Order of `siblings` is preserved. Duplicates in `siblings` are
|
|
preserved as-is. ``None`` inputs are treated as empty.
|
|
"""
|
|
if not siblings:
|
|
return []
|
|
active = set(group_names or [])
|
|
return [s for s in siblings if s in active]
|
|
|
|
|
|
def resolve_for_want(
|
|
siblings: Iterable[str] | None,
|
|
group_names: Iterable[str] | None,
|
|
want: str,
|
|
resolver: Callable[[str], str] | None,
|
|
) -> list[str]:
|
|
"""Return one value per active sibling, depending on `want`.
|
|
|
|
For ``want == 'siblings'`` the active sibling names themselves are
|
|
returned (no resolver call). For ``'domains'`` and ``'url_bases'``
|
|
the caller-supplied `resolver(sibling)` produces the value.
|
|
"""
|
|
if want == "siblings":
|
|
return select_active(siblings, group_names)
|
|
if want not in _VALID_WANTS:
|
|
raise AnsibleError(
|
|
f"fediwall_active: want must be one of {_VALID_WANTS}, got '{want}'"
|
|
)
|
|
if resolver is None:
|
|
raise AnsibleError(f"fediwall_active: want='{want}' requires a resolver")
|
|
return [resolver(s) for s in select_active(siblings, group_names)]
|
|
|
|
|
|
class LookupModule(LookupBase):
|
|
def run(
|
|
self,
|
|
terms: list[Any],
|
|
variables: dict | None = None,
|
|
**kwargs: Any,
|
|
) -> list[list[str]]:
|
|
if not terms or len(terms) != 1:
|
|
raise AnsibleError(
|
|
f"fediwall_active: exactly one positional argument required "
|
|
f"(want, one of {_VALID_WANTS})"
|
|
)
|
|
|
|
want = str(terms[0]).strip()
|
|
if want not in _VALID_WANTS:
|
|
raise AnsibleError(
|
|
f"fediwall_active: want must be one of {_VALID_WANTS}, got '{want}'"
|
|
)
|
|
|
|
variables = variables or getattr(self._templar, "available_variables", {}) or {}
|
|
group_names = variables.get("group_names", []) or []
|
|
|
|
config_lookup = lookup_loader.get(
|
|
"config", loader=self._loader, templar=self._templar
|
|
)
|
|
siblings = (
|
|
config_lookup.run(
|
|
[_APPLICATION_ID, _SIBLINGS_CONFIG_PATH], variables=variables
|
|
)[0]
|
|
or []
|
|
)
|
|
|
|
if want == "siblings":
|
|
return [select_active(siblings, group_names)]
|
|
|
|
if want == "domains":
|
|
domain_lookup = lookup_loader.get(
|
|
"domain", loader=self._loader, templar=self._templar
|
|
)
|
|
|
|
def _resolve_domain(sibling: str) -> str:
|
|
return str(domain_lookup.run([sibling], variables=variables)[0]).strip()
|
|
|
|
return [resolve_for_want(siblings, group_names, want, _resolve_domain)]
|
|
|
|
# want == "url_bases"
|
|
tls_lookup = lookup_loader.get(
|
|
"tls", loader=self._loader, templar=self._templar
|
|
)
|
|
|
|
def _resolve_url_base(sibling: str) -> str:
|
|
return str(
|
|
tls_lookup.run([sibling, "url.base"], variables=variables)[0]
|
|
).strip()
|
|
|
|
return [resolve_for_want(siblings, group_names, want, _resolve_url_base)]
|