7
7
# language=python
8
8
install_code = """
9
9
# add devtools `debug` function to builtins
10
- import sys
11
- # we don't install here for pytest as it breaks pytest, it is
12
- # installed later by a pytest fixture
13
- if not sys.argv[0].endswith('pytest'):
14
- import builtins
15
- try:
16
- from devtools import debug
17
- except ImportError:
18
- pass
19
- else:
20
- setattr(builtins, 'debug', debug)
10
+ # we don't want to import devtools until it's required since it breaks pytest, hence this proxy
11
+ class DebugProxy:
12
+ def __init__(self):
13
+ self._debug = None
14
+
15
+ def _import_debug(self):
16
+ if self._debug is None:
17
+ from devtools import debug
18
+ self._debug = debug
19
+
20
+ def __call__(self, *args, **kwargs):
21
+ self._import_debug()
22
+ kwargs['frame_depth_'] = 3
23
+ return self._debug(*args, **kwargs)
24
+
25
+ def format(self, *args, **kwargs):
26
+ self._import_debug()
27
+ kwargs['frame_depth_'] = 3
28
+ return self._debug.format(*args, **kwargs)
29
+
30
+ def __getattr__(self, item):
31
+ self._import_debug()
32
+ return getattr(self._debug, item)
33
+
34
+ import builtins
35
+ setattr(builtins, 'debug', DebugProxy())
21
36
"""
22
37
23
38
@@ -27,12 +42,6 @@ def print_code() -> int:
27
42
28
43
29
44
def install () -> int :
30
- print ('[WARNING: this command is experimental, report issues at github.com/samuelcolvin/python-devtools]\n ' )
31
-
32
- if hasattr (builtins , 'debug' ):
33
- print ('Looks like devtools is already installed.' )
34
- return 0
35
-
36
45
try :
37
46
import sitecustomize # type: ignore
38
47
except ImportError :
@@ -48,7 +57,11 @@ def install() -> int:
48
57
else :
49
58
install_path = Path (sitecustomize .__file__ )
50
59
51
- print (f'Found path "{ install_path } " to install devtools into __builtins__' )
60
+ if hasattr (builtins , 'debug' ):
61
+ print (f'Looks like devtools is already installed, probably in `{ install_path } `.' )
62
+ return 0
63
+
64
+ print (f'Found path `{ install_path } ` to install devtools into `builtins`' )
52
65
print ('To install devtools, run the following command:\n ' )
53
66
print (f' python -m devtools print-code >> { install_path } \n ' )
54
67
if not install_path .is_relative_to (Path .home ()):
@@ -65,5 +78,5 @@ def install() -> int:
65
78
elif 'print-code' in sys .argv :
66
79
sys .exit (print_code ())
67
80
else :
68
- print (f'python-devtools v{ VERSION } , CLI usage: python -m devtools [ install|print-code] ' )
81
+ print (f'python-devtools v{ VERSION } , CLI usage: ` python -m devtools install|print-code` ' )
69
82
sys .exit (1 )
0 commit comments