1 # -*- coding: iso-8859-1 -*- 2 """ 3 MoinMoin - MoinRemoteSupport library 4 5 @copyright: 2011, 2012 by Paul Boddie <paul@boddie.org.uk> 6 @license: GNU GPL (v2 or later), see COPYING.txt for details. 7 """ 8 9 from MoinMoin.action import cache 10 from MoinMoin import caching 11 import urllib2 12 13 def getCachedResource(request, url, arena, scope, max_cache_age): 14 15 """ 16 Using the given 'request', return the resource data for the given 'url', 17 accessing a cache entry with the given 'arena' and 'scope' where the data 18 has already been downloaded. The 'max_cache_age' indicates the length in 19 seconds that a cache entry remains valid. 20 """ 21 22 # See if the URL is cached. 23 24 cache_key = cache.key(request, content=url) 25 cache_entry = caching.CacheEntry(request, arena, cache_key, scope=scope) 26 27 # If no entry exists, or if the entry is older than the specified age, 28 # create one with the response from the URL. 29 30 now = time.time() 31 mtime = cache_entry.mtime() 32 33 # NOTE: The URL could be checked and the 'If-Modified-Since' header 34 # NOTE: (see MoinMoin.action.pollsistersites) could be checked. 35 36 if not cache_entry.exists() or now - mtime >= max_cache_age: 37 38 # Access the remote data source. 39 40 cache_entry.open(mode="w") 41 42 try: 43 f = urllib2.urlopen(url) 44 try: 45 cache_entry.write(url + "\n") 46 cache_entry.write((f.headers.get("content-type") or "") + "\n") 47 cache_entry.write(f.read()) 48 finally: 49 cache_entry.close() 50 f.close() 51 52 # In case of an exception, just ignore the remote source. 53 # NOTE: This could be reported somewhere. 54 55 except IOError: 56 if cache_entry.exists(): 57 cache_entry.remove() 58 continue 59 60 # Open the cache entry and read it. 61 62 cache_entry.open() 63 try: 64 return cache_entry.read() 65 finally: 66 cache_entry.close() 67 68 # vim: tabstop=4 expandtab shiftwidth=4