From: W. Trevor King Date: Sun, 30 Jun 2013 20:32:10 +0000 (-0400) Subject: test/aggregate.py: Add a Map class for persistent streams X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=3cf744a055063dd018e00c7f3c2689d5fc17c252;p=pycalendar.git test/aggregate.py: Add a Map class for persistent streams Using a class method as a processing function allows you to keep open file handles (and similar processor state) between feeds. --- diff --git a/test/aggregate.py b/test/aggregate.py index d2cd738..f37c458 100755 --- a/test/aggregate.py +++ b/test/aggregate.py @@ -7,7 +7,7 @@ This script grabs all the ``.ics`` files in the ``test/`` directory and aggregates them into a single iCalendar feed. While it's doing this, it also prints and geographic positions to stderr. In a live -site, you could use a different version of ``get_geo`` to build +site, you could use a different version of ``add_event`` to build javascript that renders a map showing event locations. """ @@ -18,11 +18,18 @@ import pycalendar.aggregator as _pycalendar_aggregator import pycalendar.feed as _pycalendar_feed -def get_geo(feed): - for event in feed['VEVENT']: +class Map (list): + def __init__(self, stream=_sys.stderr): + self.stream = stream + + def add_feed(self, feed): + for event in feed['VEVENT']: + self.add_event(event=event) + + def add_event(self, event): if 'GEO' in event: lat,lon = [float(x) for x in event['GEO'].split(';')] - _sys.stderr.write('{} at lat {}, lon {}\n'.format( + self.stream.write('{} at lat {}, lon {}\n'.format( event['UID'], lat, lon)) @@ -35,17 +42,17 @@ def get_urls(root=_os.path.dirname(__file__)): yield 'file://{}'.format(path.replace(_os.sep, '/')) -def aggregate(): +def aggregate(**kwargs): aggregator = _pycalendar_aggregator.Aggregator( prodid='-//pycalendar//NONSGML testing//EN', feeds=[_pycalendar_feed.Feed(url=url) for url in get_urls()], - processors=[get_geo], - ) + **kwargs) aggregator.fetch() return aggregator if __name__ == '__main__': - aggregator = aggregate() + geomap = Map() + aggregator = aggregate(processors=[geomap.add_feed]) aggregator.write(stream=_sys.stdout)