/usr/bin/odfimgimport is in python-odf 0.9.6-2.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
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 | #! /usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (C) 2007-2009 Søren Roug, European Environment Agency
#
# This is free software. You may redistribute it under the terms
# of the Apache license and the GNU General Public License Version
# 2 or at your option any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
# Contributor(s):
#
import zipfile, sys, getopt, mimetypes
from urllib2 import urlopen, quote, unquote
from urlparse import urlunsplit, urlsplit
from odf.opendocument import load
from odf.draw import Image
import cStringIO
sys.tracebacklimit = 0
# Variable to count the number of retrieval failures
failures = 0
# Set to one if quiet behaviour is wanted
quiet = 0
# If set will write every url to import
verbose = 0
# Dictionary with new pictures. Key is original file path
# Item is newfilename
newpictures = {}
doc = None
def importpicture(href):
""" Add the picture to the ZIP file
Returns the new path name to the file in the zip archive
If it is unable to import, then it returns the original href
Sideeffect: add line to manifest
"""
global doc, newpictures, failures, verbose
# Check that it is not already in the manifest
if doc.Pictures.has_key(href): return href
image = None
if verbose: print >>sys.stderr, "Importing", href,
if href[:7] == "http://" or href[:8] == "https://" or href[:6] == "ftp://":
# There is a bug in urlopen: It can't open urls with non-ascii unicode
# characters. Convert to UTF-8 and then use percent encoding
try:
goodhref = href.encode('ascii')
except:
o = list(urlsplit(href))
o[2] = quote(o[2].encode('utf-8'))
goodhref = urlunsplit(o)
if newpictures.has_key(goodhref):
if verbose: print >>sys.stderr, "already imported"
return newpictures[goodhref] # Already imported
try:
f = urlopen(goodhref)
image = f.read()
headers = f.info()
f.close()
# Get the mimetype from the headerlines
c_t = headers['Content-Type'].split(';')[0].strip()
if c_t: mediatype = c_t.split(';')[0].strip()
if verbose: print >>sys.stderr, "OK"
except:
failures += 1
if verbose: print >>sys.stderr, "failed"
return href
# Remove query string
try: href= href[:href.rindex('?')]
except: pass
try:
lastslash = href[href.rindex('/'):]
ext = lastslash[lastslash.rindex('.'):]
except: ext = mimetypes.guess_extension(mediatype)
# Everything is a simple path.
else:
goodhref = href
if href[:3] == '../':
if directory is None:
goodhref = unquote(href[3:])
else:
goodhref = unquote(directory + href[2:])
if newpictures.has_key(goodhref):
if verbose: print >>sys.stderr, "already imported"
return newpictures[goodhref] # Already imported
mediatype, encoding = mimetypes.guess_type(goodhref)
if mediatype is None:
mediatype = ''
try: ext = goodhref[goodhref.rindex('.'):]
except: ext=''
else:
ext = mimetypes.guess_extension(mediatype)
try:
image = file(goodhref).read()
if verbose: print >>sys.stderr, "OK"
except:
failures += 1
if verbose: print >>sys.stderr, "failed"
return href
# If we have a picture to import, the image variable contains it
# and manifestfn, ext and mediatype has a value
if image:
manifestfn = doc.addPictureFromString(image, mediatype)
newpictures[goodhref] = manifestfn
return manifestfn
if verbose: print >>sys.stderr, "not imported"
return href
def exitwithusage(exitcode=2):
""" Print out usage information and exit """
print >>sys.stderr, "Usage: %s [-q] [-v] [-o output] [inputfile]" % sys.argv[0]
print >>sys.stderr, "\tInputfile must be OpenDocument format"
sys.exit(exitcode)
outputfile = None
writefile = True
try:
opts, args = getopt.getopt(sys.argv[1:], "qvo:")
except getopt.GetoptError:
exitwithusage()
for o, a in opts:
if o == "-o":
outputfile = a
writefile = True
if o == "-q":
quiet = 1
if o == "-v":
verbose = 1
if len(args) == 0:
try:
doc = load(sys.stdin)
directory = None
except:
print >>sys.stderr, "Couldn't open OpenDocument file"
exitwithusage()
else:
fn = args[0]
if not zipfile.is_zipfile(fn):
exitwithusage()
dirinx = max(fn.rfind('\\'), fn.rfind('/'))
if dirinx >= 0: directory = fn[:dirinx]
else: directory = "."
doc = load(fn)
for image in doc.getElementsByType(Image):
href = image.getAttribute('href')
newhref = importpicture(href)
image.setAttribute('href',newhref)
if writefile:
if outputfile is None:
doc.save(fn)
else:
doc.save(outputfile)
if quiet == 0 and failures > 0:
print >>sys.stderr, "Couldn't import %d image(s)" % failures
sys.exit( int(failures > 0) )
|