# HG changeset patch # User Paul Boddie # Date 1390869863 -3600 # Node ID 95c021fe98bbc004e58c7a785a69654a9db5b567 # Parent 2c1b6f9be50b295eed4f2cd560cd3be59748115f Simplified the parsing of event source data, consolidating the parsing operations in the Types module and introducing a new underlying parsing function for iCalendar resources. diff -r 2c1b6f9be50b -r 95c021fe98bb EventAggregatorSupport/Resources.py --- a/EventAggregatorSupport/Resources.py Sat Jan 25 23:40:30 2014 +0100 +++ b/EventAggregatorSupport/Resources.py Tue Jan 28 01:44:23 2014 +0100 @@ -2,7 +2,7 @@ """ MoinMoin - EventAggregator resource acquisition and access - @copyright: 2008, 2009, 2010, 2011, 2012, 2013 by Paul Boddie + @copyright: 2008, 2009, 2010, 2011, 2012, 2013, 2014 by Paul Boddie @license: GNU GPL (v2 or later), see COPYING.txt for details. """ @@ -13,7 +13,6 @@ from MoinSupport import * from MoinRemoteSupport import getCachedResource, getCachedResourceMetadata -import codecs import urllib try: @@ -114,8 +113,7 @@ # NOTE: the content type provided by the URL. if format == "ical" and vCalendar is not None: - parser = vCalendar.parse - resource_cls = EventCalendar + parser = parseEventsInCalendarFromResource required_content_type = expected_content_type or "text/calendar" else: return None @@ -141,11 +139,8 @@ # Send the data to the parser. - uf = codecs.getreader(encoding or "utf-8")(f) - try: - return resource_cls(url, parser(uf), metadata) - finally: - uf.close() + return parser(f, encoding, url, metadata) + finally: f.close() diff -r 2c1b6f9be50b -r 95c021fe98bb EventAggregatorSupport/Types.py --- a/EventAggregatorSupport/Types.py Sat Jan 25 23:40:30 2014 +0100 +++ b/EventAggregatorSupport/Types.py Tue Jan 28 01:44:23 2014 +0100 @@ -68,11 +68,31 @@ def parseEventsInCalendar(text): - # Make a Unicode-capable StringIO. + """ + Parse events in iCalendar format from the given 'text'. + """ + + # Fill the StringIO with encoded plain string data. + + encoding = "utf-8" + calendar = parseEventsInCalendarFromResource(StringIO(text.encode(encoding)), encoding) + return calendar.getEvents() + +def parseEventsInCalendarFromResource(f, encoding=None, url=None, metadata=None): - f = getreader("utf-8")(StringIO(text.encode("utf-8"))) - calendar = EventCalendar("", vCalendar.parse(f), {}) - return calendar.getEvents() + """ + Parse events in iCalendar format from the given file-like object 'f', with + content having any specified 'encoding' and being described by the given + 'url' and 'metadata'. + """ + + # Read Unicode from the resource. + + uf = getreader(encoding or "utf-8")(f) + try: + return EventCalendar(url or "", vCalendar.parse(uf), metadata or {}) + finally: + uf.close() def parseEvents(text, event_page, fragment=None):