-
Notifications
You must be signed in to change notification settings - Fork 0
/
hook_typeguard.py
46 lines (41 loc) · 1.58 KB
/
hook_typeguard.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/bin/python3
#> Imports
import os
import sys
import typeguard
from pathlib import Path
from importlib import util as iutil
#</Imports
# Find entrypoint
if ep := os.getenv('FLEXILYNX_ENTRYPOINT', None): p = Path(ep)
elif (p := Path('__entrypoint__.py')).exists(): pass
elif (p := Path('../__entrypoint__.py')).exists(): pass
else:
raise FileNotFoundError('Could not find __entrypoint__.py or ../__entrypoint__.py, maybe set FLEXILYNX_ENTRYPOINT in env?')
sys.path.append(p.parent.as_posix())
# Caching prevents TypeGuard instrumentation
sys.dont_write_bytecode = True
for f in p.parent.glob('**/__pycache__/*.pyc'):
f.unlink()
print(f'Unlinked cache: {f}')
# Load entrypoint
__entrypoint__ = iutil.spec_from_file_location('__entrypoint__', p.as_posix()) \
.loader.load_module()
#> Main >/
#typeguard.config.debug_instrumentation = True
class Finder(typeguard.TypeguardFinder):
INCLUDE = ('util', 'frameworks', 'FlexiLynx')
EXCLUDE = ()
def should_instrument(self, module_name: str):
if (not (any(module_name.startswith(i) for i in self.INCLUDE))
or any(module_name.startswith(e) for e in self.EXCLUDE)):
print(f'<TypeGuard Hook> Skipped {module_name!r}')
return False
print(f'<TypeGuard Hook> Check {module_name!r}')
return True
print('<TypeGuard Hook> Installing import hook')
typeguard.install_import_hook(cls=Finder)
print('<TypeGuard Hook> Chainloading __entrypoint__.__load__()')
__entrypoint__.__load__()
print('<TypeGuard Hook> Chainloading __entrypoint__.__setup__()')
__entrypoint__.__setup__()