-
Notifications
You must be signed in to change notification settings - Fork 3
/
ClassImportExportDialog.py
563 lines (500 loc) · 20 KB
/
ClassImportExportDialog.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
import os
import json
from qgis.PyQt.QtCore import *
from qgis.PyQt.uic import *
from qgis.PyQt.QtGui import QIcon
from qgis.core import *
from qgis.gui import *
from qgis.utils import *
from qgis.PyQt.QtWidgets import *
from datetime import datetime
from .Function import read_version
from .db.Check_tab import CheckTab
class ClassDlgExport(QDialog):
"""
Class allow to export schema
"""
def __init__(self, mgis):
QDialog.__init__(self)
self.mgis = mgis
self.mdb = self.mgis.mdb
self.iface = iface
self.ui = loadUi(
os.path.join(self.mgis.masplugPath, 'ui/ui_export.ui'), self)
self.cond_com = None
self.parent = {}
self.child = {}
self.list_ver = []
self.listeRuns = []
self.listeScen = {}
self.init_gui()
def init_gui(self):
"""
initialisation GUI
"""
liste_col = self.mdb.list_columns('runs')
self.cond_com = ('comments' in liste_col)
dico = self.mdb.select("runs", "", "date")
if self.cond_com:
for run, scen, date, comments in zip(dico["run"], dico["scenario"],
dico["date"],
dico["comments"]):
if run not in self.listeRuns:
self.listeRuns.append(run)
self.listeScen[run] = []
self.listeScen[run].append((scen, date, comments))
else:
for run, scen, date in zip(dico["run"], dico["scenario"],
dico["date"]):
if run not in self.listeRuns:
self.listeRuns.append(run)
self.listeScen[run] = []
self.listeScen[run].append((scen, date))
if len(self.listeRuns) > 0:
self.parent = {}
self.child = {}
for run in self.listeRuns:
self.parent[run] = QTreeWidgetItem(self.tw_runs)
self.parent[run].setText(0, run)
self.parent[run].setFlags(self.parent[run].flags() |
Qt.ItemIsTristate |
Qt.ItemIsUserCheckable)
lbl = QLabel('')
self.tw_runs.setItemWidget(self.parent[run], 2, lbl)
self.child[run] = {}
maxi = datetime(1900, 1, 1, 0, 0)
if self.cond_com:
for scen, date, comments in self.listeScen[run]:
self.child[run][scen] = QTreeWidgetItem(
self.parent[run])
self.child[run][scen].setFlags(
self.child[run][scen].flags() |
Qt.ItemIsUserCheckable)
self.child[run][scen].setText(0, scen)
self.child[run][scen].setCheckState(0, Qt.Unchecked)
lbl = QLabel("{:%d/%m/%Y %H:%M}".format(date))
self.tw_runs.setItemWidget(self.child[run][scen], 1,
lbl)
maxi = max(maxi, date)
lbl = QLabel(comments)
self.tw_runs.setItemWidget(self.child[run][scen], 2,
lbl)
else:
for scen, date in self.listeScen[run]:
self.child[run][scen] = QTreeWidgetItem(
self.parent[run])
self.child[run][scen].setFlags(
self.child[run][scen].flags() |
Qt.ItemIsUserCheckable)
self.child[run][scen].setText(0, scen)
self.child[run][scen].setCheckState(0, Qt.Unchecked)
lbl = QLabel("{:%d/%m/%Y %H:%M}".format(date))
self.tw_runs.setItemWidget(self.child[run][scen], 1,
lbl)
maxi = max(maxi, date)
lbl = QLabel("{:%d/%m/%Y %H:%M}".format(maxi))
self.tw_runs.setItemWidget(self.parent[run], 1, lbl)
self.b_export.clicked.connect(self.export)
self.b_cancel.clicked.connect(self.annule)
self.cb_all_exp_res.stateChanged.connect(self.state_changed)
def state_changed(self):
"""
:return:
"""
if self.cb_all_exp_res.isChecked():
self.tw_runs.setEnabled(False)
else:
self.tw_runs.setEnabled(True)
def export(self):
"""export results selected"""
selection = {}
if self.cb_all_exp_res.isChecked():
for run in self.listeRuns:
selection[run] = []
for tple in self.listeScen[run]:
selection[run].append("'{}'".format(tple[0]))
else:
for run in self.listeRuns:
if self.parent[run].checkState(0) > 0:
selection[run] = []
if self.cond_com:
for scen, date, comments in self.listeScen[run]:
if self.child[run][scen].checkState(0) > 1:
selection[run].append("'{}'".format(scen))
else:
for scen, date in self.listeScen[run]:
if self.child[run][scen].checkState(0) > 1:
selection[run].append("'{}'".format(scen))
file = self.choix_file()
if file != '':
self.mgis.add_info('**** Run Export ****')
# self.mdb.export_model(selection,file)
plug_ver = read_version(self.mgis.masplugPath)
self.mgis.task_exp = QgsTask.fromFunction('Export Schema',
self.task_export,
on_finished=self.completed,
tup=(selection, file,
plug_ver))
self.mgis.task_exp.taskCompleted.connect(self.del_task_exp)
self.mgis.task_exp.taskTerminated.connect(self.del_task_exp)
QgsApplication.taskManager().addTask(self.mgis.task_exp)
self.accept()
else:
return
def del_task_exp(self):
"""
Clean tastk_mas variable
:return:
"""
del self.mgis.task_exp
self.mgis.task_exp = None
def completed(self, exception):
"""this is called when run is finished. Exception is not None if run
raises an exception. Result is the return value of run."""
message_category = 'My tasks from a function'
self.mdb.ignor_schema = list()
if exception is None:
QgsMessageLog.logMessage(
'task completed',
message_category, Qgis.Info)
else:
QgsMessageLog.logMessage("Exception: {}".format(exception),
message_category, Qgis.Critical)
raise exception
def task_export(self, task, tup=None):
if tup:
selection, file, plug_ver = tup
else:
self.mgis.add_info('no transmitted variable')
return
self.mdb.export_model(selection, file, plug_ver)
return
def annule(self):
""""Cancel """
self.close()
def choix_file(self):
"""
Database Export function
:param self:
:return:
"""
# choix du fichier d'exportatoin
file_name_path, _ = QFileDialog.getSaveFileName(self, "saveFile",
os.path.join(
QDir.homePath(),
self.mdb.SCHEMA),
filter="JSON (*.json)")
return file_name_path
class ClassDlgImport(QDialog):
"""
Class allow to export schema
"""
def __init__(self, mgis):
QDialog.__init__(self)
self.mgis = mgis
self.mdb = self.mgis.mdb
self.iface = iface
self.ui = loadUi(
os.path.join(self.mgis.masplugPath, 'ui/ui_import.ui'), self)
self.jsfile = None
self.metadict = None
self.checkdict = {
'ver_plug': None,
'updat_plug': False,
'updat_db': False,
'pgsql_version': None,
'updat_pgsql': False,
'schema_name': None,
'exist_schema': False,
'err': 0
}
self.bool_ver_plug = True
self.bool_ver_postgres = True
self.bool_name_schema = True
self.init_gui()
def init_gui(self):
"""
initialisation GUI
"""
chkt = CheckTab(self, self.mdb)
self.list_ver = chkt.list_hist_version
# self.list_ver = [int(v.replace('.','')) for v in self.list_ver]
self.checkdict['ver_plug'] = read_version(self.mgis.masplugPath)
self.checkdict["pgsql_version"] = self.mdb.version_postgres()
self.txt_file.textChanged[str].connect(self.change_file)
self.btn_open_file.clicked.connect(self.get_file)
self.btn_cancel.clicked.connect(self.annule)
self.btn_valid.clicked.connect(self.import_db)
def change_file(self):
"""
check text
:return:
"""
txt = self.txt_file.text()
self.metadict = None
if os.path.isfile(txt):
self.read_meta(txt)
self.fill_gui()
else:
self.clean_gui()
def get_file(self):
"""
Get file name
:return:
"""
file_name_path, _ = QFileDialog.getOpenFileName(None,
'File Selection',
QDir.homePath(),
filter="JSON (*.json);File (*)")
if file_name_path != '':
self.txt_file.setText(file_name_path)
def read_meta(self, filein):
"""
Read meta data file
and fill the GUI
:param filein:
:return:
"""
with open(filein, 'r') as file:
self.metadict = json.load(file)
def clean_gui(self):
"""
clean gui
:return:
"""
self.txt_shem.setText('')
self.txt_vplug.setText('')
self.txt_vpgsql.setText('')
def fill_gui(self):
"""
fill_gui
:return:
"""
self.txt_shem.setText(self.metadict["schema_name"])
self.txt_vplug.setText(self.metadict["plugin_version"])
self.txt_vpgsql.setText('.'.join(
[str(v) for v in
self.conv_ver_psql(self.metadict["pgsql_version"])])
)
if self.check_metadata():
self.check_ver_plug()
self.check_name_schema()
self.check_ver_postgres()
self.txt_vplug.setStyleSheet('color: green')
self.txt_vpgsql.setStyleSheet('color: green')
self.txt_shem.setStyleSheet('color: green')
if self.checkdict['updat_plug']:
self.txt_vplug.setStyleSheet('color: red')
elif self.checkdict['updat_db']:
self.txt_vplug.setStyleSheet('color: orange')
if self.checkdict['updat_pgsql']:
self.txt_vpgsql.setStyleSheet('color: red')
if self.checkdict['exist_schema']:
self.txt_shem.setStyleSheet('color: orange')
if self.checkdict['err'] > 0:
txt = 'There is erreur in the metadata file (.json)\n'
if self.checkdict['err'] == 1:
txt += "The plugin version doesn't exist."
self.txt_vplug.setStyleSheet('color: red')
QMessageBox.critical(None, 'Erreur',
txt)
def check_metadata(self):
"""
check metadata file
:return:
"""
if self.metadict is None:
self.checkdict['err'] = 2
return False
else:
self.checkdict['err'] = 0
return True
def check_ver_plug(self):
"""
Fill checkdict
:return: True if import is possible if not False
"""
self.checkdict['updat_db'] = False
self.checkdict['updat_plug'] = False
vplug = [int(v) for v in self.metadict["plugin_version"].split('.')]
maxv = self.list_ver[-1]
maxv = [int(v) for v in maxv.split('.')]
if maxv == vplug:
return True
elif self.compa_ver(vplug, maxv):
self.checkdict['updat_plug'] = True
return False
# check in list
for val in self.list_ver:
elem = [int(v) for v in val.split('.')]
if elem == vplug:
self.checkdict['updat_db'] = True
return True
self.checkdict['err'] = 1
return False
def check_ver_postgres(self):
"""
check version of postgres
:return:
"""
self.checkdict['updat_pgsql'] = False
vpsql = self.conv_ver_psql(self.metadict["pgsql_version"])
vdb = self.conv_ver_psql(self.checkdict["pgsql_version"])
if not self.compa_ver(vdb, vpsql):
self.checkdict['updat_pgsql'] = True
def conv_ver_psql(self, ver):
"""
Convert psql version in list
:param ver: format of the psql version
:return: the version in list
"""
strlst = []
wow = []
newver = [c for c in ver]
newver.reverse()
cpt = 1
for i, val in enumerate(newver):
wow.append(val)
if cpt == 2:
cpt = 1
wow.reverse()
wow = int(''.join(wow))
strlst.append(wow)
wow = []
else:
cpt += 1
if wow != []:
wow.reverse()
wow = int(''.join(wow))
strlst.append(wow)
strlst.reverse()
return strlst
def compa_ver(self, vdb, vmeta):
"""
Comparaison of version
:param vdb: version of database
:param vmeta: version of importation
:return: True if vdb >= vmeta
"""
cond = False
if vdb[0] > vmeta[0]:
cond = True
else:
if vdb[1] > vmeta[1] and vdb[0] == vmeta[0]:
cond = True
else:
if vdb[2] >= vmeta[2] \
and vdb[0] == vmeta[0] \
and vdb[1] == vmeta[1]:
cond = True
return cond
def check_name_schema(self):
""" check if name exists yet"""
self.checkdict['exist_schema'] = False
lst_schema = self.mdb.list_schema()
if self.metadict["schema_name"] in lst_schema:
self.checkdict['exist_schema'] = True
def import_db(self):
"""
import of database
:return:
"""
txt = self.txt_file.text()
if txt != '' and os.path.isfile(txt):
self.jsfile = txt
else:
QMessageBox.warning(None, 'Warning',
"Please enter a valid filename?")
return
dir = os.path.dirname(self.jsfile)
file_name = os.path.splitext(os.path.basename(self.jsfile))[0]
psqlfile = os.path.join(dir, file_name + '.psql')
if not os.path.isfile(psqlfile):
QMessageBox.critical(None, 'Error',
"Not found {}".format(psqlfile))
return
# Impossible to import
bool_import = True
if self.checkdict['err'] > 0:
bool_import = False
# import impossible
txt = ''
if self.checkdict['err'] == 2:
txt = "Metafile isn't right."
QMessageBox.critical(None, 'Erreur',
"Import isn't possible.\n {}".format(txt))
return
elif self.checkdict['updat_plug']:
bool_import = False
QMessageBox.critical(None, 'Erreur',
"Import isn't possible, because the Plugin version is superior.")
return
elif self.checkdict['updat_pgsql']:
# import impossible
bool_import = False
QMessageBox.critical(None, 'Erreur',
"Import isn't possible, because the Postgres version is superior.")
return
# change name
if self.checkdict['exist_schema']:
bool_import = False
while not bool_import:
rep = QMessageBox.question(self, 'MessageBox',
"The {} schema already exists.\n "
"Do you want overwrite the schema ?".format(
self.metadict["schema_name"]),
QMessageBox.Yes | QMessageBox.No | QMessageBox.Cancel,
QMessageBox.Cancel)
if rep == QMessageBox.Yes:
bool_import = True
elif rep == QMessageBox.No:
newname, ok = QInputDialog.getText(self,
'new Model',
'Change the name of new model:')
if ok and newname != '':
self.metadict["schema_name"] = newname
self.check_name_schema()
if not self.checkdict['exist_schema']:
bool_import = True
else:
bool_import = False
else:
return
if bool_import:
self.metadict['jsfile'] = self.jsfile
self.metadict['psqlfile'] = psqlfile
self.mgis.task_imp = QgsTask.fromFunction('Import Schema',
self.task_import,
on_finished=self.completed,
tup=None)
self.mgis.task_imp.taskCompleted.connect(self.del_task_imp)
self.mgis.task_imp.taskTerminated.connect(self.del_task_imp)
QgsApplication.taskManager().addTask(self.mgis.task_imp)
self.accept()
return
def del_task_imp(self):
"""
Clean tastk_mas variable
:return:
"""
del self.mgis.task_imp
self.mgis.task_imp = None
def completed(self, exception):
"""this is called when run is finished. Exception is not None if run
raises an exception. Result is the return value of run."""
message_category = 'My tasks from a function'
self.mdb.ignor_schema = list()
if exception is None:
QgsMessageLog.logMessage(
'task completed',
message_category, Qgis.Info)
else:
QgsMessageLog.logMessage("Exception: {}".format(exception),
message_category, Qgis.Critical)
raise exception
def task_import(self, task, tup=None):
self.mdb.import_model(self.metadict)
return
def annule(self):
""""Cancel """
self.close()