-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
462 lines (419 loc) · 19.3 KB
/
main.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
import os
import sys
import json
import calendar
import webbrowser
import datetime as dt
from dateutil.relativedelta import relativedelta
from PyQt6.QtGui import QIcon
from PyQt6.QtWidgets import QApplication, QMainWindow, QMessageBox
from MainWindow_ui import Ui_MainWindow
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5 import NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure
from report import generate_report
import db_sqlite as db_sqlite
import db_postgresql as db_postgresql
import db_mysql as db_mysql
import db_mariadb as db_mariadb
import db_oracle as db_oracle
import db_mssql as db_mssql
import db_firebird as db_firebird
import db_mongodb as db_mongodb
import db_cassandra as db_cassandra
class Error_MessageBox_Window(QMessageBox):
def __init__(self, text_error, is_exit=True, parent=None):
super(Error_MessageBox_Window, self).__init__(parent)
dialog = QMessageBox.critical(self, "Error", text_error,
QMessageBox.StandardButton.Ok)
if dialog == QMessageBox.StandardButton.Ok and is_exit:
sys.exit()
class MplCanvas(FigureCanvas):
def __init__(self, parent=None, width=5, height=4, dpi=100):
fig = Figure(figsize=(width, height), dpi=dpi)
self.axes = fig.add_subplot(111)
fig.subplots_adjust(left=0.05, bottom=0.12, right=0.98, top=0.98)
super(MplCanvas, self).__init__(fig)
##################################
# MainWindow
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setupUi(self)
# connect
self.updateButton.clicked.connect(self.update_plot)
self.day_box.currentIndexChanged.connect(self.on_day_box_index_changed)
self.month_box.currentIndexChanged.connect(self.on_month_box_index_changed)
self.year_box.currentTextChanged.connect(self.on_year_box_value_changed)
self.loadButton.clicked.connect(self.on_loadButton_clicked_)
self.reportButton.clicked.connect(self.on_reportButton_clicked_)
# year
for x in range(5):
self.year_box.addItem(format(dt.date.today().year - x, ""))
# month
self.month_box.setCurrentIndex(dt.date.today().month - 1)
# day
self.day_box.setCurrentIndex(dt.date.today().day - 1)
# graf
self.canvas = MplCanvas(self, width=5, height=4, dpi=100)
self.navi_toolbar = NavigationToolbar(self.canvas, self)
self.verticalLayout_graf.addWidget(self.canvas)
self.verticalLayout_graf.addWidget(self.navi_toolbar)
self.update_plot()
self.show()
self.read_settings()
##########################################################################
# add curs db
##########################################################################
curr_code = self.curr_box.currentText()[:3]
#
#############
# sqlite
type_db = "SQLite"
self.find_settings(type_db)
is_enable = self.data_set_find[0]
if is_enable:
db_sqlite.add_db(self.data_db, curr_code, type_db)
#############
# postgresql
type_db = "PostgreSQL"
self.find_settings(type_db)
is_enable = self.data_set_find[0]
if is_enable:
db_postgresql.add_db(self.data_db, curr_code, self.data_set_find, type_db)
#############
# postgresql
type_db = "AuroraPostgreSQL"
self.find_settings(type_db)
is_enable = self.data_set_find[0]
if is_enable:
db_postgresql.add_db(self.data_db, curr_code, self.data_set_find, type_db)
#############
# mysql
type_db = "MySQL"
self.find_settings(type_db)
is_enable = self.data_set_find[0]
if is_enable:
db_mysql.add_db(self.data_db, curr_code, self.data_set_find, type_db)
#############
# mysql
type_db = "AuroraMySQL"
self.find_settings(type_db)
is_enable = self.data_set_find[0]
if is_enable:
db_mysql.add_db(self.data_db, curr_code, self.data_set_find, type_db)
#############
# mariadb
type_db = "MariaDB"
self.find_settings(type_db)
is_enable = self.data_set_find[0]
if is_enable:
db_mariadb.add_db(self.data_db, curr_code, self.data_set_find, type_db)
#############
# oracle
type_db = "Oracle"
self.find_settings(type_db)
is_enable = self.data_set_find[0]
if is_enable:
db_oracle.add_db(self.data_db, curr_code, self.data_set_find, type_db)
#############
# mssql
type_db = "MSSQL"
self.find_settings(type_db)
is_enable = self.data_set_find[0]
if is_enable:
db_mssql.add_db(self.data_db, curr_code, self.data_set_find, type_db)
#############
# azuresql
type_db = "AzureSQL"
self.find_settings(type_db)
is_enable = self.data_set_find[0]
if is_enable:
db_mssql.add_db(self.data_db, curr_code, self.data_set_find, type_db)
#############
# firebird
type_db = "Firebird"
self.find_settings(type_db)
is_enable = self.data_set_find[0]
if is_enable:
db_firebird.add_db(self.data_db, curr_code, self.data_set_find, type_db)
#############
# mongodb
type_db = "MongoDB"
self.find_settings(type_db)
is_enable = self.data_set_find[0]
if is_enable:
db_mongodb.add_db(self.data_db, curr_code, self.data_set_find, type_db)
#############
# cassandra
type_db = "Cassandra"
self.find_settings(type_db)
is_enable = self.data_set_find[0]
if is_enable:
db_cassandra.add_db(self.data_db, curr_code, self.data_set_find, type_db)
# read settings
def read_settings(self):
if not os.path.isfile("Settings.json"):
Error_MessageBox_Window(
text_error="File 'Settings.json' not found").show()
# Opening JSON file
f = open(file="Settings.json", mode="r", encoding="utf8")
self.data_settings = json.loads(f.read())
# Closing file
f.close()
# find settings
def find_settings(self, type_d = ""):
self.data_set_find = \
(self.get_json_key_present(self.data_settings, "Connection"
+ type_d, "IsEnable"), # 0
self.get_json_key_present(self.data_settings, "Connection"
+ type_d, "DBHost"), # 1
self.get_json_key_present(self.data_settings, "Connection"
+ type_d, "DBPort"), # 2
self.get_json_key_present(self.data_settings, "Connection"
+ type_d, "DBName"), # 3
self.get_json_key_present(self.data_settings, "Connection"
+ type_d, "DBUser"), # 4
self.get_json_key_present(self.data_settings, "Connection"
+ type_d, "DBPassword"), # 5
self.get_json_key_present(self.data_settings, "Connection"
+ type_d, "DBSchema"), # 6
self.get_json_key_present(self.data_settings, "Connection"
+ type_d, "DBInsertProcedure"), # 7
self.get_json_key_present(self.data_settings, "Connection"
+ type_d, "DBSelectView"), # 8
self.get_json_key_present(self.data_settings, "Connection"
+ type_d, "Trusted_Connection"), # 9
self.get_json_key_present(self.data_settings, "Connection"
+ type_d, "DriverPython"), # 10
self.get_json_key_present(self.data_settings, "Connection"
+ type_d, "Encrypt"), # 11
)
# check exists json key
def get_json_key_present(self, json, key, key2):
try:
return json[key][key2]
except KeyError:
# as er:
# print(er)
return ""
# calc data
def calc_data(self):
#
self.xdata = []
self.data_db = []
self.ydata = []
self.data_year = int(self.year_box.currentText()) - int(self.minus_year.value())
date_min = dt.datetime.today()
date_max = dt.datetime.today()
if self.check_day.isChecked():
date_now = dt.datetime.strptime(self.day_box.currentText()
+ dt.datetime.today().strftime(".%m.%Y"), '%d.%m.%Y').date()
date_min = date_now + dt.timedelta(days=int(self.minus_day.value()*-1))
date_max = date_now + dt.timedelta(days=int(self.plus_day.value()))
if self.check_month.isChecked():
date_now = dt.datetime.strptime(dt.datetime.today().strftime("%d.")
+ self.month_box.currentText()[:2]
+ dt.datetime.today().strftime(".%Y"), '%d.%m.%Y').date()
date_min = date_now + relativedelta(months=int(self.minus_month.value()*-1))
date_max = date_now + relativedelta(months=int(self.plus_month.value()))
if date_now.year > date_min.year:
corr_year_min = -1
else:
corr_year_min = 0
if date_max.year > date_now.year:
corr_year_max = 1
else:
corr_year_max = 0
# load data
try:
file_settings_ukr = 'Офіційний курс гривні щодо іноземних валют.json'
file_settings_eng = 'Official hrivnya exchange rates.json'
is_file_settings_ukr = True
is_file_settings_eng = True
if not os.path.isfile(file_settings_ukr):
is_file_settings_ukr = False
if not os.path.isfile(file_settings_eng):
is_file_settings_eng = False
if not is_file_settings_ukr and not is_file_settings_eng:
Error_MessageBox_Window(
text_error="File 'Офіційний курс гривні щодо іноземних валют.json"+
" or Official hrivnya exchange rates.json' not found").show()
# Opening JSON file
if is_file_settings_eng:
file_name = file_settings_eng
currency_code = 'Letter code'
date_code = 'Date'
rate_code = 'Official hrivnya exchange rates, UAH'
forc_code = 'Unit'
elif is_file_settings_ukr:
file_name = file_settings_ukr
currency_code = 'Код літерний'
date_code = 'Дата'
rate_code = 'Офіційний курс гривні, грн'
forc_code = 'Кількість одиниць'
f = open(file=file_name, mode="r", encoding="utf8")
data = json.loads(f.read())
# calc
for year_n in range(self.data_year, self.data_year +
int(self.minus_year.value()) + 1, 1):
data_x = []
data_y = []
for data_json in data:
if dt.datetime.strptime(date_min.strftime("%d.%m.")
+ str(year_n + corr_year_min), '%d.%m.%Y').date() \
<= dt.datetime.strptime(data_json[date_code],
'%d.%m.%Y').date() \
<= dt.datetime.strptime(date_max.strftime("%d.%m.")
+ str(year_n + corr_year_max), '%d.%m.%Y').date() \
and data_json[currency_code] == \
self.curr_box.currentText()[:3]:
data_x.append(data_json[date_code][:5])
data_y.append(data_json[rate_code]/data_json[forc_code])
self.data_db.append((dt.datetime.strptime(data_json[date_code],
'%d.%m.%Y').date(),
data_json[rate_code]/data_json[forc_code]))
self.xdata.append(data_x)
self.ydata.append(data_y)
# average
if self.check_average.isChecked():
ydata_temp = []
for mas in self.ydata:
if len(mas) > 0:
ydata_el = []
sum_average = sum(mas)/len(mas)
for el in mas:
ydata_el.append(1 if sum_average == 0 else el / sum_average)
ydata_temp.append(ydata_el)
else:
ydata_temp.append([])
self.ydata = ydata_temp
# Closing file
f.close()
except Exception as err:
print(err)
# update plot
def update_plot(self):
self.calc_data()
self.canvas.axes.cla() # Clear the canvas.
for num, mas in enumerate(self.ydata):
if len(mas) > 0:
self.canvas.axes.plot(self.xdata[num], mas, label=self.data_year)
self.canvas.axes.grid(linestyle = 'dashed')
if self.check_dot.isChecked():
for a,b in zip(self.xdata[num], mas): # подписи значений
self.canvas.axes.text(a, b, str(b))
self.data_year +=1
self.canvas.axes.legend(loc='upper center', bbox_to_anchor=(0.5, -0.05),
fancybox=True, shadow=True, ncol=5)
self.canvas.axes.tick_params(axis='x', labelsize=7)
self.canvas.draw()
# check day month year
def check_day_month_year(self):
self.is_check_day_month_year = True
m_day = self.day_box.currentIndex() + 1
m_month = self.month_box.currentIndex() + 1
m_year = int (self.year_box.currentText())
if m_month in (4,6,9,11) and m_day > 30:
Error_MessageBox_Window(
"Для указанного месяца указана некорректный день",
is_exit=False).show()
self.is_check_day_month_year = False
if m_month == 2:
if calendar.isleap(m_year):
if m_day > 29:
Error_MessageBox_Window(
"Для указанного месяца указана некорректный день",
is_exit=False).show()
self.is_check_day_month_year = False
else:
if m_day > 28:
Error_MessageBox_Window(
"Для указанного месяца указана некорректный день",
is_exit=False).show()
self.is_check_day_month_year = False
######################################
# event - day_box - currentIndexChanged
def on_day_box_index_changed(self):
self.check_day_month_year()
######################################
# event - month_box - currentIndexChanged
def on_month_box_index_changed(self):
self.check_day_month_year()
######################################
# event - year_box - currentTextChanged
def on_year_box_value_changed(self):
self.check_day_month_year()
######################################
# event - loadButton - Clicked
def on_loadButton_clicked_(self):
webbrowser.open("https://bank.gov.ua/control/uk/curmetal/currency/search/form/period")
######################################
# event - reportButton - Clicked
def on_reportButton_clicked_(self):
#
type_db = self.report_box.currentText()
self.find_settings(type_db)
is_enable = self.data_set_find[0]
if is_enable:
self_report = []
match type_db:
case "SQLite":
self_report = db_sqlite.load_data_report(type_db)
case "PostgreSQL":
self_report = db_postgresql.load_data_report(
self.data_set_find, type_db)
case "AuroraPostgreSQL":
self_report = db_postgresql.load_data_report(
self.data_set_find, type_db)
case "MySQL":
self_report = db_mysql.load_data_report(
self.data_set_find, type_db)
case "AuroraMySQL":
self_report = db_mysql.load_data_report(
self.data_set_find, type_db)
case "MariaDB":
self_report = db_mariadb.load_data_report(
self.data_set_find, type_db)
case "Oracle":
self_report = db_oracle.load_data_report(
self.data_set_find, type_db)
case "MSSQL":
self_report = db_mssql.load_data_report(
self.data_set_find, type_db)
case "AzureSQL":
self_report = db_mssql.load_data_report(
self.data_set_find, type_db)
case "Firebird":
self_report = db_firebird.load_data_report(
self.data_set_find, type_db)
case "MongoDB":
self_report = db_mongodb.load_data_report(
self.data_set_find, type_db)
case "Cassandra":
self_report = db_cassandra.load_data_report(
self.data_set_find, type_db)
case _:
pass
if not self_report == []:
generate_report(self_report, type_db)
else:
Error_MessageBox_Window(
"Формирование отчета для базы данных "
+ type_db + " невозможно, ошибка получения данных",
is_exit=False).show()
else:
Error_MessageBox_Window("Формирование отчета для базы данных "
+ type_db + " выключено", is_exit=False).show()
def center(self):
qr = self.frameGeometry()
cp = self.screen().availableGeometry().center()
qr.moveCenter(cp)
self.move(qr.topLeft())
# primary block code
app = QApplication(sys.argv)
window = MainWindow()
window.setWindowIcon(QIcon("icon.ico"))
window.center()
window.show()
sys.exit(app.exec())