/usr/share/pyshared/d_rats/cap.py is in d-rats 0.3.3-3ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | #!/usr/bin/python
#
# Copyright 2008 Dan Smith <dsmith@danplanet.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import libxml2
import urllib
import tempfile
import datetime
try:
from hashlib import md5
except ImportError:
print "Installing hashlib replacement hack"
from utils import ExternalHash as md5
def ev_cmp_exp(ev1, ev2):
if ev1.expires < ev2.expires:
return -1
else:
return 1
def ev_cmp_eff(ev1, ev2):
if ev1.effective < ev2.effective:
return -1
else:
return 1
FMT = "%Y-%m-%dT%H:%M:%S"
class CAPEvent(object):
def __init__(self):
self.category = None
self.event = None
self.urgency = None
self.severity = None
self.certainty = None
self.effective = None
self.expires = None
self.headline = None
self.description = None
# def __setattr__(self, name, val):
# if not hasattr(self, name):
# raise ValueError("No such attribute `%s'" % name)
#
# self.__dict__[name] = val
def from_libxml_node(self, infonode):
child = infonode.children
while child:
content = child.getContent().strip()
if child.name in ["effective", "expires"]:
content = datetime.datetime.strptime(content,
"%Y-%m-%dT%H:%M:%S")
if child.name in self.__dict__.keys():
self.__dict__[child.name] = content
child = child.next
def __str__(self):
return "%s (%s): %s..." % (self.headline,
self.expires.strftime(FMT),
self.description[:20])
def report(self):
return """
%s (%s - %s)
%s
""" % (self.headline,
self.effective.strftime(FMT),
self.expires.strftime(FMT),
self.description)
class CAPParser(object):
def __init__(self, filename):
doc = libxml2.parseFile(filename)
root = doc.children
self.events = []
hashes = []
child = root.children
while child:
if child.name == "info":
try:
ev = CAPEvent()
ev.from_libxml_node(child)
hash = md5()
hash.update(ev.description)
if hash.digest() not in hashes:
self.events.append(ev)
hashes.append(hash.digest())
except Exception, e:
print "Unable to parse CAP node: %s (%s)" % (child.name, e)
child = child.next
self.events.sort(ev_cmp_eff)
def expired_events(self):
return sorted([x for x in self.events if x.expires < datetime.datetime.now()],
ev_cmp_eff)
def unexpired_events(self):
return sorted([x for x in self.events if x.expires > datetime.datetime.now()],
ev_cmp_eff)
def events_expiring_after(self, date):
return sorted([x for x in self.events if x.expires > date],
ev_cmp_eff)
def events_effective_after(self, date):
return sorted([x for x in self.events if x.effective > date],
ev_cmp_eff)
class CAPParserURL(CAPParser):
def __init__(self, url):
tmpf = tempfile.NamedTemporaryFile()
name = tmpf.name
tmpf.close()
urllib.urlretrieve(url, name)
CAPParser.__init__(self, name)
if __name__ == "__main__":
import sys
#cp = CAPParser(sys.argv[1])
cp = CAPParserURL("http://www.weather.gov/alerts/fl.cap")
epoch = datetime.datetime(2008, 9, 29, 00, 59, 00)
c = 0
for i in cp.events_expiring_after(epoch):
print i.report()
c += 1
print "%i events" % c
|