mirror of
https://github.com/django-components/django-components.git
synced 2025-09-10 16:06:18 +00:00
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:
parent
107284f474
commit
0f41a62592
11 changed files with 647 additions and 63 deletions
|
@ -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
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue