Skip to content

Commit

Permalink
FIX: DwdWeatherWarningsAPI: Exception handling when input data is None
Browse files Browse the repository at this point in the history
FIX: README.md: Typo
  • Loading branch information
stephan192 committed Apr 20, 2020
1 parent 26bac69 commit 541a593
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 22 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 1.0.1 (2020-04-20)
### Fixed
- DwdWeatherWarningsAPI: Exception handling when input data is None
- README.md: Typo

## 1.0.0 (2020-04-19)
### Added
- DwdWeatherWarningsAPI
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ Last update: 2020-04-18 17:57:29.274000+00:00
- **`__init__(identifier)`**
Create a new weather warnings API class instance

The `identifier` can either be a so called `warncell id` (int) or a `warncell name` (str). It is heavly advised to use `warncell id` because `warncell name` is not unique in some cases.
The `identifier` can either be a so called `warncell id` (int) or a `warncell name` (str). It is heavily advised to use `warncell id` because `warncell name` is not unique in some cases.
A list auf valid warncell ids and names can be found [here](https://www.dwd.de/DE/leistungen/opendata/help/warnungen/cap_warncellids_csv.html).
Some of the warncells are outdated but still listed. If init fails search the list for a similar sounding warncell.
Method `update()` is automatically called at the end of a successfull init.
Expand Down
50 changes: 30 additions & 20 deletions dwdwfsapi/weatherwarnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def convert_warning_data(data_in):
"""

# pylint: disable=too-many-branches
# pylint: disable=too-many-statements

weather_severity_mapping = {
"minor": 1,
Expand All @@ -38,26 +39,26 @@ def convert_warning_data(data_in):
"instruction": None,
"level": 0,
"parameters": None,
"color": None,
"color": "#000000",
}

# Convert data
if "onset" in data_in:
try:
data_out["start_time"] = ciso8601.parse_datetime(data_in["onset"])
except: # pylint: disable=bare-except
except: # pylint: disable=bare-except
data_out["start_time"] = None
if "expires" in data_in:
try:
data_out["end_time"] = ciso8601.parse_datetime(data_in["expires"])
except: # pylint: disable=bare-except
except: # pylint: disable=bare-except
data_out["end_time"] = None
if "event" in data_in:
data_out["event"] = data_in["event"]
if "ec_ii" in data_in:
try:
data_out["event_code"] = int(data_in["ec_ii"])
except: # pylint: disable=bare-except
except: # pylint: disable=bare-except
data_out["event_code"] = 0
if "headline" in data_in:
data_out["headline"] = data_in["headline"]
Expand All @@ -66,23 +67,32 @@ def convert_warning_data(data_in):
if "instruction" in data_in:
data_out["instruction"] = data_in["instruction"]
if "severity" in data_in:
if data_in["severity"].lower() in weather_severity_mapping:
data_out["level"] = weather_severity_mapping[
data_in["severity"].lower()
]
try:
if data_in["severity"].lower() in weather_severity_mapping:
data_out["level"] = weather_severity_mapping[
data_in["severity"].lower()
]
except: # pylint: disable=bare-except
data_out["level"] = 0
if "parametername" in data_in and "parametervalue" in data_in:
# Depending on the query the keys and values are either seperated by , or ;
if "," in data_in["parametername"]:
keys = data_in["parametername"].split(",")
values = data_in["parametervalue"].split(",")
else:
keys = data_in["parametername"].split(";")
values = data_in["parametervalue"].split(";")
data_out["parameters"] = dict(zip(keys, values))
try:
if "," in data_in["parametername"]:
keys = data_in["parametername"].split(",")
values = data_in["parametervalue"].split(",")
else:
keys = data_in["parametername"].split(";")
values = data_in["parametervalue"].split(";")
data_out["parameters"] = dict(zip(keys, values))
except: # pylint: disable=bare-except
data_out["parameters"] = None
if "ec_area_color" in data_in:
colors = data_in["ec_area_color"].split(" ")
data_out["color"] = f"#{int(colors[0]):02x}{int(colors[1]):02x}"
data_out["color"] += f"{int(colors[2]):02x}"
try:
colors = data_in["ec_area_color"].split(" ")
data_out["color"] = f"#{int(colors[0]):02x}{int(colors[1]):02x}"
data_out["color"] += f"{int(colors[2]):02x}"
except: # pylint: disable=bare-except
data_out["color"] = "#000000"

return data_out

Expand Down Expand Up @@ -274,7 +284,7 @@ def __parse_result(self, json_obj):
self.last_update = ciso8601.parse_datetime(
json_obj["timeStamp"]
)
except: # pylint: disable=bare-except
except: # pylint: disable=bare-except
self.last_update = datetime.datetime.now(
datetime.timezone.utc
)
Expand Down Expand Up @@ -315,7 +325,7 @@ def __parse_result(self, json_obj):
self.expected_warnings = expected_warnings
self.data_valid = True

except: # pylint: disable=bare-except
except: # pylint: disable=bare-except
self.data_valid = False
self.last_update = None
self.current_warning_level = None
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
EMAIL = "[email protected]"
AUTHOR = "stephan192"
REQUIRES_PYTHON = ">=3.6"
VERSION = "1.0.0"
VERSION = "1.0.1"

# Define required packages
REQUIRES = ["requests>=2.23.0,<3", "ciso8601>=2.1.3,<3", "urllib3>=1.25.8,<2"]
Expand Down

0 comments on commit 541a593

Please sign in to comment.