Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_parse_JSON_fails_with_none_input(self):
instance = AggregatedMeasurementParser()
with self.assertRaises(parse_response_error.ParseResponseError):
instance.parse_JSON(None)
def test_parse_JSON_fails_with_none_input(self):
instance = StationParser()
with self.assertRaises(parse_response_error.ParseResponseError):
instance.parse_JSON(None)
def test_parse_JSON_when_wrong_time_operator(self):
instance = TriggerParser()
with self.assertRaises(parse_response_error.ParseResponseError):
instance.parse_JSON(self.test_trigger_wrong_operator_json)
json.dumps(d))
# Handle the case when no results are found
if 'cnt' in d and d['cnt'] == "0":
return []
else:
if 'list' in d:
try:
return [weather.weather_from_dictionary(item) \
for item in d['list']]
except KeyError:
raise parse_response_error.ParseResponseError(
''.join([__name__, ': impossible to read ' \
'weather info from JSON data'])
)
else:
raise parse_response_error.ParseResponseError(
''.join([__name__, ': impossible to read ' \
'weather list from JSON data'])
"""
Parses a *StationHistory* instance out of raw JSON data. Only certain
properties of the data are used: if these properties are not found or
cannot be parsed, an error is issued.
:param JSON_string: a raw JSON string
:type JSON_string: str
:returns: a *StationHistory* instance or ``None`` if no data is
available
:raises: *ParseResponseError* if it is impossible to find or parse the
data needed to build the result, *APIResponseError* if the JSON
string embeds an HTTP status error (this is an OWM web API 2.5 bug)
"""
if JSON_string is None:
raise parse_response_error.ParseResponseError('JSON data is None')
d = json.loads(JSON_string)
# Check if server returned errors: this check overcomes the lack of use
# of HTTP error status codes by the OWM API but it's supposed to be
# deprecated as soon as the API implements a correct HTTP mechanism for
# communicating errors to the clients. In addition, in this specific
# case the OWM API responses are the very same either when no results
# are found for a station and when the station does not exist!
measurements = {}
try:
if 'cod' in d:
if d['cod'] != "200":
raise api_response_error.APIResponseError(
"OWM API: error - response " + \
"payload: " + str(d))
if str(d['cnt']) == "0":
return None
def parse_JSON(self, JSON_string):
"""
Parses an *UVIndex* instance out of raw JSON data. Only certain
properties of the data are used: if these properties are not found or
cannot be parsed, an error is issued.
:param JSON_string: a raw JSON string
:type JSON_string: str
:returns: an *UVIndex* instance or ``None`` if no data is available
:raises: *ParseResponseError* if it is impossible to find or parse the
data needed to build the result, *APIResponseError* if the JSON
string embeds an HTTP status error (this is an OWM web API 2.5 bug)
"""
if JSON_string is None:
raise parse_response_error.ParseResponseError('JSON data is None')
d = json.loads(JSON_string)
try:
# -- reference time
reference_time = d['date']
# -- reception time (now)
reception_time = timeutils.now('unix')
# -- location
lon = float(d['lon'])
lat = float(d['lat'])
place = location.Location(None, lon, lat, None)
# -- UV intensity
uv_intensity = float(d['value'])
def parse_JSON(self, JSON_string):
"""
Parses an *NO2Index* instance out of raw JSON data. Only certain
properties of the data are used: if these properties are not found or
cannot be parsed, an error is issued.
:param JSON_string: a raw JSON string
:type JSON_string: str
:returns: an *NO2Index* instance or ``None`` if no data is available
:raises: *ParseResponseError* if it is impossible to find or parse the
data needed to build the result, *APIResponseError* if the JSON
string embeds an HTTP status error (this is an OWM web API 2.5 bug)
"""
if JSON_string is None:
raise parse_response_error.ParseResponseError('JSON data is None')
d = json.loads(JSON_string)
try:
# -- reference time (strip away Z and T on ISO8601 format)
t = d['time'].replace('Z', '+00').replace('T', ' ')
reference_time = timeformatutils._ISO8601_to_UNIXtime(t)
# -- reception time (now)
reception_time = timeutils.now('unix')
# -- location
lon = float(d['location']['longitude'])
lat = float(d['location']['latitude'])
place = location.Location(None, lon, lat, None)
# -- CO samples
no2_samples = [dict(label=key,
t = d['time'].replace('Z', '+00').replace('T', ' ')
reference_time = timeformatutils._ISO8601_to_UNIXtime(t)
# -- reception time (now)
reception_time = timeutils.now('unix')
# -- location
lon = float(d['location']['longitude'])
lat = float(d['location']['latitude'])
place = location.Location(None, lon, lat, None)
# -- SO2 samples
so2_samples = d['data']
except KeyError:
raise parse_response_error.ParseResponseError(
''.join([__name__, ': impossible to parse COIndex']))
return so2index.SO2Index(reference_time, place, None, so2_samples,
reception_time)
# -- reception time (now)
reception_time = timeutils.now('unix')
# -- location
lon = float(d['location']['longitude'])
lat = float(d['location']['latitude'])
place = location.Location(None, lon, lat, None)
# -- CO samples
no2_samples = [dict(label=key,
precision=d['data'][key]['precision'],
value=d['data'][key]['value']) for key in d['data']]
except KeyError:
raise parse_response_error.ParseResponseError(
''.join([__name__, ': impossible to parse NO2Index']))
return no2index.NO2Index(reference_time, place, None, no2_samples,
reception_time)
"""
Parses a list of *UVIndex* instances out of raw JSON data. Only certain
properties of the data are used: if these properties are not found or
cannot be parsed, an error is issued.
:param JSON_string: a raw JSON string
:type JSON_string: str
:returns: a list of *UVIndex* instances or an empty list if no data is
available
:raises: *ParseResponseError* if it is impossible to find or parse the
data needed to build the result, *APIResponseError* if the JSON
string embeds an HTTP status error (this is an OWM web API 2.5 bug)
"""
if JSON_string is None:
raise parse_response_error.ParseResponseError('JSON data is None')
d = json.loads(JSON_string)
uvindex_parser = UVIndexParser()
return [uvindex_parser.parse_JSON(json.dumps(item)) for item in d]