forked from JostTim/pGenUtils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_dependancies.py
More file actions
123 lines (96 loc) · 4.98 KB
/
_dependancies.py
File metadata and controls
123 lines (96 loc) · 4.98 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import warnings, os, inspect
class color:
PURPLE = '\033[95m'
CYAN = '\033[96m'
DARKCYAN = '\033[36m'
BLUE = '\033[94m'
GREEN = '\033[92m'
YELLOW = '\033[93m'
RED = '\033[91m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
END = '\033[0m'
_called_dep_message = f"""The function : {color.BOLD + color.BLUE}{'{caller_function_name}'}(){color.END}
of the submodule : {'{caller_file_name}'} of pGenUtils, requires the optionnal package : {'{package_name}'}
"""
_install_dep_message = """If required, you can install {package_name} in your environment with :
{package_install_command}\n
({package_website})\n
"""
_missing_dep_warn_message = """The package {package_name} is missing and required for some functions of the submodule {caller_file_name} of pGenUtils.
You won't be able to use these functions.\n
"""
_dependancies_data = {"default" :
{"installs" : None,
"website" : None
},
"rasterio":
{"installs" : ["conda install rasterio","pip install rasterio"],
"website" : "https://rasterio.readthedocs.io/en/latest/installation.html",
},
"sqlalchemy":
{"installs" : ["conda install -c anaconda sqlalchemy","pip install SQLAlchemy"],
"website" : "https://www.sqlalchemy.org/library.html#tutorials"},
"pandas" :
{"installs" : ["conda install -c anaconda pandas","pip install pandas"],
"website" : "https://pandas.pydata.org/getting_started.html"}
}
def get_outside_caller_info():
"""
Iteratively scans the caller's parents and stops at first encounter of a function coming from another file than this one (the deepest outside caller)
Returns it' file name and the function that "asked" for a raise if a module wasn't present.
Returns:
dict
"""
sdepth = 2
current_file_name = os.path.basename(inspect.stack()[0][1])
for depth in range(sdepth,len(inspect.stack())):
temp_filename = os.path.basename(inspect.stack()[depth][1])
if current_file_name != temp_filename:
return { "caller_function_name" : inspect.stack()[depth][3], "caller_file_name" : temp_filename}
def called_dependancy_message(dep_placeholder):
message_format_pieces = {"package_name" : dep_placeholder.package_name}
message_format_pieces.update(get_outside_caller_info())
return _called_dep_message.format(**message_format_pieces)
def missing_dependancy_warning_message(dep_placeholder):
message_format_pieces = {"package_name" : dep_placeholder.package_name}
message_format_pieces.update(get_outside_caller_info())
return _missing_dep_warn_message.format(**message_format_pieces)
def install_dependancy_message(dep_placeholder):
if _dependancies_data[dep_placeholder.package_selector]["installs"] is None or _dependancies_data[dep_placeholder.package_selector]["website"] is None :
return ""
message_format_pieces = {"package_name" : dep_placeholder.package_name}
message_format_pieces["package_website"] = _dependancies_data[dep_placeholder.package_selector]["website"]
or_list = ["\nor\n"]*(len( _dependancies_data[dep_placeholder.package_selector]["installs"] )-1)+['']
message_format_pieces["package_install_command"] = ''.join( [ "\t- " + item + str(conj_coord) for item, conj_coord in zip(_dependancies_data[dep_placeholder.package_selector]["installs"],or_list) ] )
return _install_dep_message.format(**message_format_pieces)
def dep_miss_warning(dep_placeholder):
warnings.warn( missing_dependancy_warning_message(dep_placeholder) + str(dep_placeholder) + install_dependancy_message(dep_placeholder) )
def dep_miss_raising(dep_placeholder):
raise ImportError( called_dependancy_message(dep_placeholder) + str(dep_placeholder) + install_dependancy_message(dep_placeholder) )
class default_placeholder(ImportError):
def __init__(self, package_name , error = "" ):
super().__init__(error)
self.package_name = package_name
if not package_name in _dependancies_data.keys():
self.package_selector = "default"
else :
self.package_selector = package_name
def __bool__(self):
return False
class sql_placeholder(default_placeholder):
class _1objclass(object):
class _2objclass(object):
class _3objclass(object):
pass
Engine = _3objclass()
base = _2objclass()
engine = _1objclass()
def __init__(self, package_name, error ):
super().__init__(package_name, error)
def assert_not_imported(package,raising = True):
if not package :
if raising:
dep_miss_raising(package)
return True
return False