|
| 1 | +import builtins |
| 2 | +from types import ModuleType |
| 3 | + |
| 4 | +from piper.utils.logger_utils import logger |
| 5 | + |
| 6 | + |
| 7 | +def _empty_import(): |
| 8 | + logger.error("Import Not Installed Yet!") |
| 9 | + raise ImportError |
| 10 | + |
| 11 | + |
| 12 | +real_import = _empty_import |
| 13 | + |
| 14 | + |
| 15 | +class PiperDummyModule(ModuleType): |
| 16 | + |
| 17 | + def __init__(self, name): |
| 18 | + super().__init__(name) |
| 19 | + logger.info(f"Piper emulates {name} module") |
| 20 | + |
| 21 | + def __getattr__(self, name): |
| 22 | + return PiperDummyModule(name) |
| 23 | + |
| 24 | + __all__ = [] |
| 25 | + |
| 26 | + |
| 27 | +def try_import(name, globals={}, locals={}, fromlist=[], level=0): |
| 28 | + """ |
| 29 | + This import replace real Python import with fake import which returns warning only and PiperDummyModule. |
| 30 | + """ |
| 31 | + try: |
| 32 | + return real_import(name, globals, locals, fromlist, level) |
| 33 | + except ImportError as e: |
| 34 | + logger.warning(f"Piper ignores ImportError and module {name} replaced by dummy module. ImportError: {e}") |
| 35 | + module = PiperDummyModule(name) |
| 36 | + return module |
| 37 | + |
| 38 | + |
| 39 | +""" |
| 40 | +Here Piper saves default Python *import* only. |
| 41 | +""" |
| 42 | +if builtins.__import__ != try_import: |
| 43 | + real_import = builtins.__import__ |
| 44 | + |
| 45 | + |
| 46 | +def set_ignore_import_errors(ignore: bool = True): |
| 47 | + if ignore: |
| 48 | + builtins.__import__ = try_import |
| 49 | + else: |
| 50 | + builtins.__import__ = real_import |
| 51 | + |
| 52 | + |
| 53 | +def activate_safe_import(): |
| 54 | + """ |
| 55 | + Activate piper safe import with try_import function. |
| 56 | + Piper needs safe import to ignore imports in Executors examples. |
| 57 | + For instance if you want to use Pandas in your CustomExecutor normally you have to *import pandas* |
| 58 | + But we don't want to install everything for every executor in default Python (where Piper is installed) |
| 59 | + For that you have to ignore every Executors dependencies. |
| 60 | +
|
| 61 | + Otherwise, you can wrap buy yourself every Executors import with try_import |
| 62 | + or you can use directly only *requirements* field in your CustomExecutor. |
| 63 | +
|
| 64 | + """ |
| 65 | + logger.info(f"Piper activates safe import") |
| 66 | + set_ignore_import_errors(ignore=True) |
| 67 | + |
| 68 | + |
| 69 | +def deactivate_safe_import(): |
| 70 | + logger.info(f"Piper deactivates safe import") |
| 71 | + set_ignore_import_errors(ignore=False) |
| 72 | + |
| 73 | + |
| 74 | +class safe_import: |
| 75 | + """ |
| 76 | + Context manager to activate safe import on some part of imports. |
| 77 | + For instance: |
| 78 | +
|
| 79 | + with safe_import(): |
| 80 | + import foo |
| 81 | + import bar |
| 82 | +
|
| 83 | + foo would be ignored and replaced by PiperDummyModule |
| 84 | + boo wouldn't be replaced (you can catch ImportError) |
| 85 | + """ |
| 86 | + |
| 87 | + def __enter__(self): |
| 88 | + activate_safe_import() |
| 89 | + |
| 90 | + def __exit__(self, type, value, traceback): |
| 91 | + deactivate_safe_import() |
0 commit comments