Skip to content

Commit

Permalink
fixing refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
zebpalmer committed Mar 11, 2013
1 parent 92cad52 commit 17a517e
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 30 deletions.
7 changes: 4 additions & 3 deletions docs/changelog.rst
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@
Release Changes Release Changes
=============== ===============


Current Development is on the 5.0 version Current Development is on 5.x


**0.5.0** (*Current Release*)


**0.5.0**
* 100% Test Coverage * 100% Test Coverage
* building python3 version at install, no longer maintaining separate code * building python3 version at install, no longer maintaining separate code
* rewrite * rewrite
* improved API organization * improved API organization
* improved documentation * improved documentation
* relicensed under LGPLv3 * relicensed under LGPLv3
* reworked refresh logic so it'd actually work when running in a daemon. (thanks to Michael W. for bug report)




Older Versions Older Versions
Expand All @@ -21,7 +22,7 @@ Versions prior to 0.5.x are no longer supported. It is suggest you test and upgr






**v0.4.9** **v0.4.9**


* Last of the 4.x branch * Last of the 4.x branch
* rather large changes to classes * rather large changes to classes
Expand Down
15 changes: 11 additions & 4 deletions scratchpad.py
Original file line number Original file line Diff line number Diff line change
@@ -1,14 +1,21 @@
#!/usr/bin/env python #!/usr/bin/env python





from time import sleep
from weatheralerts import WeatherAlerts from weatheralerts import WeatherAlerts










if __name__ == "__main__": if __name__ == "__main__":
nws = WeatherAlerts(state='ID') nws = WeatherAlerts(state='ID', cachetime=1)
for alert in nws.alerts: for alert in nws.alerts:
print "{0}: {1}".format(alert.areadesc, alert.title) print "{0}: {1}".format(alert.areadesc, alert.title)

nws = WeatherAlerts(cachetime=1)
print len(nws.alerts)
print len(nws.alerts)
sleep(70)
nws.refresh(force=True)
print len(nws.alerts)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import sys import sys




versionstr = '0.5.0rc1' versionstr = '0.5.0rc2'




