# HG changeset patch # User Paul Boddie # Date 1342477448 -7200 # Node ID f1b4f05a4801571e762c5b031f08b327141c4db9 # Parent 4e984fd40300484ed4e4e4f25021475a86a8ad5a Added remote resource management from EventAggregator. diff -r 4e984fd40300 -r f1b4f05a4801 MoinRemoteSupport.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MoinRemoteSupport.py Tue Jul 17 00:24:08 2012 +0200 @@ -0,0 +1,68 @@ +# -*- coding: iso-8859-1 -*- +""" + MoinMoin - MoinRemoteSupport library + + @copyright: 2011, 2012 by Paul Boddie + @license: GNU GPL (v2 or later), see COPYING.txt for details. +""" + +from MoinMoin.action import cache +from MoinMoin import caching +import urllib2 + +def getCachedResource(request, url, arena, scope, max_cache_age): + + """ + Using the given 'request', return the resource data for the given 'url', + accessing a cache entry with the given 'arena' and 'scope' where the data + has already been downloaded. The 'max_cache_age' indicates the length in + seconds that a cache entry remains valid. + """ + + # See if the URL is cached. + + cache_key = cache.key(request, content=url) + cache_entry = caching.CacheEntry(request, arena, cache_key, scope=scope) + + # If no entry exists, or if the entry is older than the specified age, + # create one with the response from the URL. + + now = time.time() + mtime = cache_entry.mtime() + + # NOTE: The URL could be checked and the 'If-Modified-Since' header + # NOTE: (see MoinMoin.action.pollsistersites) could be checked. + + if not cache_entry.exists() or now - mtime >= max_cache_age: + + # Access the remote data source. + + cache_entry.open(mode="w") + + try: + f = urllib2.urlopen(url) + try: + cache_entry.write(url + "\n") + cache_entry.write((f.headers.get("content-type") or "") + "\n") + cache_entry.write(f.read()) + finally: + cache_entry.close() + f.close() + + # In case of an exception, just ignore the remote source. + # NOTE: This could be reported somewhere. + + except IOError: + if cache_entry.exists(): + cache_entry.remove() + continue + + # Open the cache entry and read it. + + cache_entry.open() + try: + return cache_entry.read() + finally: + cache_entry.close() + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r 4e984fd40300 -r f1b4f05a4801 README.txt --- a/README.txt Tue Jul 17 00:01:30 2012 +0200 +++ b/README.txt Tue Jul 17 00:24:08 2012 +0200 @@ -70,6 +70,8 @@ * Added a getCurrentTime function to DateSupport. * Added parsing/formatting-related functions from EventAggregator and ImprovedTableParser to MoinSupport. + * Added remote resource management from EventAggregator to + MoinRemoteSupport. Release Procedures ------------------