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
|
# -*- coding: utf-8 -*-
# Copyright (C) 2010-2023 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.
"""\
:class:`x2go.backends.printing.file.X2GoClientPrinting` class is one of Python X2Go's public API classes.
Retrieve an instance of this class from your :class:`x2go.client.X2GoClient` instance.
Use this class in your Python X2Go based applications to access the »printing«
configuration of your X2Go client application.
"""
__NAME__ = 'x2goprinting-pylib'
__package__ = 'x2go.backends.printing'
__name__ = 'x2go.backends.printing.file'
# modules
import types
import sys
# Python X2Go modules
import x2go.log as log
import x2go.printactions as printactions
# we hide the default values from epydoc (that's why we transform them to _UNDERSCORE variables)
from x2go.defaults import X2GO_CLIENTPRINTING_DEFAULTS as _X2GO_CLIENTPRINTING_DEFAULTS
from x2go.defaults import X2GO_PRINTING_CONFIGFILES as _X2GO_PRINTING_CONFIGFILES
import x2go.inifiles as inifiles
import x2go.x2go_exceptions as x2go_exceptions
_print_property_map = {
'pdfview_cmd': {
'ini_section': 'view',
'ini_option': 'command',
},
'save_to_folder': {
'ini_section': 'save',
'ini_option': 'folder',
},
'printer': {
'ini_section': 'CUPS',
'ini_option': 'defaultprinter',
},
'print_cmd': {
'ini_section': 'print',
'ini_option': 'command',
},
}
class X2GoClientPrinting(inifiles.X2GoIniFile):
"""\
:class:`x2go.backends.printing.file.X2GoClientPrinting` provides access to the X2Go ini-like file
»printing« as stored in ``~/.x2goclient/printing`` resp. globally
``/etc/x2goclient/printing``.
An instance of :class:`x2go.backends.printing.file.X2GoClientPrinting` is created on each incoming
print job. This facilitates that on every print job the print action
for this job is derived from the »printing« configuration file.
Thus, changes on the file are active for the next incoming print job.
"""
config_files = []
_print_action = None
def __init__(self, config_files=_X2GO_PRINTING_CONFIGFILES, defaults=_X2GO_CLIENTPRINTING_DEFAULTS, client_instance=None, logger=None, loglevel=log.loglevel_DEFAULT):
"""\
:param config_files: a list of configuration files names (e.g. a global filename and a user's home
directory filename)
:type config_files: ``list``
:param defaults: a cascaded Python dicitionary structure with ini file defaults (to override
Python X2Go's hard coded defaults in :mod:`x2go.defaults`
:type defaults: ``dict``
:param logger: you can pass an :class:`x2go.log.X2GoLogger` object to the
:class:`x2go.printactions.X2GoPrintAction` constructor
:type logger: ``obj``
:param loglevel: if no :class:`x2go.log.X2GoLogger` object has been supplied a new one will be
constructed with the given loglevel
:type loglevel: ``int``
"""
self.client_instance = client_instance
inifiles.X2GoIniFile.__init__(self, config_files, defaults=defaults, logger=logger, loglevel=loglevel)
self._detect_print_action()
def _detect_print_action(self):
"""\
Derive a print action from sections, keys and their values in a typical
X2Go client »printing« configuration file.
"""
_general_pdfview = self.get('General', 'pdfview', key_type=bool)
_view_open = self.get('view', 'open', key_type=bool)
_print_startcmd = self.get('print', 'startcmd', key_type=bool)
_show_dialog = self.get('General', 'showdialog', key_type=bool)
if _show_dialog and self.client_instance is not None:
self._print_action = printactions.X2GoPrintActionDIALOG(client_instance=self.client_instance, logger=self.logger)
elif _general_pdfview and _view_open:
_view_command = self.get('view', 'command')
self._print_action = printactions.X2GoPrintActionPDFVIEW(client_instance=self.client_instance, pdfview_cmd=_view_command, logger=self.logger)
elif _general_pdfview and not _view_open:
_safe_folder = self.get('save', 'folder')
self._print_action = printactions.X2GoPrintActionPDFSAVE(client_instance=self.client_instance, save_to_folder=_safe_folder, logger=self.logger)
elif not _general_pdfview and not _print_startcmd:
_cups_defaultprinter = self.get('CUPS', 'defaultprinter')
self._print_action = printactions.X2GoPrintActionPRINT(client_instance=self.client_instance, printer=_cups_defaultprinter, logger=self.logger)
elif not _general_pdfview and _print_startcmd:
_print_command = self.get('print', 'command')
self._print_action = printactions.X2GoPrintActionPRINTCMD(client_instance=self.client_instance, print_cmd=_print_command, logger=self.logger)
@property
def print_action(self):
"""\
Return the print action described by the »printing« configuration file.
This method has property status and wraps around the :func:`get_print_action()`
:returns: Returns the print action object
:rtype: ``obj`` or ``str``
"""
return self.get_print_action()
def get_print_action(self, reload=False, reinit=False, return_name=False):
"""\
Return the print action described by the »printing« configuration file.
:param reload: reload the configuration file before retrieving the print action? (Default value = False)
:type reload: ``bool``
:param reinit: re-detect the print action from what is stored in cache? (Default value = False)
:type reinit: ``bool``
:param return_name: return the print action name, not the class (Default value = False)
:type return_name: ``bool``
:returns: the configured print action
:rtype: ``obj`` or ``str``
"""
if reload:
self.load()
if reinit:
self._detect_print_action()
if return_name:
return self._print_action.__name__
else:
return self._print_action
def get_property(self, print_property):
"""\
Retrieve a printing property as mapped by the :func:`_print_property_map()` dictionary.
:param print_property: a printing property
:type print_property: ``str``
:returns: the stored value for ``<print_property>``
:rtype: ``str``
:raises X2GoClientPrintingException: if the printing property does not exist
"""
if print_property in list(_print_property_map.keys()):
_ini_section = _print_property_map[print_property]['ini_section']
_ini_option = _print_property_map[print_property]['ini_option']
return self.get_value(_ini_section, _ini_option)
else:
raise x2go_exceptions.X2GoClientPrintingException('No such X2Go client printing property ,,%s\'\'' % print_property)
def set_property(self, print_property, value):
"""\
Set a printing property as mapped by the :func:`_print_property_map()` dictionary.
:param print_property: a printing property
:type print_property: ``str``
:param value: the value to be stored as ``<print_property>``
:type value: ``str``
:raises X2GoClientPrintingException: if the printing property does not exist or if there is a type mismatch
"""
if print_property in list(_print_property_map.keys()):
_ini_section = _print_property_map[print_property]['ini_section']
_ini_option = _print_property_map[print_property]['ini_option']
_default_type = self.get_type(_ini_section, _ini_option)
if sys.version_info[0] < 3:
if type(value) is str:
value = value.encode('utf-8')
if _default_type != type(value):
raise x2go_exceptions.X2GoClientPrintingException('Type mismatch error for property ,,%s\'\' - is: %s, should be: %s' % (print_property, str(type(value)), str(_default_type)))
self.update_value(_ini_section, _ini_option, value)
else:
raise x2go_exceptions.X2GoClientPrintingException('No such X2Go client printing property ,,%s\'\'' % print_property)
def store_print_action(self, print_action, **print_properties):
"""\
Accept a new print action configuration. This includes the print action
itself (DIALOG, PDFVIEW, PDFSAVE, PRINT or PRINTCMD) and related printing properties
as mapped by the :func:`_print_property_map()` dictionary.
:param print_action: the print action name
:type print_action: ``str``
:param print_properties: the printing properties to set for the given print action
:type print_properties: ``dict``
"""
if print_action == 'DIALOG':
self.update_value('General', 'showdialog', True)
else:
self.update_value('General', 'showdialog', False)
if print_action == 'PDFVIEW':
self.update_value('General', 'pdfview', True)
self.update_value('view', 'open', True)
elif print_action == 'PDFSAVE':
self.update_value('General', 'pdfview', True)
self.update_value('view', 'open', False)
elif print_action == 'PRINT':
self.update_value('General', 'pdfview', False)
self.update_value('print', 'startcmd', False)
elif print_action == 'PRINTCMD':
self.update_value('General', 'pdfview', False)
self.update_value('print', 'startcmd', True)
for print_property in list(print_properties.keys()):
self.set_property(print_property, print_properties[print_property])
|