/usr/share/pyshared/Eikazo/output/SaneScanToPrint.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 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 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 | """
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.
print scans
Disabled for now. See the comment at line 110
"""
import os, sys, traceback, subprocess, time
import gtk, gobject
from Eikazo.SaneError import SaneError
from Eikazo import Processor, I18n
import outputinfo
DEBUG = 0
t = I18n.get_translation('eikazo')
if t:
_ = t.gettext
else:
_ = lambda x: x
N_ = lambda x: x
providers = []
class ScanJobFilePrinter(Processor.SaneThreadingQueueingProcessor):
def __init__(self, paramsadm, input_producer, notify_hub, queue_length):
""" input_producer, queue_length: see base classes
paramsadm: instance of a class which can return
"""
Processor.SaneThreadingQueueingProcessor.__init__(self,
input_producer, notify_hub, queue_length)
self.errorjobs = []
self.paramsadm = paramsadm
self.start()
self.status = 0
def append(self, job):
Processor.SaneThreadingQueueingProcessor.append(self, job)
job.status['print'] = _('waiting to be printed')
self.notify('status changed', job)
def run(self):
while not self.abort:
if len(self.queue) and not self.errorjobs:
self.queuelock.acquire()
job = self.queue.pop(0)
self.status = 1 # writing
self.queuelock.release()
job.set_active(True)
try:
job.status['print'] = _('printing')
self.notify('status changed', job)
self.paramsadm.save(job)
del job.status['print']
self.notify('removed', job)
except:
job.error = sys.exc_info()
job.status['print'] = _('print error')
if DEBUG:
print str(job.error[0]), str(job.error[1])
traceback.print_tb(job.error[2])
self.notify('status changed', job)
self.errorjobs.append(job)
job.set_active(False)
self.status = 0 # idle
if DEBUG:
print "printing finished", job.id
else:
time.sleep(0.1)
def numjobs(self, cascade):
self.queuelock.acquire()
res = len(self.queue) + len(self.errorjobs)
if self.status != 0:
res += 1
self.queuelock.release()
return res
def retry_job(self, job):
res = False
self.queuelock.acquire()
for i in xrange(len(self.errorjobs)):
if job == self.errorjobs[i]:
self.errorjobs.pop(i)
# scan jobs shouldprocessed in the sequence of their
# job ids
self.queue.append(job)
self.queue.sort(lambda x,y: cmp(x.id, y.id))
res = True
break
self.queuelock.release()
return res
# disable this printing variant for now: Too many problems with gnomeprint
# Aside from the two problems mentioned below (threading and creating a
# printer configuration from an XML file, we can get page size hassles:
# On Suse 9.3, the typical setup - CUPS, foomatic and a PCL printer - does not
# "see" the definition of A4 paper size, while a real Postscript printer
# uses this paper size. The Problem is this part of the Postscript header
# produced by gnomeprint:
#
# <<
# /PageSize [595 842]
# /ImagingBBox null
# /Duplex false
# /Tumble false
# /NumCopies 1
# /Policies <<
# /PolicyNotFound 1
# /PageSize 3
# >>
# >> setpagedevice
#
# The line /PageSize [595 842] defines an A4 page, but the dict /Policies
# defines the fallback policy, what to do, if the requested page size is
# not availabe. /PageSize 3 means "select the nearest available medium".
# Works fine for a real Postscript printer, but for Ghostscript/foomatic
# this results in the page size being set to letter or whatever else:
# If A4 is requested, the printed page has too large blank margins at the
# top of a printed page. In short: Crap. Though this is probably not
# strictly a problem of gnomeprint, it can be a showstopper from the user
# perspective.
#
# Moreover, I think that an application _library_ like gnomeprint should
# anyway not make assuptions, what to do in the case that a certain
# paper size is not available in a devide. It might sometimes be
# preferable to abort printing, if the a paper size is not supported by
# a device is selected. The decision what to do with a wrong paper size
# should be left to the user and/or an application.
if 0:
try:
import gnomeprint, gnomeprint.ui
""" two problems with gnomeprint and gnomeprint.ui
(1) older versions of Gnome's print dialog are not thread safe:
http://bugzilla.gnome.org/show_bug.cgi?id=311410
This could be fixed by upgrading to a more recent Gnome library,
but not everybody will want to such complex work.
Hence the print dialog is started in a forked program, and the
result is passed as an XML description to the main program
(2) On startup, libgnomeprint does not collect information about
available printers. Probably a decision to speed up the start, but
this has a serious consequence: A call to gnomeprint.config_from_string
silently returns the default config (silently from Python's perspective
-- we get a warning on stdout or stderr), if a printer dialog has not
been created and run before. There seems to be no other way to
initialize libgnomeprint's internal printer list.
This means that we can't load a printer configuration from an XML
description without running first a dialog. In other words, we can't
load some printing details from Eikazo's config file. Moreover, it
seems that libgnomeprint's default page size is letter -- not very useful
in some parts of the world.
The two problems combined are especially nasty: Eikazo relies
heavily on threading, so we even can't create a printer dialog
the usual way on some installations, hence we even can't try to
set printer details in our own dialog: a gnomeprint.Config instance
will always represent the default setup.
So let's use a really ugly workaround: create a printer dialog
during startup, hide it immediately, wait a few seconds until
the dialog has collected information about installed printers
and then delete it. That's acrazy, but I don't see another way
to get printing properly working.
"""
_idle_first = True
def _idle_dlg():
global _idle_first
if _idle_first:
_d.iconify()
_idle_first = False
if time.time() < _ready:
return True
_d.emit("delete-event", gtk.gdk.Event(gtk.gdk.DELETE))
# give dthe dialog 8 seconds to collect printer information from Cups etc.
_ready = time.time() + 8
_c = gnomeprint.config_default()
_j = gnomeprint.Job(_c)
_d = gnomeprint.ui.Dialog(_j, "please ignore this...")
gobject.idle_add(_idle_dlg)
_d.run()
printerdlg = """
import sys, gnomeprint, gnomeprint.ui
x = gnomeprint.config_default()
j = gnomeprint.Job(x)
cfg = sys.stdin.read()
sys.stderr.write("--------- dlg read ------------\\\n%s\\\n" % cfg)
cfg = gnomeprint.config_from_string(cfg, 0)
sys.stderr.write("--------- dlg internal ---------\\\n")
sys.stderr.write(cfg.to_string(0))
job = gnomeprint.Job(cfg)
dlg = gnomeprint.ui.Dialog(job, "Printer Setup", 0)
res = dlg.run()
if res == gnomeprint.ui.DIALOG_RESPONSE_PRINT:
sys.stdout.write(cfg.to_string())
"""
# FIXME: at present, we don't save the configuration. Problem is, that
# gnomeprint does not know about most printers, before a print selection
# dialog is shown. See
# http://mail.gnome.org/archives/gnome-print-list/2004-August/msg00026.html
# This patch is obviously not yet integrated...
class ScanToPrint(outputinfo.OutputProvider):
name = N_("Copier")
connectlabel = N_("Enable Copier")
def __init__(self, notify_hub, config):
outputinfo.OutputProvider.__init__(self, notify_hub, config)
self.printconfig = gnomeprint.config_default()
self.printjob = gnomeprint.Job(self.printconfig)
self.build_widget()
self.processor = ScanJobFilePrinter(self, self.processor_input,
notify_hub, 2)
def build_widget(self):
self.widget = gtk.VBox()
hbox = gtk.HBox()
self.widget.pack_start(hbox, expand=False)
hbox.show()
printoptbtn = gtk.Button(_("Print Options"))
hbox.pack_start(printoptbtn, expand=False)
printoptbtn.show()
printoptbtn.connect("clicked", self.printoptions)
def printoptions(self, widget):
# problem: The print dialog wants to acquire a lock
# in older GTK versions.
# Hence the dialog is shown in a sub-process.
cmd = ["python", "-c", "exec(%s)" % repr(printerdlg)]
cfg = self.printconfig.to_string(0)
p = subprocess.Popen(cmd,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
close_fds=True)
# writing the current config to the dialog process is pointless:
# We'll only get the error message "model not found", because
# of the problem described above... Hell, what a mess
# We could create a long running process, but that would
# require to shut it down properly and whatever else -- just to
# to work around a crazy Gnome bug
res, err = p.communicate(input=cfg)
#res, err = p.communicate(input="")
if res:
print "-------------------------------------"
print res
print "-------------------------------------"
self.printconfig = gnomeprint.config_from_string(res, 0)
self.printjob = gnomeprint.Job(self.printconfig)
self.printconfig.dump()
def save(self, job):
# FIXME: add: centering the image, optional scaling etc
img = job.img
xres = job.resolution
yres = job.y_resolution
gpc = self.printjob.get_context()
gpc.beginpage("1")
mode = img.mode
data = img.tostring("raw", mode)
w,h = img.size
# for now, only 100% zoom. The methods gpc.grayimage
# and gpc.rgbimage paint in the the square (0,0) (1,1)
# in the current coordinate system, so we just need to
# set the scale to the size of the image in point
if yres == None:
yres = xres
iw = float(w) * 72 / xres
ih = float(h) * 72 / yres
gpc.scale(iw, ih)
if mode in ('1', 'L'):
data = img.tostring("raw", 'L')
gpc.grayimage(data, w, h, w)
elif mode == "RGB":
gpc.rgbimage(data, w, h, w)
else:
raise SaneError("Unsupported PIL image mode: %s" % mode)
gpc.showpage()
self.printjob.close()
self.printjob.print_()
self.printjob = gnomeprint.Job(self.printconfig)
providers.append(ScanToPrint)
except ImportError, val:
print "warning: could not import module gnomeprint:", str(val)
print " Print output disabled"
def register():
return providers
|