comparison 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
comparison
equal deleted inserted replaced
653:127da830be6c 654:8c828d7b6cd4
17 import re 17 import re
18 import mimetypes 18 import mimetypes
19 from MoinMoin.action.AttachFile import getAttachDir, getAttachUrl, _get_files 19 from MoinMoin.action.AttachFile import getAttachDir, getAttachUrl, _get_files
20 from MoinMoin import wikiutil 20 from MoinMoin import wikiutil
21 from MoinMoin import config 21 from MoinMoin import config
22 from pyPdf.utils import PdfReadError
22 23
23 # ToDo change to pyPDF2 24 # ToDo change to pyPDF2
24 try: 25 try:
25 from pyPdf import PdfFileReader 26 from pyPdf import PdfFileReader
26 except ImportError: 27 except ImportError:
79 80
80 81
81 # ToDo use to DataBrowserWidget 82 # ToDo use to DataBrowserWidget
82 html.append('<table>') 83 html.append('<table>')
83 html.append('<tr><td><b>Author</b></td><td><b>Title</b></td><td></td><td></td></tr>') 84 html.append('<tr><td><b>Author</b></td><td><b>Title</b></td><td></td><td></td></tr>')
84 for file in files: 85 for fn in files:
85 html.append('<tr>') 86 html.append('<tr>')
86 html.append('<td>') 87 html.append('<td>')
87 88
88 fullpath = os.path.join(attach_dir, file).encode(config.charset) 89 pdf_meta = {"/Author": "file access problem", "/Title": fn}
89 pdf = PdfFileReader(open(fullpath, 'rb')) 90
90 pdf_meta = pdf.documentInfo 91 fullpath = os.path.join(attach_dir, fn).encode(config.charset)
91 xmp_meta = pdf.xmpMetadata 92 try:
93 pdf = PdfFileReader(open(fullpath, 'rb'))
94 except PdfReadError:
95 pdf = None
96 if pdf:
97 pdf_meta = pdf.documentInfo
98 xmp_meta = pdf.xmpMetadata
92 try: 99 try:
93 pdf_meta = {"/Title": wikiutil.escape(xmp_meta.dc_title['x-default']), 100 pdf_meta = {"/Title": wikiutil.escape(xmp_meta.dc_title['x-default']),
94 "/Author": wikiutil.escape(pdf_meta['/Author'])} 101 "/Author": wikiutil.escape(pdf_meta['/Author'])}
95 except (AttributeError, KeyError): 102 except (AttributeError, KeyError):
96 pass 103 pass
97 104
98 105
99 links = [] 106 links = []
100 107
101 links.append(fmt.url(1, getAttachUrl(pagename, file, request)) + 108 links.append(fmt.url(1, getAttachUrl(pagename, fn, request)) +
102 fmt.text(label_get) + 109 fmt.text(label_get) +
103 fmt.url(0)) 110 fmt.url(0))
104 111
105 links.append(fmt.url(1, getAttachUrl(pagename, file, request, do='view')) + 112 links.append(fmt.url(1, getAttachUrl(pagename, fn, request, do='view')) +
106 fmt.text(label_view) + 113 fmt.text(label_view) +
107 fmt.url(0)) 114 fmt.url(0))
108 115
109 if may_write and not readonly: 116 if may_write and not readonly:
110 edit_url = getAttachUrl(pagename, file, request, do='modify') 117 edit_url = getAttachUrl(pagename, fn, request, do='modify')
111 if edit_url: 118 if edit_url:
112 links.append(fmt.url(1, edit_url) + 119 links.append(fmt.url(1, edit_url) +
113 fmt.text(label_edit) + 120 fmt.text(label_edit) +
114 fmt.url(0)) 121 fmt.url(0))
115 122
125 if author or title: 132 if author or title:
126 html.append("%(/Author)s</td><td>%(/Title)s</td><td>" % {"/Title": wikiutil.escape(title), 133 html.append("%(/Author)s</td><td>%(/Title)s</td><td>" % {"/Title": wikiutil.escape(title),
127 "/Author": wikiutil.escape(author)}) 134 "/Author": wikiutil.escape(author)})
128 135
129 else: 136 else:
130 html.append('</td><td>%(filename)s</td><td>' % {"filename": file}) 137 html.append('</td><td>%(filename)s</td><td>' % {"filename": fn})
131 138
132 139
133 html.append("%s" % "&nbsp;</td><td> ".join(links)) 140 html.append("%s" % "&nbsp;</td><td> ".join(links))
134 html.append('</td>') 141 html.append('</td>')
135 html.append('</tr>') 142 html.append('</tr>')