diff data/plugin/macro/PdfList.py @ 654:8c828d7b6cd4 tip

PDFList: check for pyPdf.utils.PdfReadError
author Reimar Bauer <rb.proj AT googlemail DOT com>
date Wed, 07 Dec 2016 14:27:25 +0100
parents 127da830be6c
children
line wrap: on
line diff
--- a/data/plugin/macro/PdfList.py	Tue Apr 19 16:29:54 2016 +0200
+++ b/data/plugin/macro/PdfList.py	Wed Dec 07 14:27:25 2016 +0100
@@ -19,6 +19,7 @@
 from MoinMoin.action.AttachFile import getAttachDir, getAttachUrl, _get_files
 from MoinMoin import wikiutil
 from MoinMoin import config
+from pyPdf.utils import PdfReadError
 
 # ToDo change to pyPDF2
 try:
@@ -81,14 +82,20 @@
         # ToDo use to DataBrowserWidget
         html.append('<table>')
         html.append('<tr><td><b>Author</b></td><td><b>Title</b></td><td></td><td></td></tr>')
-        for file in files:
+        for fn in files:
             html.append('<tr>')
             html.append('<td>')
 
-            fullpath = os.path.join(attach_dir, file).encode(config.charset)
-            pdf = PdfFileReader(open(fullpath, 'rb'))
-            pdf_meta = pdf.documentInfo
-            xmp_meta = pdf.xmpMetadata
+            pdf_meta = {"/Author": "file access problem", "/Title": fn}
+
+            fullpath = os.path.join(attach_dir, fn).encode(config.charset)
+            try:
+                pdf = PdfFileReader(open(fullpath, 'rb'))
+            except PdfReadError:
+                pdf = None
+            if pdf:
+                pdf_meta = pdf.documentInfo
+                xmp_meta = pdf.xmpMetadata
             try:
                 pdf_meta = {"/Title": wikiutil.escape(xmp_meta.dc_title['x-default']),
                             "/Author": wikiutil.escape(pdf_meta['/Author'])}
@@ -98,16 +105,16 @@
 
             links = []
 
-            links.append(fmt.url(1, getAttachUrl(pagename, file, request)) +
+            links.append(fmt.url(1, getAttachUrl(pagename, fn, request)) +
                          fmt.text(label_get) +
                          fmt.url(0))
 
-            links.append(fmt.url(1, getAttachUrl(pagename, file, request, do='view')) +
+            links.append(fmt.url(1, getAttachUrl(pagename, fn, request, do='view')) +
                          fmt.text(label_view) +
                          fmt.url(0))
 
             if may_write and not readonly:
-                edit_url = getAttachUrl(pagename, file, request, do='modify')
+                edit_url = getAttachUrl(pagename, fn, request, do='modify')
                 if edit_url:
                     links.append(fmt.url(1, edit_url) +
                                  fmt.text(label_edit) +
@@ -127,7 +134,7 @@
                                                                          "/Author": wikiutil.escape(author)})
 
             else:
-                html.append('</td><td>%(filename)s</td><td>' % {"filename": file})
+                html.append('</td><td>%(filename)s</td><td>' % {"filename": fn})
 
 
             html.append("%s" % "&nbsp;</td><td> ".join(links))