test/aggregate.py: Add an example aggregation script
authorW. Trevor King <wking@tremily.us>
Sun, 30 Jun 2013 20:22:52 +0000 (16:22 -0400)
committerW. Trevor King <wking@tremily.us>
Sun, 30 Jun 2013 20:36:56 +0000 (16:36 -0400)
This is how you use the library :).

MANIFEST.in
README
test/aggregate.py [new file with mode: 0755]

index 7c95064b777be2133705fa1b120954ef9c442e85..b300d1b968d7dcebed5132d9949a32c518210ee0 100644 (file)
@@ -1,2 +1,2 @@
 include COPYING
-recursive-include test README *.ics
+recursive-include test README *.py *.ics
diff --git a/README b/README
index 053a1f9d44a6c4805a3287b50b4eccd07ea16ea7..2818559e4ac48397339ab8c955c90627e48969bf 100644 (file)
--- a/README
+++ b/README
@@ -31,6 +31,11 @@ Test with nose_::
 
   $ nosetests --with-doctest --doctest-tests pycalendar
 
+There is also a example aggregation script that prints aggregate feed
+to stdout and geographic positions to stderr.  Run it with::
+
+  $ PYTHONPATH=. test/aggregate.py
+
 
 .. _Calagator: http://calagator.org/
 .. _distutils: http://docs.python.org/3/distutils/
diff --git a/test/aggregate.py b/test/aggregate.py
new file mode 100755 (executable)
index 0000000..d2cd738
--- /dev/null
@@ -0,0 +1,51 @@
+#!/usr/bin/env python
+#
+# Copyright
+
+"""An example aggregation script
+
+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
+javascript that renders a map showing event locations.
+"""
+
+import os as _os
+import sys as _sys
+
+import pycalendar.aggregator as _pycalendar_aggregator
+import pycalendar.feed as _pycalendar_feed
+
+
+def get_geo(feed):
+    for event in feed['VEVENT']:
+        if 'GEO' in event:
+            lat,lon = [float(x) for x in event['GEO'].split(';')]
+            _sys.stderr.write('{} at lat {}, lon {}\n'.format(
+                event['UID'], lat, lon))
+
+
+def get_urls(root=_os.path.dirname(__file__)):
+    for dirpath, dirnames, filenames in _os.walk(root):
+        for filename in filenames:
+            base,ext = _os.path.splitext(filename)
+            if ext == '.ics':
+                path = _os.path.abspath(_os.path.join(dirpath, filename))
+                yield 'file://{}'.format(path.replace(_os.sep, '/'))
+
+
+def aggregate():
+    aggregator = _pycalendar_aggregator.Aggregator(
+        prodid='-//pycalendar//NONSGML testing//EN',
+        feeds=[_pycalendar_feed.Feed(url=url)
+               for url in get_urls()],
+        processors=[get_geo],
+        )
+    aggregator.fetch()
+    return aggregator
+
+
+if __name__ == '__main__':
+    aggregator = aggregate()
+    aggregator.write(stream=_sys.stdout)