Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1import os 

2import pkgutil 

3import sys 

4 

5from importlib import import_module 

6 

7 

8def get_path(module): 

9 if getattr(sys, 'frozen', False): 

10 # frozen 

11 

12 if getattr(sys, '_MEIPASS', False): 

13 # PyInstaller 

14 lib_dir = getattr(sys, '_MEIPASS') 

15 else: 

16 # others 

17 base_dir = os.path.dirname(sys.executable) 

18 lib_dir = os.path.join(base_dir, "lib") 

19 

20 module_to_rel_path = os.path.join(*module.__package__.split(".")) 

21 path = os.path.join(lib_dir, module_to_rel_path) 

22 else: 

23 # unfrozen 

24 path = os.path.dirname(os.path.realpath(module.__file__)) 

25 return path 

26 

27 

28def list_module(module): 

29 path = get_path(module) 

30 

31 if getattr(sys, '_MEIPASS', False): 

32 # PyInstaller 

33 return [name for name in os.listdir(path) 

34 if os.path.isdir(os.path.join(path, name)) and 

35 "__init__.py" in os.listdir(os.path.join(path, name))] 

36 else: 

37 return [name for _, name, is_pkg in pkgutil.iter_modules([path]) if is_pkg] 

38 

39 

40def find_available_locales(providers): 

41 available_locales = set() 

42 

43 for provider_path in providers: 

44 

45 provider_module = import_module(provider_path) 

46 if getattr(provider_module, 'localized', False): 

47 langs = list_module(provider_module) 

48 available_locales.update(langs) 

49 available_locales = sorted(available_locales) 

50 return available_locales 

51 

52 

53def find_available_providers(modules): 

54 available_providers = set() 

55 for providers_mod in modules: 

56 providers = [ 

57 '.'.join([providers_mod.__package__, mod]) 

58 for mod in list_module(providers_mod) if mod != '__pycache__' 

59 ] 

60 available_providers.update(providers) 

61 return sorted(available_providers)