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_035_parseTime_suppress_auto_month():
"""Check that explicit month suppresses automatic month rollback."""
next_day = tomorrow.day
if next_day > today.day:
last_year = today.year - 1
timestr = "%02d1651Z" % (next_day)
report = Metar.Metar("KEWR " + timestr, month=1)
assert report.decode_completed
assert report.time.day == next_day
assert report.time.month == 1
if today.month > 1:
assert report.time.year == today.year
else:
assert report.time.year == last_year
def test_020_parseStation_legal():
"""Check parsing of the station code."""
assert Metar.Metar("KEWR").station_id == "KEWR"
assert Metar.Metar("METAR KEWR").station_id == "KEWR"
assert Metar.Metar("METAR COR KEWR").station_id == "KEWR"
assert Metar.Metar("BIX1").station_id == "BIX1"
assert Metar.Metar("K256").station_id == "K256"
def test_020_parseStation_legal():
"""Check parsing of the station code."""
assert Metar.Metar("KEWR").station_id == "KEWR"
assert Metar.Metar("METAR KEWR").station_id == "KEWR"
assert Metar.Metar("METAR COR KEWR").station_id == "KEWR"
assert Metar.Metar("BIX1").station_id == "BIX1"
assert Metar.Metar("K256").station_id == "K256"
def report_nowind(vis_group):
"""(Macro) Return Metar object for a report containing the given
visibility group, without a preceeding wind group.
"""
return Metar.Metar(sta_time + vis_group)
def report(vis_group):
"""(Macro) Return Metar object for a report given visibility group."""
return Metar.Metar(sta_time + "09010KT " + vis_group)
def report(runway_state):
"""(Macro) Return Metar object for given runway state group"""
sample_metar = (
"EGNX 191250Z VRB03KT 9999 -RASN FEW008 SCT024 " "BKN046 M01/M03 Q0989 "
)
return Metar.Metar(sample_metar + " " + runway_state)
def test_031_parseTime_specify_year():
"""Check that the year can be specified."""
other_year = 2003
report = Metar.Metar("KEWR 101651Z", year=other_year)
assert report.decode_completed
assert report.time.year == other_year
def report(vis_group):
"""(Macro) Return Metar object for a report with the vis group."""
return Metar.Metar(sta_time + "09010KT " + vis_group)
def process_metar(mstr, now):
""" Do the METAR Processing """
mtr = None
while mtr is None:
try:
mtr = Metar(mstr, now.month, now.year)
except MetarParserError as exp:
try:
msg = str(exp)
except Exception:
return None
if msg.find("day is out of range for month") > 0 and now.day == 1:
now -= datetime.timedelta(days=1)
continue
tokens = ERROR_RE.findall(str(exp))
orig_mstr = mstr
if tokens:
for token in tokens[0].split():
mstr = mstr.replace(" %s" % (token,), "")
if orig_mstr == mstr:
print("Can't fix badly formatted metar: " + mstr)
return None
# try 3 times in case of download errors.
for i in range(3):
try:
read_str = urllib2.urlopen(URL % stnCode).read()
break
except:
_logger.info('Retry required in getWeatherObservation.')
# wait before retrying
time.sleep(1)
if 'read_str' not in locals():
# retries must have failed if there is no 'read_str' variable.
raise Exception('Could not access %s.' % stnCode)
obs = Metar.Metar('\n'.join( read_str.splitlines()[1:] )) # second line onward
_nws_cache.store(stnCode, obs)
return obs