-
Notifications
You must be signed in to change notification settings - Fork 2
/
dal.py
executable file
·47 lines (35 loc) · 1.52 KB
/
dal.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
47
#!/usr/bin/env python3
'''
A helper script to provide backwards compatibility for pre-PIP package structure. Eventually will be removed altogether.
'''
from pathlib import Path
src = Path(__file__).absolute().parent / 'src'
NAME = min(src.glob('*/*.py')).parent.name # package name (e.g. rexport/hypexport etc)
import warnings
warnings.warn(f'This script is DEPRECATED. Please install the package directly (see https://github.com/karlicoss/{NAME}#setting-up)')
import sys
sys.path.insert(0, str(src))
module_name = Path(__file__).stem # export/dal
mod = f'{NAME}.{module_name}'
# unload previously loaded DAL module (i.e. this file)
if NAME in sys.modules: del sys.modules[NAME]
if mod in sys.modules: del sys.modules[mod ]
from contextlib import contextmanager
@contextmanager
def handle_submodule_error():
# todo this might also be useful in the actual dal/export files.. not sure
try:
yield
except ImportError as e:
import logging
logging.critical(f"[{__file__}]: Error while importing {mod}. Make sure you've used 'git clone --recursive' or 'git pull && git submodule update --init'.")
raise e
# see https://stackoverflow.com/questions/43059267/how-to-do-from-module-import-using-importlib
from importlib import import_module
with handle_submodule_error():
dal = import_module(mod)
names = [x for x in dal.__dict__ if not x.startswith("_")]
globals().update({k: getattr(dal, k) for k in names})
if __name__ == '__main__':
with handle_submodule_error():
main() # type: ignore