setup( setup(
Expand Down
2 changes: 1 addition & 1 deletion weatheralerts/cap.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class CapParser(object):
''' '''
Parses the xml from the alert feed, creates and returns a list of alert objects. Parses the xml from the alert feed, creates and returns a list of alert objects.
FIXME: This is slow, messy, and painful to look at. FIXME: This is slow, messy, and painful to look at. I'll be totally rewriting it shortly.
''' '''
def __init__(self, raw_cap, geo=None): def __init__(self, raw_cap, geo=None):
Expand Down
1 change: 1 addition & 0 deletions weatheralerts/feed.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def raw_cap(self, refresh=False):
''' '''
Raw xml(cap) of the the feed. If a valid cache is availible Raw xml(cap) of the the feed. If a valid cache is availible
it is used, else a new copy of the feed is grabbed it is used, else a new copy of the feed is grabbed
Note: you can force refresh here, if you do, don't also manually call refresh
''' '''
raw = self._get_feed_cache() raw = self._get_feed_cache()
if raw is None or refresh is True: if raw is None or refresh is True:
Expand Down
2 changes: 1 addition & 1 deletion weatheralerts/test_feed.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


class Test_Feed(unittest.TestCase): class Test_Feed(unittest.TestCase):
def setUp(self): def setUp(self):
self.cf = AlertsFeed(maxage=60) self.cf = AlertsFeed(maxage=5)


def test_refesh(self): def test_refesh(self):
self.cf.raw_cap(refresh=True) self.cf.raw_cap(refresh=True)
Expand Down
42 changes: 26 additions & 16 deletions weatheralerts/test_weatheralerts.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ def test_event_state_counties(self):
def test_samecode_alerts_method(self): def test_samecode_alerts_method(self):
self.nws.samecode_alerts('016027') self.nws.samecode_alerts('016027')


def test_refresh(self):
self.nws.refresh()

def test_refresh_forced(self):
self.nws.refresh(force=True)

def test_county_state_alerts(self): def test_county_state_alerts(self):
self.nws.county_state_alerts('canyon', 'ID') self.nws.county_state_alerts('canyon', 'ID')


Expand All @@ -37,22 +43,26 @@ def test_alert_attributes(self):


def test_passing_samecodes(self): def test_passing_samecodes(self):
# Alerts by a Samecode # Alerts by a Samecode
nws = WeatherAlerts(samecodes='016027') testobjs = []
nws = WeatherAlerts(samecodes=['016027', '016001', '016073', '016075']) testobjs.append(WeatherAlerts(samecodes='016027'))
for alert in nws.alerts: testobjs.append(WeatherAlerts(samecodes=['016027', '016001', '016073', '016075']))
x = alert.title samecodes = self.nws.geo.samecodes.keys() # get list of all known samecodes
x = alert.summary testobjs.append(WeatherAlerts(samecodes=samecodes)) # use them for testing
x = alert.areadesc for nws in testobjs:
x = alert.event for alert in nws.alerts:
x = alert.samecodes x = alert.title
x = alert.zonecodes x = alert.summary
x = alert.expiration x = alert.areadesc
x = alert.updated x = alert.event
x = alert.effective x = alert.samecodes
x = alert.published x = alert.zonecodes
x = alert.severity x = alert.expiration
x = alert.category x = alert.updated
x = alert.urgency x = alert.effective
x = alert.published
x = alert.severity
x = alert.category
x = alert.urgency


def test_passing_state(self): def test_passing_state(self):
nws = WeatherAlerts(state='ID') nws = WeatherAlerts(state='ID')
Expand Down
21 changes: 17 additions & 4 deletions weatheralerts/weather_alerts.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class WeatherAlerts(object):
Most interaction from users, scripts, etc will be through the api provided by this class. So, as we approach a Most interaction from users, scripts, etc will be through the api provided by this class. So, as we approach a
more stable project, the API in this class will also become more stable and maintain backwards compatibility. more stable project, the API in this class will also become more stable and maintain backwards compatibility.
''' '''
def __init__(self, state=None, samecodes=None, load=True): def __init__(self, state=None, samecodes=None, load=True, cachetime=3):
''' '''
init Alerts, default to National Feed, set state level samecodes or county codes the area in which you want to init Alerts, default to National Feed, set state level samecodes or county codes the area in which you want to
load alerts. load alerts.
Expand All @@ -19,6 +19,7 @@ def __init__(self, state=None, samecodes=None, load=True):
self.geo = GeoDB() self.geo = GeoDB()
self.state = state self.state = state
self.scope = 'US' self.scope = 'US'
self.cachetime = cachetime
if samecodes is None: if samecodes is None:
self.samecodes = None self.samecodes = None
elif isinstance(samecodes, str): elif isinstance(samecodes, str):
Expand All @@ -38,13 +39,24 @@ def __init__(self, state=None, samecodes=None, load=True):


def load_alerts(self): def load_alerts(self):
''' '''
NOTE: use refresh() instead of this, if you are just needing to refresh the alerts list
Gets raw xml (cap) from the Alerts feed, throws it into the parser Gets raw xml (cap) from the Alerts feed, throws it into the parser
and ends up with a list of alerts object, which it stores to self._alerts and ends up with a list of alerts object, which it stores to self._alerts
''' '''
cap = AlertsFeed(state=self.scope).raw_cap() self._feed = AlertsFeed(state=self.scope, maxage=self.cachetime)
parser = CapParser(cap, geo=self.geo) parser = CapParser(self._feed.raw_cap(), geo=self.geo)
self._alerts = parser.get_alerts() self._alerts = parser.get_alerts()


def refresh(self, force=False):
'''
Refresh the alerts list. set force=true to force pulling a new list from the NWS. if force=false (default)
it'll only pull a new list if the cached copy is expired. (see cachetime)
'''
if force is True:
self._feed.refresh()
self._alerts = CapParser(self._feed.raw_cap(), geo=self.geo).get_alerts()


@property @property
def alerts(self): def alerts(self):
if self.samecodes is not None: if self.samecodes is not None:
Expand Down Expand Up @@ -72,7 +84,8 @@ def county_state_alerts(self, county, state):
return self.samecode_alerts(samecode) return self.samecode_alerts(samecode)


def event_state_counties(self): def event_state_counties(self):
'''Return an event type and it's state(s) and counties (consolidated)''' '''DEPRICATED: this will be moved elsewhere or dropped in the near future, stop using it.
Return an event type and it's state(s) and counties (consolidated)'''
# FIXME: most of this logic should be moved to the alert instance and refactored # FIXME: most of this logic should be moved to the alert instance and refactored
for alert in self._alerts: for alert in self._alerts:
locations = [] locations = []
Expand Down

0 comments on commit 17a517e

Please sign in to comment.