Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/django/utils/module_loading.py: 66%

52 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2023-07-17 14:22 -0600

1import copy 

2import os 

3import sys 

4from importlib import import_module 

5from importlib.util import find_spec as importlib_find 

6 

7 

8def cached_import(module_path, class_name): 

9 modules = sys.modules 

10 if module_path not in modules or ( 

11 # Module is not fully initialized. 

12 getattr(modules[module_path], "__spec__", None) is not None 

13 and getattr(modules[module_path].__spec__, "_initializing", False) is True 

14 ): 

15 import_module(module_path) 

16 return getattr(modules[module_path], class_name) 

17 

18 

19def import_string(dotted_path): 

20 """ 

21 Import a dotted module path and return the attribute/class designated by the 

22 last name in the path. Raise ImportError if the import failed. 

23 """ 

24 try: 

25 module_path, class_name = dotted_path.rsplit(".", 1) 

26 except ValueError as err: 

27 raise ImportError("%s doesn't look like a module path" % dotted_path) from err 

28 

29 try: 

30 return cached_import(module_path, class_name) 

31 except AttributeError as err: 

32 raise ImportError( 

33 'Module "%s" does not define a "%s" attribute/class' 

34 % (module_path, class_name) 

35 ) from err 

36 

37 

38def autodiscover_modules(*args, **kwargs): 

39 """ 

40 Auto-discover INSTALLED_APPS modules and fail silently when 

41 not present. This forces an import on them to register any admin bits they 

42 may want. 

43 

44 You may provide a register_to keyword parameter as a way to access a 

45 registry. This register_to object must have a _registry instance variable 

46 to access it. 

47 """ 

48 from django.apps import apps 

49 

50 register_to = kwargs.get("register_to") 

51 for app_config in apps.get_app_configs(): 

52 for module_to_search in args: 

53 # Attempt to import the app's module. 

54 try: 

55 if register_to: 55 ↛ 58line 55 didn't jump to line 58, because the condition on line 55 was never false

56 before_import_registry = copy.copy(register_to._registry) 

57 

58 import_module("%s.%s" % (app_config.name, module_to_search)) 

59 except Exception: 

60 # Reset the registry to the state before the last import 

61 # as this import will have to reoccur on the next request and 

62 # this could raise NotRegistered and AlreadyRegistered 

63 # exceptions (see #8245). 

64 if register_to: 64 ↛ 70line 64 didn't jump to line 70, because the condition on line 64 was never false

65 register_to._registry = before_import_registry 

66 

67 # Decide whether to bubble up this error. If the app just 

68 # doesn't have the module in question, we can ignore the error 

69 # attempting to import it, otherwise we want it to bubble up. 

70 if module_has_submodule(app_config.module, module_to_search): 70 ↛ 71line 70 didn't jump to line 71, because the condition on line 70 was never true

71 raise 

72 

73 

74def module_has_submodule(package, module_name): 

75 """See if 'module' is in 'package'.""" 

76 try: 

77 package_name = package.__name__ 

78 package_path = package.__path__ 

79 except AttributeError: 

80 # package isn't a package. 

81 return False 

82 

83 full_module_name = package_name + "." + module_name 

84 try: 

85 return importlib_find(full_module_name, package_path) is not None 

86 except ModuleNotFoundError: 

87 # When module_name is an invalid dotted path, Python raises 

88 # ModuleNotFoundError. 

89 return False 

90 

91 

92def module_dir(module): 

93 """ 

94 Find the name of the directory that contains a module, if possible. 

95 

96 Raise ValueError otherwise, e.g. for namespace packages that are split 

97 over several directories. 

98 """ 

99 # Convert to list because __path__ may not support indexing. 

100 paths = list(getattr(module, "__path__", [])) 

101 if len(paths) == 1: 

102 return paths[0] 

103 else: 

104 filename = getattr(module, "__file__", None) 

105 if filename is not None: 

106 return os.path.dirname(filename) 

107 raise ValueError("Cannot determine directory containing %s" % module)