-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathvtec.py
More file actions
86 lines (71 loc) · 2.84 KB
/
vtec.py
File metadata and controls
86 lines (71 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
"""
VTEC product ingestor
The warnings table has the following timestamp based columns, this gets ugly
with watches. Lets try to explain
issue <- VTEC timestamp of when this event was valid for
expire <- When does this VTEC product expire
updated <- Product Timestamp of when a product gets updated
init_expire <- When did this product initially expire
product_issue <- When was this product issued by the NWS
"""
from functools import partial
import click
from pyiem.database import get_sqlalchemy_conn
from pyiem.nws.products.vtec import parser as vtecparser
from pyiem.nws.ugc import UGCProvider
from twisted.mail.smtp import SMTPSenderFactory
from pywwa import LOG, common
from pywwa.database import get_database, load_nwsli
from pywwa.ldm import bridge
SMTPSenderFactory.noisy = False
NWSLI_DICT = {}
def process_data(ugc_dict, pgconn, data):
"""Process the product"""
# Make sure we have a trailing $$, if not report error and slap one on
if data.find("$$") == -1:
common.email_error("No $$ Found!", data)
data += "\n\n$$\n\n"
# Create our TextProduct instance
text_product = vtecparser(
data,
utcnow=common.utcnow(),
ugc_provider=ugc_dict,
nwsli_provider=NWSLI_DICT,
)
# Don't parse these as they contain duplicated information
if text_product.source == "KNHC" and text_product.afos[:3] == "TCV":
return
# Skip spanish products
if text_product.source == "TJSJ" and text_product.afos[3:] == "SPN":
return
# This is sort of ambiguous as to what is best to be done when database
# writing is disabled. The web workflow will likely fail as well.
if common.dbwrite_enabled():
df = pgconn.runInteraction(text_product.sql)
df.addCallback(step2, text_product)
df.addErrback(common.email_error, text_product.unixtext)
df.addErrback(LOG.error)
else:
step2(None, text_product)
def step2(_dummy, text_product):
"""Callback after hopeful database work."""
if text_product.warnings:
common.email_error(
"\n\n".join(text_product.warnings), text_product.text
)
# Do the Jabber work necessary after the database stuff has completed
for plain, html, xtra in text_product.get_jabbers(
common.SETTINGS.get("pywwa_vtec_url", "pywwa_vtec_url"),
common.SETTINGS.get("pywwa_river_url", "pywwa_river_url"),
):
if xtra.get("channels", "") == "":
common.email_error("xtra[channels] is empty!", text_product.text)
common.send_message(plain, html, xtra)
@click.command(help=__doc__)
@common.init
def main(*args, **kwargs):
"""Go Main Go."""
load_nwsli(NWSLI_DICT)
with get_sqlalchemy_conn("postgis") as conn:
ugc_dict = UGCProvider(pgconn=conn)
bridge(partial(process_data, ugc_dict, get_database("postgis")))