Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/faker/utils/loading.py: 76%
36 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
1import pkgutil
2import sys
4from importlib import import_module
5from pathlib import Path
6from types import ModuleType
7from typing import List
10def get_path(module: ModuleType) -> str:
11 if getattr(sys, "frozen", False): 11 ↛ 14line 11 didn't jump to line 14, because the condition on line 11 was never true
12 # frozen
14 if getattr(sys, "_MEIPASS", False):
15 # PyInstaller
16 lib_dir = Path(getattr(sys, "_MEIPASS"))
17 else:
18 # others
19 lib_dir = Path(sys.executable).parent / "lib"
21 path = lib_dir.joinpath(*module.__package__.split(".")) # type: ignore
22 else:
23 # unfrozen
24 if module.__file__ is not None: 24 ↛ 27line 24 didn't jump to line 27, because the condition on line 24 was never false
25 path = Path(module.__file__).parent
26 else:
27 raise RuntimeError(f"Can't find path from module `{module}.")
28 return str(path)
31def list_module(module: ModuleType) -> List[str]:
32 path = get_path(module)
34 if getattr(sys, "_MEIPASS", False): 34 ↛ 36line 34 didn't jump to line 36, because the condition on line 34 was never true
35 # PyInstaller
36 return [file.parent.name for file in Path(path).glob("*/__init__.py")]
37 else:
38 return [name for _, name, is_pkg in pkgutil.iter_modules([str(path)]) if is_pkg]
41def find_available_locales(providers: List[str]) -> List[str]:
42 available_locales = set()
44 for provider_path in providers:
46 provider_module = import_module(provider_path)
47 if getattr(provider_module, "localized", False):
48 langs = list_module(provider_module)
49 available_locales.update(langs)
50 return sorted(available_locales)
53def find_available_providers(modules: List[ModuleType]) -> List[str]:
54 available_providers = set()
55 for providers_mod in modules:
56 if providers_mod.__package__: 56 ↛ 55line 56 didn't jump to line 55, because the condition on line 56 was never false
57 providers = [
58 ".".join([providers_mod.__package__, mod]) for mod in list_module(providers_mod) if mod != "__pycache__"
59 ]
60 available_providers.update(providers)
61 return sorted(available_providers)