Fix checksum_ok if no internet connection

This commit is contained in:
alessandro90
2019-03-29 19:54:48 +01:00
parent 0d83bf4ab0
commit 94ac35071f
6 changed files with 127 additions and 110 deletions

View File

@@ -22,7 +22,7 @@ class Messages(object):
NO_DB = "No database"
DOWNLOAD_NOW_QUESTION = "Do you want to download it now?"
DOWNLOAD_ANYWAY_QUESTION = "Do you want to download it anyway?"
NO_CONNECTION = "No internet connection"
NO_CONNECTION = "No connection"
NO_CONNECTION_MSG = "Unable to establish an internet connection."
BAD_DOWNLOAD = "Something went wrong"
BAD_DOWNLOAD_MSG = "Something went wrong with the downaload.\nCheck your internet connection and try again."

View File

@@ -2,7 +2,7 @@ from PyQt5 import uic
from PyQt5.QtCore import Qt, pyqtSlot
from PyQt5.QtWidgets import QWidget
from threads import DownloadThread, ThreadStatus
from utilities import throwable_message
from utilities import pop_up
from constants import Messages
Ui_Download_window, _ = uic.loadUiType("download_db_window.ui")
@@ -20,11 +20,11 @@ class DownloadWindow(QWidget, Ui_Download_window):
)
self.everything_ok = True
self.no_internet_msg = throwable_message(self, title = Messages.NO_CONNECTION,
self.no_internet_msg = pop_up(self, title = Messages.NO_CONNECTION,
text = Messages.NO_CONNECTION_MSG,
connection = self.close)
self.bad_db_download_msg = throwable_message(self, title = Messages.BAD_DOWNLOAD,
self.bad_db_download_msg = pop_up(self, title = Messages.BAD_DOWNLOAD,
text = Messages.BAD_DOWNLOAD_MSG,
connection = self.close)

28
main.py
View File

@@ -32,7 +32,7 @@ from themes import Theme
from utilities import (checksum_ok,
uncheck_and_emit,
throwable_message,
pop_up,
connect_to,
filters_ok,
is_undef_freq,
@@ -497,10 +497,16 @@ class MyApp(QMainWindow, Ui_MainWindow):
except:
self.download_db()
else:
if not checksum_ok(db, constants.ChecksumWhat.DB):
try:
is_checksum_ok = checksum_ok(db, constants.ChecksumWhat.DB)
except:
pop_up(self, title = constants.Messages.NO_CONNECTION,
text = constants.Messages.NO_CONNECTION_MSG).show()
else:
if not is_checksum_ok:
self.download_db()
else:
answer = throwable_message(self, title = constants.Messages.DB_UP_TO_DATE,
answer = pop_up(self, title = constants.Messages.DB_UP_TO_DATE,
text = constants.Messages.DB_UP_TO_DATE_MSG,
informative_text = constants.Messages.DOWNLOAD_ANYWAY_QUESTION,
is_question = True,
@@ -517,17 +523,23 @@ class MyApp(QMainWindow, Ui_MainWindow):
with open(db_path, "rb") as file_db:
db = file_db.read()
except:
answer = throwable_message(self, title = constants.Messages.NO_DB,
answer = pop_up(self, title = constants.Messages.NO_DB,
text = constants.Messages.NO_DB_AVAIL,
informative_text = constants.Messages.DOWNLOAD_NOW_QUESTION,
is_question = True).exec()
else:
if checksum_ok(db, constants.ChecksumWhat.DB):
throwable_message(self, title = constants.Messages.DB_UP_TO_DATE,
try:
is_checksum_ok = checksum_ok(db, constants.ChecksumWhat.DB)
except:
pop_up(self, title = constants.Messages.NO_CONNECTION,
text = constants.Messages.NO_CONNECTION_MSG).show()
else:
if is_checksum_ok:
pop_up(self, title = constants.Messages.DB_UP_TO_DATE,
text = constants.Messages.DB_UP_TO_DATE_MSG).show()
else:
answer = throwable_message(self, title = constants.Messages.DB_NEW_VER,
answer = pop_up(self, title = constants.Messages.DB_NEW_VER,
text = constants.Messages.DB_NEW_VER_MSG,
informative_text = constants.Messages.DOWNLOAD_NOW_QUESTION,
is_question = True).exec()
@@ -552,7 +564,7 @@ class MyApp(QMainWindow, Ui_MainWindow):
names = names,)
except FileNotFoundError:
self.search_bar.setDisabled(True)
answer = throwable_message(self, title = constants.Messages.NO_DB,
answer = pop_up(self, title = constants.Messages.NO_DB,
text = constants.Messages.NO_DB_AVAIL,
informative_text = constants.Messages.DOWNLOAD_NOW_QUESTION,
is_question = True).exec()

View File

@@ -4,8 +4,7 @@ from PyQt5.QtWidgets import QAction
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtGui import QPixmap
import constants
from utilities import (throwable_message,
is_valid_html_color,)
from utilities import pop_up, is_valid_html_color
class ThemeConstants(object):
FOLDER = "themes"
@@ -81,12 +80,12 @@ class Theme(object):
try:
with open(os.path.join(
self.__theme_path,
os.path.basename(self.__theme_path).split('-')[1] + ThemeConstants.EXTENSION)) as stylesheet:
os.path.basename(self.__theme_path).split('-')[1] + ThemeConstants.EXTENSION), "r") as stylesheet:
style = stylesheet.read()
self.__parent.setStyleSheet(style)
self.__parent.download_window.setStyleSheet(style)
except FileNotFoundError:
throwable_message(self.__parent, title = ThemeConstants.THEME_NOT_FOUND,
pop_up(self.__parent, title = ThemeConstants.THEME_NOT_FOUND,
text = ThemeConstants.MISSING_THEME).show()
else:
icons_path = os.path.join(self.__theme_path, ThemeConstants.ICONS_FOLDER)
@@ -160,7 +159,7 @@ class Theme(object):
def initialize(self):
current_theme_file = os.path.join(ThemeConstants.FOLDER, ThemeConstants.CURRENT)
if os.path.exists(current_theme_file):
with open(current_theme_file) as current_theme_path:
with open(current_theme_file, "r") as current_theme_path:
theme_path = current_theme_path.read()
if theme_path != ThemeConstants.DEFAULT:
self.__apply(theme_path)

View File

@@ -42,7 +42,13 @@ class DownloadThread(QThread):
self.reason = db.reason
self.__status = ThreadStatus.BAD_DOWNLOAD_ERR
return
if not checksum_ok(db.data, constants.ChecksumWhat.FOLDER):
try:
is_checksum_ok = checksum_ok(db.data, constants.ChecksumWhat.FOLDER)
except:
self.__status = ThreadStatus.NO_CONNECTION_ERR
return
else:
if not is_checksum_ok:
self.__status = ThreadStatus.BAD_DOWNLOAD_ERR
return
if os.path.exists(constants.DATA_FOLDER):

View File

@@ -13,7 +13,7 @@ def uncheck_and_emit(button):
button.setChecked(False)
button.clicked.emit()
def throwable_message(cls, title, text,
def pop_up(cls, title, text,
informative_text = None,
connection = None,
is_question = False,
@@ -43,8 +43,8 @@ def checksum_ok(data, what):
try:
reference = read_csv(constants.Database.LINK_REF,
delimiter = constants.Database.DELIMITER).iat[-1, n]
except HTTPError:
return False
except:
raise
return code.hexdigest() == reference
def is_valid_html_color(color):
@@ -52,11 +52,11 @@ def is_valid_html_color(color):
def connect_to(events_to_connect, fun_to_connect, fun_args):
if fun_args:
for signal in events_to_connect:
signal.connect(partial(fun_to_connect, *fun_args))
for event in events_to_connect:
event.connect(partial(fun_to_connect, *fun_args))
else:
for signal in events_to_connect:
signal.connect(fun_to_connect)
for event in events_to_connect:
event.connect(fun_to_connect)
def filters_ok(spinbox, filter_unit, confidence, sign = 1):
band_filter = spinbox.value() * constants.CONVERSION_FACTORS[filter_unit.currentText()]