/usr/share/pyshared/x2go/printactions.py is in python-x2go 0.4.0.8-1.
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 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 | # -*- coding: utf-8 -*-
# Copyright (C) 2010-2013 by Mike Gabriel <mike.gabriel@das-netzwerkteam.de>
#
# Python X2Go is free software; you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Python X2Go 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
"""\
Print jobs can either be sent to any of the local print queues (CUPS, Win32API),
be opened in an external PDF viewer, be saved to a local folder or be handed
over to a custom (print) command. This is defined by four print action classes
(L{X2GoPrintActionDIALOG}, L{X2GoPrintActionPDFVIEW}, L{X2GoPrintActionPDFSAVE}, L{X2GoPrintActionPRINT} and
L{X2GoPrintActionPRINTCMD}).
"""
__NAME__ = 'x2goprintactions-pylib'
# modules
import os
import shutil
import copy
import time
import gevent
from defaults import X2GOCLIENT_OS as _X2GOCLIENT_OS
if _X2GOCLIENT_OS in ("Windows"):
import subprocess
import win32api
import win32print
else:
import gevent_subprocess as subprocess
import x2go_exceptions
WindowsError = x2go_exceptions.WindowsError
# Python X2Go modules
import log
import defaults
# we hide the default values from epydoc (that's why we transform them to _UNDERSCORE variables)
import utils
import x2go_exceptions
_PRINT_ENV = os.environ.copy()
class X2GoPrintAction(object):
__name__ = 'NAME'
__description__ = 'DESCRIPTION'
def __init__(self, client_instance=None, logger=None, loglevel=log.loglevel_DEFAULT):
"""\
This is a meta class and has no functionality as such. It is used as parent
class by »real« X2Go print actions.
@param client_instance: the underlying L{X2GoClient} instance
@type client_instance: C{obj}
@param logger: you can pass an L{X2GoLogger} object to the
L{X2GoPrintAction} constructor
@type logger: C{obj}
@param loglevel: if no L{X2GoLogger} object has been supplied a new one will be
constructed with the given loglevel
@type loglevel: C{int}
"""
if logger is None:
self.logger = log.X2GoLogger(loglevel=loglevel)
else:
self.logger = copy.deepcopy(logger)
self.logger.tag = __NAME__
# these get set from within the X2GoPrintQueue class
self.profile_name = 'UNKNOWN'
self.session_name = 'UNKNOWN'
self.client_instance = client_instance
@property
def name(self):
"""\
Return the X2Go print action's name.
"""
return self.__name__
@property
def description(self):
"""\
Return the X2Go print action's description text.
"""
return self.__description__
def _do_print(self, pdf_file, job_title, spool_dir, ):
"""
Perform the defined print action (doing nothing in L{X2GoPrintAction} parent class).
@param pdf_file: PDF file name as placed in to the X2Go spool directory
@type pdf_file: C{str}
@param job_title: human readable print job title
@type job_title: C{str}
@param spool_dir: location of the X2Go client's spool directory
@type spool_dir: C{str}
"""
pass
def do_print(self, pdf_file, job_title, spool_dir, ):
"""\
Wrap around the actual print action (C{self._do_print}) with
gevent.spawn().
@param pdf_file: PDF file name as placed in to the X2Go spool directory
@type pdf_file: C{str}
@param job_title: human readable print job title
@type job_title: C{str}
@param spool_dir: location of the X2Go client's spool directory
@type spool_dir: C{str}
"""
pdf_file = os.path.normpath(pdf_file)
spool_dir = os.path.normpath(spool_dir)
self._do_print(pdf_file, job_title, spool_dir)
def _humanreadable_filename(self, pdf_file, job_title, target_path):
"""\
Extract a human readable filename for the X2Go print job file.
@param pdf_file: PDF file name as placed in to the X2Go spool directory
@type pdf_file: C{str}
@param job_title: human readable print job title
@type job_title: C{str}
@param target_path: target path for human readable file
@type target_path: C{str}
@return: full readable file name path
@rtype: C{str}
"""
_hr_path = os.path.normpath(os.path.expanduser(os.path.join(os.path.normpath(target_path), '%s.pdf' % utils.slugify(job_title))))
i = 0
while os.path.exists(_hr_path):
i += 1
_hr_path = os.path.normpath(os.path.expanduser(os.path.join(os.path.normpath(target_path), '%s(%s).pdf' % (utils.slugify(job_title), i))))
return _hr_path
class X2GoPrintActionPDFVIEW(X2GoPrintAction):
"""\
Print action that views incoming print job in an external PDF viewer application.
"""
__name__= 'PDFVIEW'
__decription__= 'View as PDF document'
pdfview_cmd = None
def __init__(self, client_instance=None, pdfview_cmd=None, logger=None, loglevel=log.loglevel_DEFAULT):
"""\
@param client_instance: the underlying L{X2GoClient} instance
@type client_instance: C{obj}
@param pdfview_cmd: command that starts the external PDF viewer application
@type pdfview_cmd: C{str}
@param logger: you can pass an L{X2GoLogger} object to the
L{X2GoPrintActionPDFVIEW} constructor
@type logger: C{obj}
@param loglevel: if no L{X2GoLogger} object has been supplied a new one will be
constructed with the given loglevel
@type loglevel: C{int}
"""
if pdfview_cmd is None:
pdfview_cmd = defaults.DEFAULT_PDFVIEW_CMD
self.pdfview_cmd = pdfview_cmd
X2GoPrintAction.__init__(self, client_instance=client_instance, logger=logger, loglevel=loglevel)
def _do_print(self, pdf_file, job_title, spool_dir, ):
"""\
Open an incoming X2Go print job (PDF file) in an external PDF viewer application.
@param pdf_file: PDF file name as placed in to the X2Go spool directory
@type pdf_file: C{str}
@param job_title: human readable print job title
@type job_title: C{str}
@param spool_dir: location of the X2Go client's spool directory
@type spool_dir: C{str}
@raise OSError: pass through all C{OSError}s except no. 2
"""
pdf_file = os.path.normpath(pdf_file)
spool_dir = os.path.normpath(spool_dir)
if _X2GOCLIENT_OS == "Windows":
self.logger('viewing incoming job in PDF viewer with Python\'s os.startfile( command): %s' % pdf_file, loglevel=log.loglevel_DEBUG)
try:
gevent.spawn(os.startfile, pdf_file)
except WindowsError, win_err:
if self.client_instance:
self.client_instance.HOOK_printaction_error(pdf_file,
profile_name=self.profile_name,
session_name=self.session_name,
err_msg=str(win_err)
)
else:
self.logger('Encountered WindowsError: %s' % str(win_err), loglevel=log.loglevel_ERROR)
time.sleep(20)
else:
_hr_filename = self._humanreadable_filename(pdf_file, job_title, spool_dir, )
shutil.copy2(pdf_file, _hr_filename)
cmd_line = [ self.pdfview_cmd, _hr_filename, ]
self.logger('viewing incoming PDF with command: %s' % ' '.join(cmd_line), loglevel=log.loglevel_DEBUG)
try:
subprocess.Popen(cmd_line, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=_PRINT_ENV)
except OSError, e:
if e.errno == 2:
cmd_line = [ defaults.DEFAULT_PDFVIEW_CMD, _hr_filename ]
subprocess.Popen(cmd_line, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=_PRINT_ENV)
else:
raise(e)
self.logger('waiting 20s longer before deleting the PDF file ,,%s\'\'' % _hr_filename, loglevel=log.loglevel_DEBUG)
time.sleep(20)
os.remove(_hr_filename)
class X2GoPrintActionPDFSAVE(X2GoPrintAction):
"""\
Print action that saves incoming print jobs to a local folder.
"""
__name__ = 'PDFSAVE'
__decription__= 'Save as PDF'
save_to_folder = None
def __init__(self, client_instance=None, save_to_folder=None, logger=None, loglevel=log.loglevel_DEFAULT):
"""\
@param client_instance: the underlying L{X2GoClient} instance
@type client_instance: C{obj}
@param save_to_folder: saving location for incoming print jobs (PDF files)
@type save_to_folder: C{str}
@param logger: you can pass an L{X2GoLogger} object to the
L{X2GoPrintActionPDFSAVE} constructor
@type logger: C{obj}
@param loglevel: if no L{X2GoLogger} object has been supplied a new one will be
constructed with the given loglevel
@type loglevel: C{int}
"""
if save_to_folder is None:
save_to_folder = defaults.DEFAULT_PDFSAVE_LOCATION
if not utils.is_abs_path(save_to_folder):
if not save_to_folder.startswith('~'):
save_to_folder = os.path.normpath('~/%s' % save_to_folder)
save_to_folder = os.path.expanduser(save_to_folder)
self.save_to_folder = save_to_folder
X2GoPrintAction.__init__(self, client_instance=client_instance, logger=None, loglevel=loglevel)
self.logger('Save location for incoming PDFs is: %s' % self.save_to_folder, loglevel=log.loglevel_DEBUG)
if not os.path.exists(self.save_to_folder):
os.makedirs(self.save_to_folder, mode=0755)
def _do_print(self, pdf_file, job_title, spool_dir):
"""\
Save an incoming X2Go print job (PDF file) to a local folder.
@param pdf_file: PDF file name as placed in to the X2Go spool directory
@type pdf_file: C{str}
@param job_title: human readable print job title
@type job_title: C{str}
@param spool_dir: location of the X2Go client's spool directory
@type spool_dir: C{str}
"""
pdf_file = os.path.normpath(pdf_file)
spool_dir = os.path.normpath(spool_dir)
dest_file = self._humanreadable_filename(pdf_file, job_title, target_path=self.save_to_folder)
shutil.copy2(pdf_file, dest_file)
class X2GoPrintActionPRINT(X2GoPrintAction):
"""\
Print action that actually prints an incoming print job file.
"""
__name__ = 'PRINT'
__decription__= 'UNIX/Win32GDI printing'
def __init__(self, client_instance=None, printer=None, logger=None, loglevel=log.loglevel_DEFAULT):
"""\
@param client_instance: the underlying L{X2GoClient} instance
@type client_instance: C{obj}
@param printer: name of the preferred printer, if C{None} the system's/user's default printer will be used
@type printer: C{str}
@param logger: you can pass an L{X2GoLogger} object to the
L{X2GoPrintActionPRINT} constructor
@type logger: C{obj}
@param loglevel: if no L{X2GoLogger} object has been supplied a new one will be
constructed with the given loglevel
@type loglevel: C{int}
"""
self.printer = printer
X2GoPrintAction.__init__(self, client_instance=client_instance, logger=logger, loglevel=loglevel)
def _do_print(self, pdf_file, job_title, spool_dir, ):
"""\
Actually really print an incoming X2Go print job (PDF file) to a local printer device.
@param pdf_file: PDF file name as placed in to the X2Go spool directory
@type pdf_file: C{str}
@param job_title: human readable print job title
@type job_title: C{str}
@param spool_dir: location of the X2Go client's spool directory
@type spool_dir: C{str}
"""
pdf_file = os.path.normpath(pdf_file)
spool_dir = os.path.normpath(spool_dir)
_hr_filename = self._humanreadable_filename(pdf_file, job_title, spool_dir)
if _X2GOCLIENT_OS == 'Windows':
_default_printer = win32print.GetDefaultPrinter()
if self.printer:
_printer = self.printer
win32print.SetDefaultPrinter(_printer)
else:
_printer = _default_printer
self.logger('printing incoming PDF file %s' % pdf_file, loglevel=log.loglevel_NOTICE)
self.logger('printer name is ,,%s\'\'' % _printer, loglevel=log.loglevel_DEBUG)
try:
_stdin = file('nul', 'r')
_shell = True
if self.client_instance:
_gsprint_bin = self.client_instance.client_printing.get_value('print', 'gsprint')
self.logger('Using gsprint.exe path from printing config file: %s' % _gsprint_bin, loglevel=log.loglevel_DEBUG)
else:
_program_files = os.environ['ProgramFiles']
_gsprint_bin = os.path.normpath(os.path.join(_program_files, 'ghostgum', 'gsview', 'gsprint.exe',))
self.logger('Using hard-coded gsprint.exe path: %s' % _gsprint_bin, loglevel=log.loglevel_DEBUG)
self.logger('Trying Ghostgum tool ,,gsprint.exe'' for printing first (full path: %s)' % _gsprint_bin, loglevel=log.loglevel_DEBUG)
subprocess.Popen([_gsprint_bin, pdf_file, ],
stdin=_stdin,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
shell=_shell,
)
# give gsprint.exe a little time to find our printer
time.sleep(10)
except:
self.logger('Falling back to win32api printing...', loglevel=log.loglevel_DEBUG)
try:
win32api.ShellExecute (
0,
"print",
pdf_file,
None,
".",
0
)
# give the win32api some time to find our printer...
time.sleep(10)
except win32api.error, e:
if self.client_instance:
self.client_instance.HOOK_printaction_error(filename=_hr_filename, printer=_printer, err_msg=e.message, profile_name=self.profile_name, session_name=self.session_name)
else:
self.logger('Encountered win32api.error: %s' % str(e), loglevel=log.loglevel_ERROR)
if self.printer:
win32print.SetDefaultPrinter(_default_printer)
time.sleep(60)
else:
_hr_filename = self._humanreadable_filename(pdf_file, job_title, spool_dir)
self.logger('printing incoming PDF file %s' % _hr_filename, loglevel=log.loglevel_NOTICE)
if self.printer:
self.logger('printer name is %s' % self.printer, loglevel=log.loglevel_DEBUG)
else:
self.logger('using default CUPS printer', loglevel=log.loglevel_DEBUG)
shutil.copy2(pdf_file, _hr_filename)
if self.printer is None:
cmd_line = [ 'lpr',
'-h',
'-r',
'-J%s' % job_title,
'%s' % _hr_filename,
]
else:
cmd_line = [ 'lpr',
'-h',
'-r',
'-P%s' % self.printer,
'-J%s' % job_title,
'%s' % _hr_filename,
]
self.logger('executing local print command: %s' % " ".join(cmd_line), loglevel=log.loglevel_DEBUG)
subprocess.Popen(cmd_line, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=_PRINT_ENV)
# this is nasty!!!!
self.logger('waiting 20s longer before deleting the PDF file ,,%s\'\'' % _hr_filename, loglevel=log.loglevel_DEBUG)
time.sleep(20)
try: os.remove(_hr_filename)
except OSError: pass
class X2GoPrintActionPRINTCMD(X2GoPrintAction):
"""\
Print action that calls an external command for further processing of incoming print jobs.
The print job's PDF filename will be prepended as last argument to the print command
used in L{X2GoPrintActionPRINTCMD} instances.
"""
__name__ = 'PRINTCMD'
__decription__= 'Print via a command (like LPR)'
def __init__(self, client_instance=None, print_cmd=None, logger=None, loglevel=log.loglevel_DEFAULT):
"""\
@param client_instance: the underlying L{X2GoClient} instance
@type client_instance: C{obj}
@param print_cmd: external command to be called on incoming print jobs
@type print_cmd: C{str}
@param logger: you can pass an L{X2GoLogger} object to the
L{X2GoPrintActionPRINTCMD} constructor
@type logger: C{obj}
@param loglevel: if no L{X2GoLogger} object has been supplied a new one will be
constructed with the given loglevel
@type loglevel: C{int}
"""
if print_cmd is None:
print_cmd = defaults.DEFAULT_PRINTCMD_CMD
self.print_cmd = print_cmd
X2GoPrintAction.__init__(self, client_instance=client_instance, logger=logger, loglevel=loglevel)
def _do_print(self, pdf_file, job_title, spool_dir):
"""\
Execute an external command that has been defined on construction
of this L{X2GoPrintActionPRINTCMD} instance.
@param pdf_file: PDF file name as placed in to the X2Go spool directory
@type pdf_file: C{str}
@param job_title: human readable print job title
@type job_title: C{str}
@param spool_dir: location of the X2Go client's spool directory
@type spool_dir: C{str}
"""
pdf_file = os.path.normpath(pdf_file)
spool_dir = os.path.normpath(spool_dir)
_hr_filename = self._humanreadable_filename(pdf_file, job_title, spool_dir)
shutil.copy2(pdf_file, _hr_filename)
self.logger('executing external command ,,%s\'\' on PDF file %s' % (self.print_cmd, _hr_filename), loglevel=log.loglevel_NOTICE)
cmd_line = self.print_cmd.split()
cmd_line.append(_hr_filename)
self.logger('executing external command: %s' % " ".join(cmd_line), loglevel=log.loglevel_DEBUG)
subprocess.Popen(cmd_line, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=_PRINT_ENV)
# this is nasty!!!!
self.logger('waiting 20s longer before deleting the PDF file ,,%s\'\'' % _hr_filename, loglevel=log.loglevel_DEBUG)
time.sleep(20)
try: os.remove(_hr_filename)
except OSError: pass
class X2GoPrintActionDIALOG(X2GoPrintAction):
"""\
Print action that mediates opening a print dialog window. This class is rather empty,
the actual print dialog box must be implemented in our GUI application (with the application's
L{X2GoClient} instance.
"""
__name__ = 'DIALOG'
__decription__= 'Open a print dialog box'
def __init__(self, client_instance=None, logger=None, loglevel=log.loglevel_DEFAULT):
"""\
@param client_instance: an L{X2GoClient} instance, within your customized L{X2GoClient} make sure
you have a C{HOOK_open_print_dialog(filename=<str>)} method defined that will actually
open the print dialog.
@type client_instance: C{obj}
@param logger: you can pass an L{X2GoLogger} object to the
L{X2GoPrintActionDIALOG} constructor
@type logger: C{obj}
@param loglevel: if no L{X2GoLogger} object has been supplied a new one will be
constructed with the given loglevel
@type loglevel: C{int}
@raise X2GoPrintActionException: if the client_instance has not been passed to the DIALOG print action
"""
if client_instance is None:
raise x2go_exceptions.X2GoPrintActionException('the DIALOG print action needs to know the X2GoClient instance (client=<instance>)')
X2GoPrintAction.__init__(self, client_instance=client_instance, logger=logger, loglevel=loglevel)
def _do_print(self, pdf_file, job_title, spool_dir):
"""\
Execute an external command that has been defined on construction
of this L{X2GoPrintActionPRINTCMD} instance.
@param pdf_file: PDF file name as placed in to the X2Go spool directory
@type pdf_file: C{str}
@param job_title: human readable print job title
@type job_title: C{str}
@param spool_dir: location of the X2Go client's spool directory
@type spool_dir: C{str}
"""
pdf_file = os.path.normpath(pdf_file)
spool_dir = os.path.normpath(spool_dir)
self.logger('Session %s (%s) is calling X2GoClient class hook method <client_instance>.HOOK_open_print_dialog' % (self.session_name, self.profile_name), loglevel=log.loglevel_NOTICE)
_new_print_action = self.client_instance.HOOK_open_print_dialog(profile_name=self.profile_name, session_name=self.session_name)
if _new_print_action and type(_new_print_action) != type(self):
_new_print_action._do_print(pdf_file, job_title, spool_dir)
|