def test_180321_mst(): """Do we do the right thing with MST products whilst in DST""" tp = productparser(get_test_file("AFDMST.txt")) res = tp.get_jabbers("http://localhost") ans = ("PSR issues Area Forecast Discussion (AFD) at Mar 21, 5:15 AM " "MST http://localhost?pid=201803211215-KPSR-FXUS65-AFDPSR") assert res[0][0] == ans
def really_process_data(txn, buf): ''' Actually do some processing ''' utcnow = datetime.datetime.utcnow() utcnow = utcnow.replace(tzinfo=pytz.timezone("UTC")) # Create our TextProduct instance prod = productparser(buf, utcnow=utcnow, ugc_provider=ugc_dict, nwsli_provider=nwsli_dict) # Do the Jabber work necessary after the database stuff has completed for (plain, html, xtra) in prod.get_jabbers( common.settings.get('pywwa_product_url', 'pywwa_product_url')): if xtra.get('channels', '') == '': common.email_error("xtra[channels] is empty!", buf) if not MANUAL: jabber.sendMessage(plain, html, xtra) if DB_ON: # Insert into database product_id = prod.get_product_id() sqlraw = buf.replace("\015\015\012", "\n").replace("\000", "").strip() sql = """ INSERT into text_products(product, product_id) values (%s,%s) """ myargs = (sqlraw, product_id) if (len(prod.segments) > 0 and prod.segments[0].sbw): giswkt = ('SRID=4326;%s' ) % (MultiPolygon([prod.segments[0].sbw]).wkt, ) sql = """ INSERT into text_products(product, product_id, geom) values (%s,%s,%s) """ myargs = (sqlraw, product_id, giswkt) txn.execute(sql, myargs)
def test_wpc(): """Can we deal with the AWIPS ID in WPC products.""" tp = productparser(get_test_file("PMDSA.txt")) res = tp.get_jabbers("http://localhost") ans = ("WBC issues South America Forecast Discussion (PMD) at Jul 13, " "11:17 AM EDT http://localhost?pid=202007131517-KWBC-FXSA20-PMDSA") assert res[0][0] == ans
def test_200731_cvt(): """See that we handle CVT timezone products.""" tp = productparser(get_test_file("TCDAT5_CVT.txt")) res = tp.get_jabbers("http://localhost") ans = ("NHC issues TCDAT5 (TCD) at Jul 31, 8:00 PM CVT " "http://localhost?pid=202007312100-KNHC-WTNT45-TCDAT5") assert res[0][0] == ans
def test_170411_fakemnd(): """This RTP has a quasi-faked timestamp in the header causing error""" utcnow = utc(2017, 4, 10, 23, 50) tp = productparser(get_test_file("RTPSGX.txt"), utcnow=utcnow) res = utc(2017, 4, 10, 23, 30) assert tp.valid == res res = utc(2017, 4, 10, 23, 24) assert tp.wmo_valid == res
def test_180321_mst(): """Do we do the right thing with MST products whilst in DST""" tp = productparser(get_test_file('AFDMST.txt')) res = tp.get_jabbers("http://localhost") ans = ( "PSR issues Area Forecast Discussion (AFD) at Mar 21, 5:15 AM " "MST http://localhost?pid=201803211215-KPSR-FXUS65-AFDPSR") assert res[0][0] == ans
def test_RFD(self): """ Parse a RFD """ tp = productparser(get_file('RFDOAX.txt')) self.assertEqual(tp.get_channels()[0], 'RFDOAX') j = tp.get_jabbers('http://localhost') self.assertEqual(j[0][0], ( 'OAX issues Grassland Fire Danger ' '(RFD) http://localhost?pid=201501191010-KOAX-FNUS63-RFDOAX'))
def test_HWO(self): """ Parse a HWO """ tp = productparser(get_file('HWO.txt')) self.assertEqual(tp.get_channels()[0], 'HWOLOT') j = tp.get_jabbers('http://localhost') self.assertEqual(j[0][0], ( 'LOT issues Hazardous Weather Outlook ' '(HWO) http://localhost?pid=201301082123-KLOT-FLUS43-HWOLOT'))
def test_rfd(self): """ Parse a RFD """ tp = productparser(get_file('RFDOAX.txt')) self.assertEqual(tp.get_channels()[0], 'RFDOAX') j = tp.get_jabbers('http://localhost') self.assertEqual(j[0][0], ( 'OAX issues Grassland Fire Danger ' '(RFD) at Jan 19, 4:10 AM CST ...MODERATE FIRE DANGER TODAY... ' 'http://localhost?pid=201501191010-KOAX-FNUS63-RFDOAX'))
def test_hwo(): """ Parse a HWO """ tp = productparser(get_test_file("HWO.txt")) assert tp.get_channels()[0] == "HWOLOT" j = tp.get_jabbers("http://localhost") ans = ("LOT issues Hazardous Weather Outlook " "(HWO) at Jan 8, 3:23 PM CST " "http://localhost?pid=201301082123-KLOT-FLUS43-HWOLOT") assert j[0][0] == ans
def test_rfd(): """ Parse a RFD """ tp = productparser(get_test_file("RFDOAX.txt")) assert tp.get_channels()[0] == "RFDOAX" j = tp.get_jabbers("http://localhost") ans = ("OAX issues Grassland Fire Danger " "(RFD) at Jan 19, 4:10 AM CST ...MODERATE FIRE DANGER TODAY... " "http://localhost?pid=201501191010-KOAX-FNUS63-RFDOAX") assert j[0][0] == ans
def test_hwo(): """ Parse a HWO """ tp = productparser(get_test_file('HWO.txt')) assert tp.get_channels()[0] == 'HWOLOT' j = tp.get_jabbers('http://localhost') ans = ('LOT issues Hazardous Weather Outlook ' '(HWO) at Jan 8, 3:23 PM CST ' 'http://localhost?pid=201301082123-KLOT-FLUS43-HWOLOT') assert j[0][0] == ans
def test_rfd(): """ Parse a RFD """ tp = productparser(get_test_file('RFDOAX.txt')) assert tp.get_channels()[0] == 'RFDOAX' j = tp.get_jabbers('http://localhost') ans = ( 'OAX issues Grassland Fire Danger ' '(RFD) at Jan 19, 4:10 AM CST ...MODERATE FIRE DANGER TODAY... ' 'http://localhost?pid=201501191010-KOAX-FNUS63-RFDOAX') assert j[0][0] == ans
def test_hwo(): """ Parse a HWO """ tp = productparser(get_test_file('HWO.txt')) assert tp.get_channels()[0] == 'HWOLOT' j = tp.get_jabbers('http://localhost') ans = ( 'LOT issues Hazardous Weather Outlook ' '(HWO) at Jan 8, 3:23 PM CST ' 'http://localhost?pid=201301082123-KLOT-FLUS43-HWOLOT') assert j[0][0] == ans
def test_200731_bogus_timezone(): """Test that we don't bomb out with unknown timezone.""" tp = productparser( get_test_file("TCDAT5_CVT.txt").replace(" CVT ", " ZZT ")) res = tp.get_jabbers("http://localhost") ans = ("NHC issues TCDAT5 (TCD) at Jul 31, 8:00 PM ZZT " "http://localhost?pid=202007312000-KNHC-WTNT45-TCDAT5") assert res[0][0] == ans assert len(tp.warnings) == 1 assert tp.warnings[0] == "product timezone 'ZZT' unknown"
def do_generic(): print(""" <h3>NWS Local Office Products</h3> <table class="table table-bordered table-condensed"> <thead> <tr><td></td><th>AFOS PIL + Product Name</th><th>Directive</th> <th>Channel Templates Used</th></tr> </thead> """) for entry in GEN_PRODUCTS: afos = entry['afos'] if afos == '': continue try: v = productparser(get_data(afos), ugc_provider=ugc_dict, nwsli_provider=nwsli_dict) except Exception, exp: sys.stderr.write(str(exp)) sys.stderr.write(afos+"\n") continue j = v.get_jabbers("http://mesonet.agron.iastate.edu/p.php") jmsg = "" tweet = "" channels = [] for (_, html, xtra) in j: tweet += xtra['twitter'] + "<br />" jmsg += html for channel in xtra['channels'].split(","): if channel not in channels: channels.append(channel) channels.sort() print("""<tr><td><a name="channel_%s"/> <a id="%s_btn" class="btn btn-small" role="button" href="javascript: revdiv('%s');"><i class="glyphicon glyphicon-plus"></i></a> </td><td>%s</td><td><a href="%s">%s</a></td><td>%s</td></tr> <tr><td colspan="4"><div id="%s" style="display:none;"> <dl class="dl-horizontal"> %s <dt>Example Raw Text:</dt><dd><a href="http://mesonet.agron.iastate.edu/p.php?pid=%s">View Text</a></dd> <dt>Channels for Product Example:</dt><dd>%s</dd> <dt>XMPP Chatroom Example:</dt><dd>%s</dd> <dt>Twitter Example:</dt><dd>%s</dd> </dl> </div> </td> </tr> """ % (afos, afos, afos, prodDefinitions.get(afos, afos), D[entry['directive']], entry['directive'], " ".join(["<span class=\"badge\">%s</span>" % (s,) for s in entry['channels']]), afos, '<dt>Notes</dt><dd>%s</dd>' % (entry.get('notes'),) if 'notes' in entry else '', v.get_product_id(), " ".join(["<span class=\"badge\">%s</span>" % (s,) for s in channels]), jmsg, tweet))
def really_process_data(txn, buf): """ Actually do some processing """ utcnow = datetime.datetime.utcnow() utcnow = utcnow.replace(tzinfo=pytz.utc) # Create our TextProduct instance prod = productparser(buf, utcnow=utcnow, ugc_provider=ugc_dict, nwsli_provider=nwsli_dict) # Do the Jabber work necessary after the database stuff has completed for (plain, html, xtra) in prod.get_jabbers( common.SETTINGS.get("pywwa_product_url", "pywwa_product_url")): if xtra.get("channels", "") == "": common.email_error("xtra[channels] is empty!", buf) if not MANUAL: jabber.send_message(plain, html, xtra) if DB_ON: # Insert into database product_id = prod.get_product_id() sqlraw = buf.replace("\015\015\012", "\n").replace("\000", "").strip() sql = """ INSERT into text_products(product, product_id) values (%s,%s) """ myargs = (sqlraw, product_id) if prod.segments and prod.segments[0].sbw: giswkt = ("SRID=4326;%s") % (MultiPolygon([prod.segments[0].sbw ]).wkt, ) sql = """ INSERT into text_products(product, product_id, geom) values (%s,%s,%s) """ myargs = (sqlraw, product_id, giswkt) txn.execute(sql, myargs)
def test_180130_chst(): """Whoa, our offset for CHST appears to be wrong""" tp = productparser(get_test_file("CHST.txt")) res = utc(2018, 1, 30, 20, 12) assert tp.valid == res
def test_resent(self): """ Make sure we can tell a ...RESENT product """ tp = productparser(get_file('MWWBRO.txt')) self.assertTrue(tp.is_resent())
def test_first_flapping(): """Test for product crossing month backwards, prevent flapping test.""" # Scenario is utcnow is the first and the WMO header is > 25th utcnow = utc(2012, 6, 1) prod = productparser(get_test_file("SVRBMX.txt"), utcnow=utcnow) assert prod.valid == utc(2012, 5, 31, 23, 11)
def test_151024_cae(self): """Make sure this CAE product works and does not throw an UGC error""" tp = productparser(get_file('CAEIA.txt')) self.assertEquals(tp.afos, 'CAEIA')
def test_resent(): """ Make sure we can tell a ...RESENT product """ tp = productparser(get_test_file('MWWBRO.txt')) assert tp.is_resent()
def test_no_afos(): """Test product without AFOS/AWIPS ID.""" with pytest.raises(TextProductException): productparser(get_test_file("PMDSA.txt").replace("PMDSA ", ""))
def test_tropical_channels(): """Test the channels we have some products go into.""" prod = productparser(get_test_file("tropical/TCUCP1.txt")) j = prod.get_jabbers("") assert "TCUCP" in j[0][2]["channels"].split(",")
def test_151024_cae(): """Make sure this CAE product works and does not throw an UGC error""" tp = productparser(get_test_file('CAEIA.txt')) assert tp.afos == 'CAEIA'
def test_170411_fakemnd(self): """This RTP has a quasi-faked timestamp in the header causing error""" tp = productparser(get_file('RTPSGX.txt')) res = utc(2017, 4, 10, 23, 30) self.assertEqual(tp.valid, res)
def test_151024_cae(): """Make sure this CAE product works and does not throw an UGC error""" tp = productparser(get_test_file("CAEIA.txt")) assert tp.afos == "CAEIA"
def do_generic(): """Handle the generic case.""" print( """ <h3>NWS Local Office / National Products</h3> <table class="table table-bordered table-condensed"> <thead> <tr><td></td><th>AFOS PIL + Product Name</th><th>Directive</th> <th>Channel Templates Used</th></tr> </thead> """ ) for entry in GEN_PRODUCTS: afos = entry["afos"] if afos == "": continue try: v = productparser( get_data(afos), ugc_provider=ugc_dict, nwsli_provider=nwsli_dict, ) assert v.afos is not None except Exception as exp: sys.stderr.write(str(exp)) sys.stderr.write(afos + "\n") continue j = v.get_jabbers("https://mesonet.agron.iastate.edu/p.php") jmsg = "" tweet = "" channels = [] for (_, html, xtra) in j: tweet += xtra["twitter"] + "<br />" jmsg += html if isinstance(xtra["channels"], list): xtra["channels"] = ",".join(xtra["channels"]) for channel in xtra["channels"].split(","): if channel not in channels: channels.append(channel) channels.sort() print( """<tr><td><a name="channel_%s"/> <a id="%s_btn" class="btn btn-small" role="button" href="javascript: revdiv('%s');"><i class="fa fa-plus"></i></a> </td><td>%s (%s)</td><td><a href="%s">%s</a></td><td>%s</td></tr> <tr><td colspan="4"><div id="%s" style="display:none;"> <dl class="dl-horizontal"> %s <dt>Example Raw Text:</dt> <dd><a href="https://mesonet.agron.iastate.edu/p.php?pid=%s">View Text</a></dd> <dt>Channels for Product Example:</dt><dd>%s</dd> <dt>XMPP Chatroom Example:</dt><dd>%s</dd> <dt>Twitter Example:</dt><dd>%s</dd> </dl> </div> </td> </tr> """ % ( afos, afos, afos, prodDefinitions.get(afos, afos), afos, D[entry["directive"]], entry["directive"], " ".join( [ '<span class="badge">%s</span>' % (s,) for s in entry["channels"] ] ), afos, "<dt>Notes</dt><dd>%s</dd>" % (entry.get("notes"),) if "notes" in entry else "", v.get_product_id(), " ".join( ['<span class="badge">%s</span>' % (s,) for s in channels] ), jmsg, tweet, ) ) print("""</table>""")
def test_200913_dualtime(): """Process a HLS in two timezones, sigh.""" tp = productparser(get_test_file("HLS.txt")) assert not tp.warnings assert tp.z == "EDT"
def test_180130_chst(): """Whoa, our offset for CHST appears to be wrong""" tp = productparser(get_test_file('CHST.txt')) res = utc(2018, 1, 30, 20, 12) assert tp.valid == res
def test_170411_fakemnd(): """This RTP has a quasi-faked timestamp in the header causing error""" tp = productparser(get_test_file('RTPSGX.txt')) res = utc(2017, 4, 10, 23, 30) assert tp.valid == res
def test_resent(): """ Make sure we can tell a ...RESENT product """ tp = productparser(get_test_file("MWWBRO.txt")) assert tp.is_resent()