1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 """\
21 L{X2GoClientPrinting} class is one of Python X2Go's public API classes.
22
23 Retrieve an instance of this class from your L{X2GoClient} instance.
24 Use this class in your Python X2Go based applications to access the »printing«
25 configuration of your X2Go client application.
26
27 """
28 __NAME__ = 'x2goprinting-pylib'
29
30
31 import types
32
33
34 import x2go.log as log
35 import x2go.printactions as printactions
36
37 from x2go.defaults import X2GO_CLIENTPRINTING_DEFAULTS as _X2GO_CLIENTPRINTING_DEFAULTS
38 from x2go.defaults import X2GO_PRINTING_CONFIGFILES as _X2GO_PRINTING_CONFIGFILES
39 import x2go.inifiles as inifiles
40 import x2go.x2go_exceptions as x2go_exceptions
41
42 _print_property_map = {
43 'pdfview_cmd': {
44 'ini_section': 'view',
45 'ini_option': 'command',
46 },
47 'save_to_folder': {
48 'ini_section': 'save',
49 'ini_option': 'folder',
50 },
51 'printer': {
52 'ini_section': 'CUPS',
53 'ini_option': 'defaultprinter',
54 },
55 'print_cmd': {
56 'ini_section': 'print',
57 'ini_option': 'command',
58 },
59 }
62 """\
63 L{x2go.backends.printing.file.X2GoClientPrinting} provides access to the X2Go ini-like file
64 »printing« as stored in C{~/.x2goclient/printing} resp. globally
65 C{/etc/x2goclient/printing}.
66
67 An instance of L{x2go.backends.printing.file.X2GoClientPrinting} is created on each incoming
68 print job. This facilitates that on every print job the print action
69 for this job is derived from the »printing« configuration file.
70
71 Thus, changes on the file are active for the next incoming print job.
72
73 """
74 config_files = []
75 _print_action = None
76
77 - def __init__(self, config_files=_X2GO_PRINTING_CONFIGFILES, defaults=_X2GO_CLIENTPRINTING_DEFAULTS, client_instance=None, logger=None, loglevel=log.loglevel_DEFAULT):
78 """\
79 @param config_files: a list of configuration files names (e.g. a global filename and a user's home
80 directory filename)
81 @type config_files: C{list}
82 @param defaults: a cascaded Python dicitionary structure with ini file defaults (to override
83 Python X2Go's hard coded defaults in L{defaults}
84 @type defaults: C{dict}
85 @param logger: you can pass an L{X2GoLogger} object to the
86 L{X2GoPrintAction} constructor
87 @type logger: C{obj}
88 @param loglevel: if no L{X2GoLogger} object has been supplied a new one will be
89 constructed with the given loglevel
90 @type loglevel: C{int}
91
92 """
93 self.client_instance = client_instance
94 inifiles.X2GoIniFile.__init__(self, config_files, defaults=defaults, logger=logger, loglevel=loglevel)
95
96 self._detect_print_action()
97
99 """\
100 Derive a print action from sections, keys and their values in a typical
101 X2Go client »printing« configuration file.
102
103 """
104 _general_pdfview = self.get('General', 'pdfview', key_type=types.BooleanType)
105 _view_open = self.get('view', 'open', key_type=types.BooleanType)
106 _print_startcmd = self.get('print', 'startcmd', key_type=types.BooleanType)
107 _show_dialog = self.get('General', 'showdialog', key_type=types.BooleanType)
108
109 if _show_dialog and self.client_instance is not None:
110 self._print_action = printactions.X2GoPrintActionDIALOG(client_instance=self.client_instance, logger=self.logger)
111
112 elif _general_pdfview and _view_open:
113 _view_command = self.get('view', 'command')
114 self._print_action = printactions.X2GoPrintActionPDFVIEW(client_instance=self.client_instance, pdfview_cmd=_view_command, logger=self.logger)
115
116 elif _general_pdfview and not _view_open:
117 _safe_folder = self.get('save', 'folder')
118 self._print_action = printactions.X2GoPrintActionPDFSAVE(client_instance=self.client_instance, save_to_folder=_safe_folder, logger=self.logger)
119
120 elif not _general_pdfview and not _print_startcmd:
121 _cups_defaultprinter = self.get('CUPS', 'defaultprinter')
122 self._print_action = printactions.X2GoPrintActionPRINT(client_instance=self.client_instance, printer=_cups_defaultprinter, logger=self.logger)
123
124 elif not _general_pdfview and _print_startcmd:
125 _print_command = self.get('print', 'command')
126 self._print_action = printactions.X2GoPrintActionPRINTCMD(client_instance=self.client_instance, print_cmd=_print_command, logger=self.logger)
127
128 @property
130 """\
131 Return the print action described by the »printing« configuration file.
132
133 This method has property status and wraps around the L{get_print_action}
134 method.
135
136 """
137 return self.get_print_action()
138
140 """\
141 Return the print action described by the »printing« configuration file.
142
143 @param reload: reload the configuration file before retrieving the print action?
144 @type reload: C{bool}
145 @param reinit: re-detect the print action from what is stored in cache?
146 @type reinit: C{bool}
147 @param return_name: return the print action name, not the class
148 @type return_name: C{bool}
149
150 @return: the configured print action
151 @rtype: C{obj} or C{str}
152
153 """
154 if reload:
155 self.load()
156
157 if reinit:
158 self._detect_print_action()
159
160 if return_name:
161 return self._print_action.__name__
162 else:
163 return self._print_action
164
166 """\
167 Retrieve a printing property as mapped by the L{_print_property_map} dictionary.
168
169 @param print_property: a printing property
170 @type print_property: C{str}
171
172 @return: the stored value for C{<print_property>}
173 @rtype: C{str}
174
175 @raise X2GoClientPrintingException: if the printing property does not exist
176
177 """
178 if print_property in _print_property_map.keys():
179 _ini_section = _print_property_map[print_property]['ini_section']
180 _ini_option = _print_property_map[print_property]['ini_option']
181 return self.get_value(_ini_section, _ini_option)
182 else:
183 raise x2go_exceptions.X2GoClientPrintingException('No such X2Go client printing property ,,%s\'\'' % print_property)
184
186 """\
187 Set a printing property as mapped by the L{_print_property_map} dictionary.
188
189 @param print_property: a printing property
190 @type print_property: C{str}
191 @param value: the value to be stored as C{<print_property>}
192 @rtype: C{str}
193
194 @raise X2GoClientPrintingException: if the printing property does not exist or if there is a type mismatch
195
196 """
197 if print_property in _print_property_map.keys():
198 _ini_section = _print_property_map[print_property]['ini_section']
199 _ini_option = _print_property_map[print_property]['ini_option']
200 _default_type = self.get_type(_ini_section, _ini_option)
201 if type(value) is types.UnicodeType:
202 value = value.encode('utf-8')
203 if _default_type != type(value):
204 raise x2go_exceptions.X2GoClientPrintingException('Type mismatch error for property ,,%s\'\' - is: %s, should be: %s' % (print_property, str(type(value)), str(_default_type)))
205 self.update_value(_ini_section, _ini_option, value)
206 else:
207 raise x2go_exceptions.X2GoClientPrintingException('No such X2Go client printing property ,,%s\'\'' % print_property)
208
210 """\
211 Accept a new print action configuration. This includes the print action
212 itself (DIALOG, PDFVIEW, PDFSAVE, PRINT or PRINTCMD) and related printing properties
213 as mapped by the L{_print_property_map} dictionary.
214
215 @param print_action: the print action name
216 @type print_action: C{str}
217 @param print_properties: the printing properties to set for the given print action
218 @type print_properties: C{dict}
219
220 """
221 if print_action == 'DIALOG':
222 self.update_value('General', 'showdialog', True)
223 else:
224 self.update_value('General', 'showdialog', False)
225
226 if print_action == 'PDFVIEW':
227 self.update_value('General', 'pdfview', True)
228 self.update_value('view', 'open', True)
229
230 elif print_action == 'PDFSAVE':
231 self.update_value('General', 'pdfview', True)
232 self.update_value('view', 'open', False)
233
234 elif print_action == 'PRINT':
235 self.update_value('General', 'pdfview', False)
236 self.update_value('print', 'startcmd', False)
237
238 elif print_action == 'PRINTCMD':
239 self.update_value('General', 'pdfview', False)
240 self.update_value('print', 'startcmd', True)
241
242 for print_property in print_properties.keys():
243 self.set_property(print_property, print_properties[print_property])
244