/usr/share/pyshared/Eikazo/postprocessing/_ExternalFilter.py is in eikazo 0.5.2-8.
This file is owned by root:root, with mode 0o644.
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 | """
Copyright (c) Abel Deuring 2006 <adeuring@gmx.net>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
Base class for filters which invoke external programs
"""
import subprocess, threading, tempfile, os
import gtk, gobject
from Eikazo.SaneError import SaneError
from Eikazo import Processor, I18n
import procinfo
import Image
DEBUG = 1
t = I18n.get_translation('eikazo')
if t:
_ = t.gettext
else:
_ = lambda x: x
N_ = lambda x: x
providers = []
class Infile:
def __init__(self, name):
self.name = name
def __call__(self):
return self.name
class Outfile:
def __init__(self, name):
self.name = name
def __call__(self):
return self.name
class _ExternalFilter(procinfo.ProcessingProvider):
filterdescription = N_("""\
Replace this!
""")
def __init__(self, notify_hub, enabled, config):
self.connectlabel = N_("Enable %s") % self.name
procinfo.ProcessingProvider.__init__(self, notify_hub, enabled, config)
self.build_widget(config)
self.processor = procinfo.ScanJobFilterProcessor(self,
self.processor_input, notify_hub, self.name, self.enabled)
def build_widget(self):
raise SaneError("_ExternalFilter.build_widget must be overloaded in derived classes")
def filter(self, job):
""" called by the procesor for this filter.
This method must be overloaded
"""
raise SaneError("_ExternalFilter.build_widget must be overloaded in derived classes")
def file_out_file_in(self, args, img, imgtype=None):
""" invoke an external program.
The input image is written to a temporay file; the output file
is read from a temporary file
args is a sequence string defining the "command line"; args[0]
is the program name; the following sequence elements are
parameters for the program. See the subprocess.Popen
documentation for further details.
Placeholders for the input filename and output filename
can be given to the args sequence as
_ExternalFilter.Infile(filename)
resp
_ExternalFilter.Outfile(filename)
where filename is a string with the name of the input resp.
output file. (The input file is the file to be read by the
external program.) The filenames must not contain a
path.
img is the PIL image object to be processed by the external
program
imgtype is a string specifying the format of the image
as sent to the external program. If must be recognized by
the save method of a PIL image. May be None
returns: PIL image instance
"""
dir = tempfile.mkdtemp()
infilename = outfilename = None
try:
for i in xrange(len(args)):
arg = args[i]
if isinstance(arg, Infile):
infilename = os.path.join(dir, arg())
args[i] = infilename
elif isinstance(arg, Outfile):
outfilename = os.path.join(dir, arg())
args[i] = outfilename
if imgtype:
img.save(infilename, imgtype)
else:
img.save(outfilename)
res = subprocess.call(args)
if res:
raise SaneError("external filter returned an error: %s" % res)
return Image.open(outfilename)
finally:
files = os.listdir(dir)
for name in files:
name = os.path.join(dir, name)
os.unlink(name)
os.rmdir(dir)
def register():
return []
|