Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

fixing refresh

  • Loading branch information...
commit 17a517e4e0d9ccdb18ddc6ba9a37bc32dfc6fc3a 1 parent 92cad52
Zeb Palmer authored
7 docs/changelog.rst
View
@@ -2,16 +2,17 @@
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
* building python3 version at install, no longer maintaining separate code
* rewrite
* improved API organization
* improved documentation
* 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
@@ -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
* rather large changes to classes
15 scratchpad.py
View
@@ -1,7 +1,7 @@
#!/usr/bin/env python
-
+from time import sleep
from weatheralerts import WeatherAlerts
@@ -9,6 +9,13 @@
if __name__ == "__main__":
- nws = WeatherAlerts(state='ID')
- for alert in nws.alerts:
- print "{0}: {1}".format(alert.areadesc, alert.title)
+ nws = WeatherAlerts(state='ID', cachetime=1)
+ for alert in nws.alerts:
+ 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  setup.py
View
@@ -5,7 +5,7 @@
import sys
-versionstr = '0.5.0rc1'
+versionstr = '0.5.0rc2'
setup(
2  weatheralerts/cap.py
View
@@ -8,7 +8,7 @@ class CapParser(object):
'''
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):
1  weatheralerts/feed.py
View
@@ -37,6 +37,7 @@ def raw_cap(self, refresh=False):
'''
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
+ Note: you can force refresh here, if you do, don't also manually call refresh
'''
raw = self._get_feed_cache()
if raw is None or refresh is True:
2  weatheralerts/test_feed.py
View
@@ -5,7 +5,7 @@
class Test_Feed(unittest.TestCase):
def setUp(self):
- self.cf = AlertsFeed(maxage=60)
+ self.cf = AlertsFeed(maxage=5)
def test_refesh(self):
self.cf.raw_cap(refresh=True)
42 weatheralerts/test_weatheralerts.py
View
@@ -16,6 +16,12 @@ def test_event_state_counties(self):
def test_samecode_alerts_method(self):
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):
self.nws.county_state_alerts('canyon', 'ID')
@@ -37,22 +43,26 @@ def test_alert_attributes(self):
def test_passing_samecodes(self):
# Alerts by a Samecode
- nws = WeatherAlerts(samecodes='016027')
- nws = WeatherAlerts(samecodes=['016027', '016001', '016073', '016075'])
- for alert in nws.alerts:
- x = alert.title
- x = alert.summary
- x = alert.areadesc
- x = alert.event
- x = alert.samecodes
- x = alert.zonecodes
- x = alert.expiration
- x = alert.updated
- x = alert.effective
- x = alert.published
- x = alert.severity
- x = alert.category
- x = alert.urgency
+ testobjs = []
+ testobjs.append(WeatherAlerts(samecodes='016027'))
+ testobjs.append(WeatherAlerts(samecodes=['016027', '016001', '016073', '016075']))
+ samecodes = self.nws.geo.samecodes.keys() # get list of all known samecodes
+ testobjs.append(WeatherAlerts(samecodes=samecodes)) # use them for testing
+ for nws in testobjs:
+ for alert in nws.alerts:
+ x = alert.title
+ x = alert.summary
+ x = alert.areadesc
+ x = alert.event
+ x = alert.samecodes
+ x = alert.zonecodes
+ x = alert.expiration
+ x = alert.updated
+ x = alert.effective
+ x = alert.published
+ x = alert.severity
+ x = alert.category
+ x = alert.urgency
def test_passing_state(self):
nws = WeatherAlerts(state='ID')
21 weatheralerts/weather_alerts.py
View
@@ -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
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
load alerts.
@@ -19,6 +19,7 @@ def __init__(self, state=None, samecodes=None, load=True):
self.geo = GeoDB()
self.state = state
self.scope = 'US'
+ self.cachetime = cachetime
if samecodes is None:
self.samecodes = None
elif isinstance(samecodes, str):
@@ -38,13 +39,24 @@ def __init__(self, state=None, samecodes=None, load=True):
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
and ends up with a list of alerts object, which it stores to self._alerts
'''
- cap = AlertsFeed(state=self.scope).raw_cap()
- parser = CapParser(cap, geo=self.geo)
+ self._feed = AlertsFeed(state=self.scope, maxage=self.cachetime)
+ parser = CapParser(self._feed.raw_cap(), geo=self.geo)
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
def alerts(self):
if self.samecodes is not None:
@@ -72,7 +84,8 @@ def county_state_alerts(self, county, state):
return self.samecode_alerts(samecode)
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
for alert in self._alerts:
locations = []
Please sign in to comment.
Something went wrong with that request. Please try again.