feat: add a command to list all components (#1041)

* feat: add a command to list all components

* refactor: fix tests

* refactor: fix linter errors

* refactor: fix the tests for tests running within tox

* temp: print out test outputs

* refactor: fix tests for windows

* refactor: remove escape from slash?

* refactor: fixes to regex

* refactor: remove print statements

* docs: update API reference
This commit is contained in:
Juro Oravec 2025-03-19 09:38:25 +01:00 committed by GitHub
parent 107284f474
commit 0f41a62592
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 647 additions and 63 deletions

View file

@ -1,6 +1,9 @@
import re
import sys
from hashlib import md5
from typing import TYPE_CHECKING, Any, Callable, List, Optional, Type, TypeVar
from importlib import import_module
from types import ModuleType
from typing import TYPE_CHECKING, Any, Callable, List, Optional, Tuple, Type, TypeVar, Union
from django_components.util.nanoid import generate
@ -59,6 +62,31 @@ def get_import_path(cls_or_fn: Type[Any]) -> str:
return module + "." + cls_or_fn.__qualname__
def get_module_info(
cls_or_fn: Union[Type[Any], Callable[..., Any]],
) -> Tuple[Optional[ModuleType], Optional[str], Optional[str]]:
"""Get the module, module name and module file path where the class or function is defined."""
module_name: Optional[str] = getattr(cls_or_fn, "__module__", None)
if module_name:
if module_name in sys.modules:
module = sys.modules[module_name]
else:
try:
module = import_module(module_name)
except Exception:
module = None
else:
module = None
if module:
module_file_path: Optional[str] = getattr(module, "__file__", None)
else:
module_file_path = None
return module, module_name, module_file_path
def default(val: Optional[T], default: T) -> T:
return val if val is not None else default