changeset 549:e75fc2c81653

macro.pdf2svg: identifier for updated content based on last modification time added
author Reimar Bauer <rb.proj AT googlemail DOT com>
date Mon, 28 Feb 2011 10:42:38 +0100
parents 9e59fdd04742
children f927d699ca0e
files data/plugin/macro/pdf2svg.py
diffstat 1 files changed, 30 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/data/plugin/macro/pdf2svg.py	Fri Feb 25 11:02:00 2011 +0100
+++ b/data/plugin/macro/pdf2svg.py	Mon Feb 28 10:42:38 2011 +0100
@@ -16,11 +16,13 @@
 from MoinMoin import log
 logging = log.getLogger(__name__)
 
+import httplib
 import os
 import tempfile
 import urllib2
+from urlparse import urlparse
 
-from MoinMoin import caching
+from MoinMoin import caching, config
 from MoinMoin.util.SubProcess import exec_cmd
 from MoinMoin.action import AttachFile, cache
 from MoinMoin.Page import Page
@@ -28,17 +30,30 @@
 CACHE_ARENA = 'sendcache'
 CACHE_SCOPE = 'wiki'
 
-def get_svg_key(request, url, pageno):
+
+def last_modified(request=None, pagename=None, attachment=None, url=""):
+    if not url:
+        pdf_file = os.path.join(AttachFile.getAttachDir(request, pagename), attachment).encode(config.charset)
+        st = os.stat(pdf_file)
+        return request.user.getFormattedDateTime(st.st_mtime)
+
+    parse_result = urlparse(url)
+    conn = httplib.HTTPConnection(parse_result.netloc)
+    conn.request("GET", parse_result.path)
+    response = conn.getresponse()
+    return response.getheader('last-modified')
+
+def get_svg_key(request, url, pageno, identifier):
     """
     gets the key needed for the svg cache file 
     """
-    return cache.key(request, itemname="SVG", content="%s.%s.SVG" % (url, pageno))
+    return cache.key(request, itemname="SVG", content="%s.%s.%s.SVG" % (url, pageno, identifier))
 
-def prepare_svg_cache(request, url, pageno):
+def prepare_svg_cache(request, url, pageno, identifier):
     """
     prepares the svg cache file and returns its file name
     """
-    key = get_svg_key(request, url, pageno)
+    key = get_svg_key(request, url, pageno, identifier)
     cache.put(request, key, "<svg></svg>",
               content_type="image/svg+xml")
     data_cache = caching.CacheEntry(request, CACHE_ARENA, key+'.data', CACHE_SCOPE, do_locking=False)
@@ -73,15 +88,22 @@
     pdf_file = None
 
     if os.name == 'posix':
-        key = get_svg_key(request, attachment or url, pageno)
+        if attachment:
+            url = AttachFile.getAttachUrl(pagename, attachment, request)
+            identifier = last_modified(request, pagename, attachment)
+        else:
+            identifier = last_modified(url=url)
+        logging.debug("%s: %s" % (url, identifier))
+
+        key = get_svg_key(request, url, pageno, identifier)
         if not cache.exists(request, key):
             if attachment and attachment.lower().endswith('.pdf') and AttachFile.exists(request, pagename, attachment):
-                pdf_file = os.path.join(AttachFile.getAttachDir(request, pagename), attachment)
+                pdf_file = os.path.join(AttachFile.getAttachDir(request, pagename), attachment).encode(config.charset)
             elif url:
                 #TODO: a mimetype check is needed beforehand 
                 pdf_file = fetch_pdf_item(request, url)
             if pdf_file:
-                svg_file = prepare_svg_cache(request, url, pageno)
+                svg_file = prepare_svg_cache(request, url, pageno, identifier)
                 cmd = template_cmd % {"pdf_file": pdf_file,
                                       "svg_file": svg_file,
                                       "pageno": pageno