Skip to content
Snippets Groups Projects
Verified Commit 900bbc27 authored by Nik | Klampfradler's avatar Nik | Klampfradler
Browse files

Use setuptools entrypoints to find apps

In contrast to scanning the `aleksis.apps` namespace, this has some
benefits:

 1. Does not need to import the module to consider it
    (resolve potential circular imports :star_struck:)
 2. Does not need to scan filesystems and wheels
 3. Apps can choose their own namespaces, register multiple apps/
    variants, and much more)
parent d1ff5fff
No related branches found
No related tags found
Loading
Pipeline #4515 passed
import os
import pkgutil
import time
from datetime import datetime, timedelta
from importlib import import_module
from importlib import import_module, metadata
from itertools import groupby
from operator import itemgetter
from typing import Any, Callable, Optional, Sequence, Union
......@@ -59,14 +58,8 @@ def dt_show_toolbar(request: HttpRequest) -> bool:
def get_app_packages() -> Sequence[str]:
"""Find all packages within the aleksis.apps namespace."""
# Import error are non-fatal here because probably simply no app is installed.
try:
import aleksis.apps
except ImportError:
return []
return [f"aleksis.apps.{pkg[1]}" for pkg in pkgutil.iter_modules(aleksis.apps.__path__)]
"""Find all registered apps from the setuptools entrypoint."""
return [f"{ep.module}.{ep.attr}" for ep in metadata.entry_points()["aleksis.app"]]
def merge_app_settings(
......@@ -81,7 +74,8 @@ def merge_app_settings(
Note: Only selected names will be imported frm it to minimise impact of
potentially malicious apps!
"""
for pkg in get_app_packages():
for app in get_app_packages():
pkg = ".".join(app.split(".")[:-2])
try:
mod_settings = import_module(pkg + ".settings")
except ImportError:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment