mirror of
https://github.com/django-components/django-components.git
synced 2025-09-27 07:59:08 +00:00
refactor: add logger and move importlib one scope up
This commit is contained in:
parent
5ca4313238
commit
a08072c515
1 changed files with 27 additions and 12 deletions
|
@ -6,6 +6,7 @@ from pathlib import Path
|
||||||
import django
|
import django
|
||||||
from django.utils.module_loading import autodiscover_modules
|
from django.utils.module_loading import autodiscover_modules
|
||||||
|
|
||||||
|
from django_components.logger import logger
|
||||||
from django_components.utils import search
|
from django_components.utils import search
|
||||||
|
|
||||||
if django.VERSION < (3, 2):
|
if django.VERSION < (3, 2):
|
||||||
|
@ -21,27 +22,41 @@ def autodiscover() -> None:
|
||||||
|
|
||||||
# Autodetect a <component>.py file in a components dir
|
# Autodetect a <component>.py file in a components dir
|
||||||
component_filepaths = search(search_glob="**/*.py").matched_files
|
component_filepaths = search(search_glob="**/*.py").matched_files
|
||||||
|
logger.debug(
|
||||||
|
f"Autodiscover found {len(component_filepaths)} files in component directories."
|
||||||
|
)
|
||||||
|
|
||||||
for path in component_filepaths:
|
for path in component_filepaths:
|
||||||
import_file(path)
|
# This imports the file and runs it's code. So if the file defines any
|
||||||
|
# django components, they will be registered.
|
||||||
|
module_name = _filepath_to_python_module(path)
|
||||||
|
logger.debug(f'Importing module "{module_name}" (derived from path "{path}")')
|
||||||
|
importlib.import_module(module_name)
|
||||||
|
|
||||||
for path_lib in app_settings.LIBRARIES:
|
for path_lib in app_settings.LIBRARIES:
|
||||||
importlib.import_module(path_lib)
|
importlib.import_module(path_lib)
|
||||||
|
|
||||||
|
|
||||||
def import_file(file_path: Path) -> None:
|
def _filepath_to_python_module(file_path: Path) -> str:
|
||||||
|
"""
|
||||||
|
Derive python import path from the filesystem path.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
- If project root is `/path/to/project`
|
||||||
|
- And file_path is `/path/to/project/app/components/mycomp.py`
|
||||||
|
- Then the path relative to project root is `app/components/mycomp.py`
|
||||||
|
- Which we then turn into python import path `app.components.mycomp`
|
||||||
|
"""
|
||||||
# Get the root dir, see https://stackoverflow.com/a/16413955/9788634
|
# Get the root dir, see https://stackoverflow.com/a/16413955/9788634
|
||||||
project_root = os.path.abspath(os.path.dirname(__name__))
|
project_root = os.path.abspath(os.path.dirname(__name__))
|
||||||
|
|
||||||
# Derive python import path from the filesystem path
|
|
||||||
# Example:
|
|
||||||
# If project root is `/path/to/project`
|
|
||||||
# And file_path is `/path/to/project/app/components/mycomp.py`
|
|
||||||
# Then the path relative to project root is `app/components/mycomp.py`
|
|
||||||
# Which we then turn into python import path `app.components.mycomp`
|
|
||||||
rel_path = os.path.relpath(file_path, start=project_root)
|
rel_path = os.path.relpath(file_path, start=project_root)
|
||||||
rel_path_without_suffix = str(Path(rel_path).with_suffix(""))
|
rel_path_without_suffix = str(Path(rel_path).with_suffix(""))
|
||||||
module_name = rel_path_without_suffix.replace(os.sep, ".")
|
|
||||||
|
|
||||||
# This imports the file and runs it's code. So if the file defines any
|
# NOTE: Path normalizes paths to use `/` as separator, while os.path
|
||||||
# django components, they will be registered.
|
# uses `os.path.sep`.
|
||||||
importlib.import_module(module_name)
|
sep = os.path.sep if os.path.sep in rel_path_without_suffix else "/"
|
||||||
|
module_name = rel_path_without_suffix.replace(sep, ".")
|
||||||
|
|
||||||
|
return module_name
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue