Fixes bug in forecast thread (close app while downloading). Also makes some style adjustments

This commit is contained in:
alessandro90
2019-05-01 19:50:03 +02:00
parent 9c77ac8278
commit 3826681685
13 changed files with 670 additions and 438 deletions

View File

@@ -3,12 +3,12 @@ from enum import Enum, auto
from io import BytesIO
import os.path
from shutil import rmtree
import urllib3
from zipfile import ZipFile
import aiohttp
import urllib3
from PyQt5.QtCore import QThread
from constants import Constants, Database, ChecksumWhat
from utilities import checksum_ok
import aiohttp
class ThreadStatus(Enum):
@@ -19,38 +19,41 @@ class ThreadStatus(Enum):
UNDEFINED = auto()
class DownloadThread(QThread):
def __init__(self):
super().__init__()
self.__status = ThreadStatus.UNDEFINED
self.reason = 0
@property
def status(self):
return self.__status
class _BaseDownloadThread(QThread):
def __init__(self, parent=None):
super().__init__(parent)
self.status = ThreadStatus.UNDEFINED
def __del__(self):
self.terminate()
self.wait()
super().__del__()
class DownloadThread(_BaseDownloadThread):
def __init__(self):
super().__init__()
self.reason = 0
def run(self):
self.status = ThreadStatus.UNDEFINED
try:
db = urllib3.PoolManager().request('GET', Database.LINK_LOC)
except urllib3.exceptions.MaxRetryError: # No internet connection.
self.__status = ThreadStatus.NO_CONNECTION_ERR
self.status = ThreadStatus.NO_CONNECTION_ERR
return
if db.status != 200:
self.reason = db.reason
self.__status = ThreadStatus.BAD_DOWNLOAD_ERR
self.status = ThreadStatus.BAD_DOWNLOAD_ERR
return
try:
is_checksum_ok = checksum_ok(db.data, ChecksumWhat.FOLDER)
except Exception:
self.__status = ThreadStatus.NO_CONNECTION_ERR
self.status = ThreadStatus.NO_CONNECTION_ERR
return
else:
if not is_checksum_ok:
self.__status = ThreadStatus.BAD_DOWNLOAD_ERR
self.status = ThreadStatus.BAD_DOWNLOAD_ERR
return
if os.path.exists(Constants.DATA_FOLDER):
rmtree(Constants.DATA_FOLDER)
@@ -58,25 +61,19 @@ class DownloadThread(QThread):
with ZipFile(BytesIO(db.data)) as zipped:
zipped.extractall()
except Exception:
self.__status = ThreadStatus.UNKNOWN_ERR
self.status = ThreadStatus.UNKNOWN_ERR
else:
self.__status = ThreadStatus.OK
self.status = ThreadStatus.OK
class UpadteSpaceWeatherThread(QThread):
class UpadteSpaceWeatherThread(_BaseDownloadThread):
__properties = ("xray", "prot_el", "ak_index", "sgas", "geo_storm")
def __init__(self, space_weather_data):
super().__init__()
self.__status = ThreadStatus.UNDEFINED
self.__space_weather_data = space_weather_data
@property
def status(self):
return self.__status
def __del__(self):
self.terminate()
self.wait()
async def __download_resource(self, session, link):
resp = await session.get(link)
return await resp.read()
@@ -92,18 +89,27 @@ class UpadteSpaceWeatherThread(QThread):
async def __download_resources(self, *links):
session = aiohttp.ClientSession()
properties = ("xray", "prot_el", "ak_index", "sgas", "geo_storm")
try:
t = [asyncio.create_task(self.__download_property(session, p)) for p in properties]
t = []
for p in self.__properties:
t.append(
asyncio.create_task(self.__download_property(session, p))
)
tot_images = range(len(Constants.FORECAST_IMGS))
t1 = [asyncio.create_task(self.__download_image(session, im_number)) for im_number in tot_images]
t1 = []
for im_number in tot_images:
t1.append(
asyncio.create_task(self.__download_image(session, im_number))
)
await asyncio.gather(*t, *t1)
except Exception:
self.__status = ThreadStatus.UNKNOWN_ERR
self.status = ThreadStatus.UNKNOWN_ERR
else:
self.__status = ThreadStatus.OK
self.status = ThreadStatus.OK
finally:
await session.close()
def run(self):
self.status = ThreadStatus.UNDEFINED
asyncio.run(self.__download_resources())