Skip to content
Snippets Groups Projects
Commit 59be5513 authored by Jonathan Weth's avatar Jonathan Weth :keyboard:
Browse files

Merge branch 'feature/use-entrypoints' into 'master'

Use setuptools entrypoints to find apps

See merge request !395
parents ce62cc6a 215693e3
No related branches found
No related tags found
1 merge request!395Use setuptools entrypoints to find apps
Pipeline #4531 passed
import os import os
import pkgutil
import time import time
from datetime import datetime, timedelta from datetime import datetime, timedelta
from importlib import import_module from importlib import import_module
...@@ -8,6 +7,11 @@ from operator import itemgetter ...@@ -8,6 +7,11 @@ from operator import itemgetter
from typing import Any, Callable, Optional, Sequence, Union from typing import Any, Callable, Optional, Sequence, Union
from uuid import uuid4 from uuid import uuid4
try:
from importlib import metadata
except ImportError:
import importlib_metadata as metadata
from django.conf import settings from django.conf import settings
from django.db.models import Model, QuerySet from django.db.models import Model, QuerySet
from django.http import HttpRequest from django.http import HttpRequest
...@@ -59,14 +63,8 @@ def dt_show_toolbar(request: HttpRequest) -> bool: ...@@ -59,14 +63,8 @@ def dt_show_toolbar(request: HttpRequest) -> bool:
def get_app_packages() -> Sequence[str]: def get_app_packages() -> Sequence[str]:
"""Find all packages within the aleksis.apps namespace.""" """Find all registered apps from the setuptools entrypoint."""
# Import error are non-fatal here because probably simply no app is installed. return [f"{ep.module}.{ep.attr}" for ep in metadata.entry_points()["aleksis.app"]]
try:
import aleksis.apps
except ImportError:
return []
return [f"aleksis.apps.{pkg[1]}" for pkg in pkgutil.iter_modules(aleksis.apps.__path__)]
def merge_app_settings( def merge_app_settings(
...@@ -81,11 +79,19 @@ def merge_app_settings( ...@@ -81,11 +79,19 @@ def merge_app_settings(
Note: Only selected names will be imported frm it to minimise impact of Note: Only selected names will be imported frm it to minimise impact of
potentially malicious apps! potentially malicious apps!
""" """
for pkg in get_app_packages(): for app in get_app_packages():
try: pkg = ".".join(app.split(".")[:-2])
mod_settings = import_module(pkg + ".settings") mod_settings = None
except ImportError: while "." in pkg:
# Import errors are non-fatal. They mean that the app has no settings.py. try:
mod_settings = import_module(pkg + ".settings")
except ImportError:
# Import errors are non-fatal.
pkg = ".".join(pkg.split(".")[:-1])
continue
break
if not mod_settings:
# The app does not have settings
continue continue
app_setting = getattr(mod_settings, setting, None) app_setting = getattr(mod_settings, setting, None)
......
This diff is collapsed.
...@@ -88,6 +88,7 @@ django-health-check = "^3.12.1" ...@@ -88,6 +88,7 @@ django-health-check = "^3.12.1"
psutil = "^5.7.0" psutil = "^5.7.0"
celery-progress = "^0.0.14" celery-progress = "^0.0.14"
django-prometheus = "^2.1.0" django-prometheus = "^2.1.0"
importlib-metadata = {version = "^2.0.0", python = "<3.8"}
[tool.poetry.extras] [tool.poetry.extras]
ldap = ["django-auth-ldap"] ldap = ["django-auth-ldap"]
......
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