def ask_if_download(self): """Check if the database is at its latest version. If a new database is available automatically start the download. If not ask if should download it anyway. If already downloading do nothing. Handle possible connection errors. """ if not self.download_window.isVisible(): db_path = os.path.join(Constants.DATA_FOLDER, Database.NAME) try: with open(db_path, "rb") as file_db: db = file_db.read() except Exception: self.download_db() else: try: is_checksum_ok = checksum_ok(db, ChecksumWhat.DB) except Exception: pop_up(self, title=Messages.NO_CONNECTION, text=Messages.NO_CONNECTION_MSG).show() else: if not is_checksum_ok: self.download_db() else: answer = pop_up(self, title=Messages.DB_UP_TO_DATE, text=Messages.DB_UP_TO_DATE_MSG, informative_text=Messages.DOWNLOAD_ANYWAY_QUESTION, is_question=True, default_btn=QMessageBox.No).exec() if answer == QMessageBox.Yes: self.download_db()
def _wrong_checksum(self, raw_data): """Verify the checksum of the downloaded data and set the status accordingly.""" try: is_checksum_ok = checksum_ok(raw_data, self._target.hash_code) except ValueError: # Invalid hash code. self.status = ThreadStatus.NO_CONNECTION_ERR return True else: if not is_checksum_ok: self.status = ThreadStatus.BAD_DOWNLOAD_ERR return True return False
def check_db_ver(self): """Check if the database is at its latest version. If a new database version is available, ask if it should be downloaded. If a new database version is not available display a message. If already downloading do nothing. Handle possible connection errors. """ if not self.download_window.isVisible(): db_path = os.path.join(Constants.DATA_FOLDER, Database.NAME) answer = None try: with open(db_path, "rb") as file_db: db = file_db.read() except Exception: answer = pop_up( self, title=Messages.NO_DB, text=Messages.NO_DB_AVAIL, informative_text=Messages.DOWNLOAD_NOW_QUESTION, is_question=True).exec() if answer == QMessageBox.Yes: self.download_db() else: try: is_checksum_ok = checksum_ok(db, get_db_hash_code()) except Exception: pop_up(self, title=Messages.NO_CONNECTION, text=Messages.NO_CONNECTION_MSG).show() else: if is_checksum_ok: pop_up(self, title=Messages.UP_TO_DATE, text=Messages.UP_TO_DATE_MSG).show() else: answer = pop_up( self, title=Messages.DB_NEW_VER, text=Messages.DB_NEW_VER_MSG, informative_text=Messages.DOWNLOAD_NOW_QUESTION, is_question=True).exec() if answer == QMessageBox.Yes: self.download_db()
def run(self): """Override QThread.run. Download the database, images and audio samples. Handle all possible exceptions. Also extract the files in the local folder.""" self.status = ThreadStatus.UNDEFINED self._db = None raw_data = bytes(0) sub_data = bytes(0) try: self._db = get_pool_manager().request('GET', Database.LINK_LOC, preload_content=False, timeout=4.0) start = perf_counter() prev_downloaded = 0 while True: try: data = self._db.read(self._CHUNK) except Exception: raise _SlowConnError else: delta = perf_counter() - start if not data: break raw_data += data sub_data += data # Emit a progress signal only if at least 1 MB has been downloaded. if len(raw_data) - prev_downloaded >= self._MEGA: prev_downloaded = len(raw_data) self.progress.emit(self._pretty_len(raw_data)) if delta >= self._DELTAT: self.speed_progress.emit( self._get_download_speed(sub_data, delta)) sub_data = bytes(0) start = perf_counter() if self._exit_call: self._exit_call = False self._db.release_conn() return except Exception as e: # No (or bad) internet connection. self._db.release_conn() if isinstance(e, _SlowConnError): self.status = ThreadStatus.SLOW_CONN_ERR else: self.status = ThreadStatus.NO_CONNECTION_ERR return if self._db.status != 200: self.status = ThreadStatus.BAD_DOWNLOAD_ERR return try: is_checksum_ok = checksum_ok(raw_data, ChecksumWhat.FOLDER) except Exception: # checksum_ok unable to connect to the reference. 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): rmtree(Constants.DATA_FOLDER) try: self.progress.emit(Constants.EXTRACTING_CODE) self.speed_progress.emit(Constants.ZERO_FINAL_SPEED) with ZipFile(BytesIO(raw_data)) as zipped: zipped.extractall() except Exception: self.status = ThreadStatus.UNKNOWN_ERR else: self.status = ThreadStatus.OK