feat: better error handling & feedback

This commit is contained in:
2024-02-09 17:18:52 +01:00
parent c752f0ab89
commit 1dc3a4671a
3 changed files with 39 additions and 7 deletions

View File

@@ -1,8 +1,26 @@
import glob
import datetime
import os
import tkinter
import tkinter.messagebox
from plyer import notification
SKIP_LINES = 13
IGNORE_MISSING = False
DTYPES_MISSING_NOTIFIED = []
def send_warning_ignore(dtype):
if dtype in DTYPES_MISSING_NOTIFIED:
return
else:
DTYPES_MISSING_NOTIFIED.append(dtype)
notification.notify(
title='Klimadaten EXCEL',
message='Datei fehlt für: {}'.format(dtype),
)
def cache_content(from_time, to_time, data, dtype):
@@ -38,6 +56,8 @@ def cache_content(from_time, to_time, data, dtype):
def generate(master_dir, from_time, to_time, cache_file, dtype):
global IGNORE_MISSING
if dtype == "lufttemperatur-aussen" or dtype == "luftfeuchte":
base_name = "/produkt_tu_stunde*.txt"
elif dtype == "windgeschwindigkeit" or dtype == "windrichtung":
@@ -56,10 +76,23 @@ def generate(master_dir, from_time, to_time, cache_file, dtype):
# read files
files = glob.glob(master_dir + base_name)
if not files:
raise ValueError("Keine DWD_Datei für {} in: {} gefunden. Bitte herunterladen und entpacken! https://www.dwd.de/DE/leistungen/klimadatendeutschland/klarchivstunden.html;jsessionid=C423E76B30D18F24C43F4E7E36744C8C.live21073?nn=16102".format(dtype, os.getcwd() + ", " + master_dir))
if IGNORE_MISSING:
send_warning_ignore(dtype)
return ""
description = "Keine DWD_Datei für {} gefunden!".format(dtype)
description += "\nAlle fehlenden Dateien ignorieren und weiter? ('Nein' bricht den Durchlauf ab)"
root = tkinter.Tk()
response = tkinter.messagebox.askyesno("Achtung", description)
root.withdraw()
if response:
send_warning_ignore(dtype)
IGNORE_MISSING=True
return ""
last_date = None
for fname in files:
start = None
@@ -89,6 +122,7 @@ def generate(master_dir, from_time, to_time, cache_file, dtype):
# parse date #
date = datetime.datetime.strptime(fulldate, "%Y%m%d%H")
last_date = date
# append data #
data.append((date, float(primary), float(secondary)))
@@ -105,6 +139,7 @@ def generate(master_dir, from_time, to_time, cache_file, dtype):
print(dtype, from_time, to_time)
# find a fitting frame #
to_time = last_date - datetime.timedelta(hours=12)
for start, end, data in timeframes:
if from_time >= start and to_time <= end:
return cache_content(from_time, to_time, data, dtype)