-
Notifications
You must be signed in to change notification settings - Fork 49
/
aurum_cli.py
executable file
·282 lines (225 loc) · 7.81 KB
/
aurum_cli.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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#! /usr/bin/env python3
import subprocess
from dataclasses import dataclass
from os import environ
from pathlib import Path
from warnings import warn
from knowledgerepr.ekgstore.neo4j_store import Neo4jExporter
from fire import Fire
import IPython
from main import init_system
run_cmd = subprocess.call
get_env = environ.get
class BaseAurumException(Exception):
pass
class DataSourceNotConfigured(BaseAurumException):
pass
class ModelNotFoundError(BaseAurumException):
pass
@dataclass()
class CSVDataSource:
name: str = 'NO NAME PROVIDED'
_path: Path = Path.cwd()
separator: str = ','
@property
def path(self):
return self._path
@path.setter
def path(self, p):
assert isinstance(p, Path) or isinstance(p, str)
self._path = Path(p)
def __dict__(self):
return {
'name': self.name,
'type': 'csv',
'config': {
'path': str(self.path),
'separator': self.separator
}
}
def to_yml(self):
return f"""api_version: 0
sources:
- name: "{self.name}"
type: csv
config:
path: "{str(self.path)}"
separator: '{self.separator}'"""
@dataclass()
class DBDataSource:
name: str = ''
host: str = ''
_port: int = ''
db_name: str = ''
db_user: str = ''
db_password: str = ''
_type: str = ''
@property
def port(self):
return self._port
@port.setter
def port(self, p):
self._port = int(p)
@property
def type(self):
return self._type
@type.setter
def type(self, s):
self._type = s
def __dict__(self):
return {
'name': self.name,
'type': self.type,
'config': {
'db_server_ip': self.host,
'db_server_port': self.port,
'database_name': self.db_name,
'db_username': self.db_user,
'db_password': self.db_password
}
}
def to_yml(self):
return f"""api_version: 0
sources:
- name: "{self.name}"
type: {self.type}
config:
db_server_ip: {self.host}
db_server_port: {self.port}
database_name: {self.db_name}
db_username: {self.db_user}
db_password: {self.db_password}"""
class AurumWrapper(object):
"""
Class that acts as a bridge between the current Aurum source code and an higher-level APIs (e.g. CLI and Web).
Relies heavily on filesystem CRUD interaction (e.g. relative paths) and thus should be removed once imports can be handled at the code level.
Eventually the FS will need to be replaced with something like SQLite to store data sources, track profilings etc.
"""
def __init__(self):
self.aurum_src_home = Path(get_env('AURUM_SRC_HOME', Path.cwd()))
self.ddprofiler_home = self.aurum_src_home.joinpath('ddprofiler')
self.ddprofiler_run_sh = self.ddprofiler_home.joinpath('run.sh')
self.aurum_home = Path(get_env('AURUM_HOME', Path.home().joinpath('.aurum')))
try:
self.aurum_home.mkdir(parents=True)
except FileExistsError:
pass
self.sources_dir = self.aurum_home.joinpath('sources')
try:
self.sources_dir.mkdir(parents=True)
except FileExistsError:
pass
self.models_dir = self.aurum_home.joinpath('models')
try:
self.models_dir.mkdir(parents=True)
except FileExistsError:
pass
def _make_ds_path(self, ds_name):
return self.sources_dir.joinpath(ds_name + '.yml')
def _make_model_path(self, model_name):
return self.models_dir.joinpath(model_name)
@property
def sources(self):
return [f.name.replace('.yml', '') for f in self.sources_dir.iterdir()]
@property
def models(self):
return [f.name for f in self.models_dir.iterdir()]
def _make_csv_ds(self, name, fp, separator=','):
return {
'name': name,
'type': 'csv',
'config': {
'path': fp,
'separator': separator
}
}
def _store_ds(self, ds):
with open(self._make_ds_path(ds.name), 'w') as f:
f.write(ds.to_yml())
class AurumCLI(AurumWrapper):
def __init__(self):
super().__init__()
@property
def sources(self):
return super().sources
def add_csv_data_source(self, name, path, sep=','):
ds = CSVDataSource()
ds.name = name
ds.path = path
ds.separator = sep
super()._store_ds(ds)
def add_db_data_source(self, name, db_type, host, port, db_name, username, password):
# TODO check if `db_type` is supported
ds = DBDataSource()
ds.name = name
ds.type = db_type
ds.host = host
ds.port = port
ds.db_name = db_name
ds.db_user = username
ds.db_password = password
super()._store_ds(ds)
def profile(self, data_source_name):
ds_fp = super()._make_ds_path(data_source_name)
if not ds_fp.exists():
raise DataSourceNotConfigured(f"Data Source {data_source_name} not configured!")
profile_cmd = ['bash', self.ddprofiler_run_sh, '--sources', ds_fp]
run_cmd(profile_cmd, cwd=self.ddprofiler_home)
def build_model(self, name):
model_dir_path = self._make_model_path(name)
try:
model_dir_path.mkdir(parents=True)
except FileExistsError:
warn(f'Model with the same name ({name}) already exists!')
run_cmd(['python', 'networkbuildercoordinator.py', '--opath', model_dir_path])
def export_model(self, model_name, to='neo4j', neo4j_host='localhost', neo4j_port=7687, neo4j_user='neo4j',
neo4j_pass='n304j'):
supported_destionations = ['neo4j']
if to not in supported_destionations:
raise NotImplementedError(f"Model destination not supported. Only {supported_destionations} are supported")
model_dir_path = self._make_model_path(model_name)
# Check if model exists
# TODO refactor to separate method
if not model_dir_path.exists():
available_models = '\n'.join(self.models)
raise ModelNotFoundError(
f"Model {model_name} not found!\nHere are the available ones:\n{available_models}")
# Hacky way. The underlying `fieldnetwork.py:deserialize_network` should be changed
model_path_str = model_dir_path.__str__() + '/'
if to == 'neo4j':
exporter = Neo4jExporter(host=neo4j_host, port=neo4j_port, user=neo4j_user, pwd=neo4j_pass)
exporter.export(model_path_str)
def clear_store(self):
"""
γφ
"""
from elasticsearch import Elasticsearch
# TODO extract AURUM_ES_HOST
es = Elasticsearch()
es.indices.delete('profile')
es.indices.delete('text')
def show_source(self, soure_name):
with open(self._make_ds_path(soure_name)) as f:
print(f.read())
def explore_model(self, model_name):
"""
Initiates an interactive IPython session to run discovery queries.
:param model_name:
:return:
"""
api, reporting = init_system(self._make_model_path(model_name).__str__() + '/', create_reporting=True)
IPython.embed()
if __name__ == '__main__':
aurum_cli = AurumCLI()
Fire({
'list-sources': aurum_cli.sources,
'add-csv': aurum_cli.add_csv_data_source,
'add-db': aurum_cli.add_db_data_source,
'show-source': aurum_cli.show_source,
'profile': aurum_cli.profile,
'build-model': aurum_cli.build_model,
'list-models': aurum_cli.models,
'export-model': aurum_cli.export_model,
'clear-store': aurum_cli.clear_store,
'explore-model': aurum_cli.explore_model
})