@@ -28,29 +28,52 @@ def __getattr__(self, name):
28
28
__all__ = []
29
29
30
30
31
+ def _piper_was_touched_in_frame (frame_before = 1 ):
32
+ call_function_frame = inspect .currentframe ().f_back
33
+ frame = call_function_frame
34
+ for i in range (frame_before ):
35
+ frame = frame .f_back
36
+
37
+ result = False
38
+ f_locals = frame .f_locals
39
+ f_globals = frame .f_globals
40
+ all_variables = f_locals | f_globals
41
+
42
+ if all_variables .values ():
43
+ all_variables = [v for v in all_variables .values () if v is not None ]
44
+ if len (all_variables ) > 0 :
45
+ variables_have_piper_package = any ("piper" in v .__package__ \
46
+ for v in all_variables if hasattr (v , "__package__" ) and type (v .__package__ ) == str )
47
+ variables_have_piper_module = any ("piper" in v .__module__ \
48
+ for v in all_variables if hasattr (v , "__module__" ) and type (v .__module__ ) == str )
49
+ result = variables_have_piper_module | variables_have_piper_package
50
+
51
+ return result
52
+
53
+
54
+ def _from_piper_file_but_not_piper (name : str , globals = {}):
55
+ is_import_from_piper_source_code = "__file__" in globals and "piper/" in globals ["__file__" ]
56
+ not_piper_import = not ("piper" in name )
57
+ result = is_import_from_piper_source_code and not_piper_import
58
+
59
+ return result
60
+
61
+
31
62
def try_import (name , globals = {}, locals = {}, fromlist = [], level = 0 ):
32
63
"""
33
64
This import replace real Python import with fake import which returns warning only and PiperDummyModule.
34
65
This works for everything under piper/ frameworks files by filename but not for piper import (like piper.base)
35
66
And this also works for every file where you import something from piper firstly !
36
67
"""
37
- is_piper_imported_from_previous_module = False
38
- prev_module_locals = inspect .currentframe ().f_back .f_locals
39
- prev_module_globals = inspect .currentframe ().f_back .f_globals
40
- prev_modules = prev_module_locals | prev_module_globals
41
-
42
- if prev_modules .values ():
43
- prev_modules = [v for v in prev_modules .values () if v is not None and hasattr (v , "__module__" )]
44
- if len (prev_modules ) > 0 :
45
- is_piper_imported_from_previous_module = \
46
- any ("piper" in v .__module__ for v in prev_modules if type (v .__module__ ) == str )
68
+ if not (configuration .ignore_import_errors or configuration .safe_import_activated ):
69
+ logger .info ("Ignore import errors is off in Configuration and deactivated" )
70
+ return real_import (name , globals , locals , fromlist , level )
47
71
48
- is_import_from_piper_source_code = "__file__" in globals and "piper/" in globals ["__file__" ]
49
- not_piper_import = not ("piper" in name )
50
- is_from_source_but_not_piper = is_import_from_piper_source_code and not_piper_import
72
+ piper_was_touched_in_previous_frame = _piper_was_touched_in_frame (frame_before = 1 )
73
+ need_to_catch = piper_was_touched_in_previous_frame or _from_piper_file_but_not_piper (name , globals )
51
74
52
- if is_piper_imported_from_previous_module and is_from_source_but_not_piper :
53
- logger .info (f"Piper activates safe import for library { name } in piper file { globals ['__file__' ]} " )
75
+ if need_to_catch :
76
+ logger .info (f"Piper runs safe import for library { name } in piper file { globals ['__file__' ]} " )
54
77
try :
55
78
return real_import (name , globals , locals , fromlist , level )
56
79
except ImportError as e :
@@ -69,7 +92,7 @@ def try_import(name, globals={}, locals={}, fromlist=[], level=0):
69
92
real_import = builtins .__import__
70
93
71
94
72
- def set_ignore_import_errors (ignore : bool = True ):
95
+ def _set_import_functions (ignore : bool = True ):
73
96
if ignore :
74
97
builtins .__import__ = try_import
75
98
else :
@@ -89,12 +112,14 @@ def activate_safe_import():
89
112
90
113
"""
91
114
logger .info (f"Piper activates safe import" )
92
- set_ignore_import_errors (ignore = True )
115
+ configuration .safe_import_activated = True
116
+ _set_import_functions (ignore = True )
93
117
94
118
95
119
def deactivate_safe_import ():
96
120
logger .info (f"Piper deactivates safe import" )
97
- set_ignore_import_errors (ignore = False )
121
+ configuration .safe_import_activated = False
122
+ _set_import_functions (ignore = configuration .ignore_import_errors )
98
123
99
124
100
125
class safe_import :
0 commit comments