-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdf.py
306 lines (251 loc) · 10.5 KB
/
pdf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# -*- coding: utf-8 -*-
#
# Copyright (c) 2010-2011, Monash e-Research Centre
# (Monash University, Australia)
# Copyright (c) 2010-2011, VeRSI Consortium
# (Victorian eResearch Strategic Initiative, Australia)
# All rights reserved.
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the VeRSI, the VeRSI Consortium members, nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
"""
pdf.py
.. moduleauthor:: James Wettenhall <james.wettenhall@monash.edu>
"""
from fractions import Fraction
import logging
from django.conf import settings
from tardis.tardis_portal.models import Schema, DatafileParameterSet
from tardis.tardis_portal.models import ParameterName, DatafileParameter
from tardis.tardis_portal.models import DataFileObject
import subprocess
import tempfile
import os
import shutil
import traceback
import urlparse
logger = logging.getLogger(__name__)
class PdfImageFilter(object):
"""This filter uses ImageMagick's convert to display a preview
image of a PDF file.
:param name: the short name of the schema.
:type name: string
:param schema: the name of the schema to load the previewImage into.
:type schema: string
:param tagsToFind: a list of the tags to include.
:type tagsToFind: list of strings
:param tagsToExclude: a list of the tags to exclude.
:type tagsToExclude: list of strings
"""
def __init__(self, name, schema,
tagsToFind=[], tagsToExclude=[]):
self.name = name
self.schema = schema
self.tagsToFind = tagsToFind
self.tagsToExclude = tagsToExclude
def __call__(self, sender, **kwargs):
"""post save callback entry point.
:param sender: The model class.
:param instance: The actual instance being saved.
:param created: A boolean; True if a new record was created.
:type created: bool
"""
instance = kwargs.get('instance')
if not instance.filename.lower().endswith('.pdf'):
return None
print "Applying PDF filter to '%s'..." % instance.filename
schema = self.getSchema()
tmpdir = tempfile.mkdtemp()
filepath = os.path.join(tmpdir, instance.filename)
logger.info("filepath = '" + filepath + "'")
with instance.file_object as f:
with open(filepath, 'wb') as g:
while True:
chunk = f.read(1024)
if not chunk:
break
g.write(chunk)
try:
outputextension = "png"
dfos = DataFileObject.objects.filter(datafile=instance)
preview_image_rel_file_path = os.path.join(
os.path.dirname(urlparse.urlparse(dfos[0].uri).path),
str(instance.id),
'%s.%s' % (os.path.basename(filepath),
outputextension))
logger.info("preview_image_rel_file_path = " +
preview_image_rel_file_path)
preview_image_file_path = os.path.join(
settings.METADATA_STORE_PATH, preview_image_rel_file_path)
logger.info("preview_image_file_path = " + preview_image_file_path)
if not os.path.exists(os.path.dirname(preview_image_file_path)):
os.makedirs(os.path.dirname(preview_image_file_path))
self.fileoutput('/usr/bin',
'convert',
filepath + '[0]',
preview_image_file_path)
metadata_dump = dict()
metadata_dump['previewImage'] = preview_image_rel_file_path
shutil.rmtree(tmpdir)
self.saveMetadata(instance, schema, metadata_dump)
except Exception, e:
print str(e)
print traceback.format_exc()
logger.debug(str(e))
return None
def saveMetadata(self, instance, schema, metadata):
"""Save all the metadata to a Dataset_Files paramamter set.
"""
parameters = self.getParameters(schema, metadata)
# Some/all? of these excludes below are specific to DM3 format:
exclude_line = dict()
if not parameters:
print "Bailing out of saveMetadata because of 'not parameters'."
return None
try:
ps = DatafileParameterSet.objects.get(schema=schema,
datafile=instance)
print "Parameter set already exists for %s, so we'll just " \
"return it." % instance.filename
return ps # if already exists then just return it
except DatafileParameterSet.DoesNotExist:
ps = DatafileParameterSet(schema=schema,
datafile=instance)
ps.save()
for p in parameters:
print p.name
if p.name in metadata:
dfp = DatafileParameter(parameterset=ps,
name=p)
if p.isNumeric():
if metadata[p.name] != '':
dfp.numerical_value = metadata[p.name]
dfp.save()
else:
print p.name
if isinstance(metadata[p.name], list):
for val in reversed(metadata[p.name]):
strip_val = val.strip()
if strip_val:
if strip_val not in exclude_line:
dfp = DatafileParameter(parameterset=ps,
name=p)
dfp.string_value = strip_val
dfp.save()
else:
dfp.string_value = metadata[p.name]
dfp.save()
return ps
def getParameters(self, schema, metadata):
"""Return a list of the paramaters that will be saved.
"""
param_objects = ParameterName.objects.filter(schema=schema)
parameters = []
for p in metadata:
if self.tagsToFind and p not in self.tagsToFind:
continue
if p in self.tagsToExclude:
continue
parameter = filter(lambda x: x.name == p, param_objects)
if parameter:
parameters.append(parameter[0])
continue
# detect type of parameter
datatype = ParameterName.STRING
# Int test
try:
int(metadata[p])
except ValueError:
pass
except TypeError:
pass
else:
datatype = ParameterName.NUMERIC
# Fraction test
if isinstance(metadata[p], Fraction):
datatype = ParameterName.NUMERIC
# Float test
try:
float(metadata[p])
except ValueError:
pass
except TypeError:
pass
else:
datatype = ParameterName.NUMERIC
return parameters
def getSchema(self):
"""Return the schema object that the paramaterset will use.
"""
try:
return Schema.objects.get(namespace__exact=self.schema)
except Schema.DoesNotExist:
schema = Schema(namespace=self.schema, name=self.name,
type=Schema.DATAFILE)
schema.save()
return schema
def exec_command(self, cmd):
"""execute command on shell
"""
p = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
shell=True)
p.wait()
result_str = p.stdout.read()
return result_str
def fileoutput(self,
cd, execfilename, inputfilename, outputfilename, args=""):
"""execute command on shell with a file output
"""
cmd = "cd '%s'; ./'%s' '%s' '%s' %s" %\
(cd, execfilename, inputfilename, outputfilename, args)
print cmd
logger.info(cmd)
return self.exec_command(cmd)
def fileoutput2(self, cd, execfilename, inputfilename, args1,
outputfilename, args2=""):
"""execute command on shell with a file output
"""
cmd = "cd '%s'; ./'%s' '%s' %s '%s' %s" %\
(cd, execfilename, inputfilename, args1, outputfilename, args2)
print cmd
logger.info(cmd)
return self.exec_command(cmd)
def textoutput(self, cd, execfilename, inputfilename, args=""):
"""execute command on shell with a stdout output
"""
cmd = "cd '%s'; ./'%s' '%s' %s" %\
(cd, execfilename, inputfilename, args)
print cmd
logger.info(cmd)
return self.exec_command(cmd)
def make_filter(name='', schema='', tagsToFind=[], tagsToExclude=[]):
if not name:
raise ValueError("PdfImageFilter "
"requires a name to be specified")
if not schema:
raise ValueError("PdfImageFilter "
"requires a schema to be specified")
return PdfImageFilter(name, schema, tagsToFind, tagsToExclude)
make_filter.__doc__ = PdfImageFilter.__doc__