1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/MoinRemoteSupport.py Tue Jul 17 00:24:08 2012 +0200
1.3 @@ -0,0 +1,68 @@
1.4 +# -*- coding: iso-8859-1 -*-
1.5 +"""
1.6 + MoinMoin - MoinRemoteSupport library
1.7 +
1.8 + @copyright: 2011, 2012 by Paul Boddie <paul@boddie.org.uk>
1.9 + @license: GNU GPL (v2 or later), see COPYING.txt for details.
1.10 +"""
1.11 +
1.12 +from MoinMoin.action import cache
1.13 +from MoinMoin import caching
1.14 +import urllib2
1.15 +
1.16 +def getCachedResource(request, url, arena, scope, max_cache_age):
1.17 +
1.18 + """
1.19 + Using the given 'request', return the resource data for the given 'url',
1.20 + accessing a cache entry with the given 'arena' and 'scope' where the data
1.21 + has already been downloaded. The 'max_cache_age' indicates the length in
1.22 + seconds that a cache entry remains valid.
1.23 + """
1.24 +
1.25 + # See if the URL is cached.
1.26 +
1.27 + cache_key = cache.key(request, content=url)
1.28 + cache_entry = caching.CacheEntry(request, arena, cache_key, scope=scope)
1.29 +
1.30 + # If no entry exists, or if the entry is older than the specified age,
1.31 + # create one with the response from the URL.
1.32 +
1.33 + now = time.time()
1.34 + mtime = cache_entry.mtime()
1.35 +
1.36 + # NOTE: The URL could be checked and the 'If-Modified-Since' header
1.37 + # NOTE: (see MoinMoin.action.pollsistersites) could be checked.
1.38 +
1.39 + if not cache_entry.exists() or now - mtime >= max_cache_age:
1.40 +
1.41 + # Access the remote data source.
1.42 +
1.43 + cache_entry.open(mode="w")
1.44 +
1.45 + try:
1.46 + f = urllib2.urlopen(url)
1.47 + try:
1.48 + cache_entry.write(url + "\n")
1.49 + cache_entry.write((f.headers.get("content-type") or "") + "\n")
1.50 + cache_entry.write(f.read())
1.51 + finally:
1.52 + cache_entry.close()
1.53 + f.close()
1.54 +
1.55 + # In case of an exception, just ignore the remote source.
1.56 + # NOTE: This could be reported somewhere.
1.57 +
1.58 + except IOError:
1.59 + if cache_entry.exists():
1.60 + cache_entry.remove()
1.61 + continue
1.62 +
1.63 + # Open the cache entry and read it.
1.64 +
1.65 + cache_entry.open()
1.66 + try:
1.67 + return cache_entry.read()
1.68 + finally:
1.69 + cache_entry.close()
1.70 +
1.71 +# vim: tabstop=4 expandtab shiftwidth=4
2.1 --- a/README.txt Tue Jul 17 00:01:30 2012 +0200
2.2 +++ b/README.txt Tue Jul 17 00:24:08 2012 +0200
2.3 @@ -70,6 +70,8 @@
2.4 * Added a getCurrentTime function to DateSupport.
2.5 * Added parsing/formatting-related functions from EventAggregator and
2.6 ImprovedTableParser to MoinSupport.
2.7 + * Added remote resource management from EventAggregator to
2.8 + MoinRemoteSupport.
2.9
2.10 Release Procedures
2.11 ------------------