1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 """\
21 L{X2goClientPrintingFILE} 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{X2goClientPrintingFILE} 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{X2goClientPrintingFILE} 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 defaultValues = _X2GO_CLIENTPRINTING_DEFAULTS
77
78 - def __init__(self, config_files=_X2GO_PRINTING_CONFIGFILES, defaults=None, client_instance=None, logger=None, loglevel=log.loglevel_DEFAULT):
79 """\
80 @param config_files: a list of configuration files names (e.g. a global filename and a user's home
81 directory filename)
82 @type config_files: C{list}
83 @param defaults: a cascaded Python dicitionary structure with ini file defaults (to override
84 Python X2go's hard coded defaults in L{defaults}
85 @type defaults: C{dict}
86 @param logger: you can pass an L{X2goLogger} object to the
87 L{X2goPrintAction} constructor
88 @type logger: C{obj}
89 @param loglevel: if no L{X2goLogger} object has been supplied a new one will be
90 constructed with the given loglevel
91 @type loglevel: C{int}
92
93 """
94 self.client_instance = client_instance
95 inifiles.X2goIniFile.__init__(self, config_files, defaults=defaults, logger=logger, loglevel=loglevel)
96
97 self._detect_print_action()
98
100 """\
101 Derive a print action from sections, keys and their values in a typical
102 X2Go client »printing« configuration file.
103
104 """
105 _general_pdfview = self.get('General', 'pdfview', key_type=types.BooleanType)
106 _view_open = self.get('view', 'open', key_type=types.BooleanType)
107 _print_startcmd = self.get('print', 'startcmd', key_type=types.BooleanType)
108 _show_dialog = self.get('General', 'showdialog', key_type=types.BooleanType)
109
110 if _show_dialog and self.client_instance is not None:
111 self._print_action = printactions.X2goPrintActionDIALOG(client_instance=self.client_instance, logger=self.logger)
112
113 elif _general_pdfview and _view_open:
114 _view_command = self.get('view', 'command')
115 self._print_action = printactions.X2goPrintActionPDFVIEW(client_instance=self.client_instance, pdfview_cmd=_view_command, logger=self.logger)
116
117 elif _general_pdfview and not _view_open:
118 _safe_folder = self.get('save', 'folder')
119 self._print_action = printactions.X2goPrintActionPDFSAVE(client_instance=self.client_instance, save_to_folder=_safe_folder, logger=self.logger)
120
121 elif not _general_pdfview and not _print_startcmd:
122 _cups_defaultprinter = self.get('CUPS', 'defaultprinter')
123 self._print_action = printactions.X2goPrintActionPRINT(client_instance=self.client_instance, printer=_cups_defaultprinter, logger=self.logger)
124
125 elif not _general_pdfview and _print_startcmd:
126 _print_command = self.get('print', 'command')
127 self._print_action = printactions.X2goPrintActionPRINTCMD(client_instance=self.client_instance, print_cmd=_print_command, logger=self.logger)
128
129 @property
131 """\
132 Return the print action described by the »printing« configuration file.
133
134 This method has property status and wraps around the L{get_print_action}
135 method.
136
137 """
138 return self.get_print_action()
139
141 """\
142 Return the print action described by the »printing« configuration file.
143
144 @param reload: reload the configuration file before retrieving the print action?
145 @type reload: C{bool}
146 @param reinit: re-detect the print action from what is stored in cache?
147 @type reinit: C{bool}
148 @param return_name: return the print action name, not the class
149 @type return_name: C{bool}
150
151 @return: the configured print action
152 @rtype: C{obj} or C{str}
153
154 """
155 if reload:
156 self.load()
157
158 if reinit:
159 self._detect_print_action()
160
161 if return_name:
162 return self._print_action.__name__
163 else:
164 return self._print_action
165
167 """\
168 Retrieve a printing property as mapped by the L{_print_property_map} dictionary.
169
170 @param print_property: a printing property
171 @type print_property: C{str}
172
173 @return: the stored value for C{<print_property>}
174 @rtype: C{str}
175
176 @raise X2goClientPrintingException: if the printing property does not exist
177
178 """
179 if print_property in _print_property_map.keys():
180 _ini_section = _print_property_map[print_property]['ini_section']
181 _ini_option = _print_property_map[print_property]['ini_option']
182 return self.get_value(_ini_section, _ini_option)
183 else:
184 raise x2go_exceptions.X2goClientPrintingException('No such X2Go client printing property ,,%s\'\'' % print_property)
185
187 """\
188 Set a printing property as mapped by the L{_print_property_map} dictionary.
189
190 @param print_property: a printing property
191 @type print_property: C{str}
192 @param value: the value to be stored as C{<print_property>}
193 @rtype: C{str}
194
195 @raise X2goClientPrintingException: if the printing property does not exist or if there is a type mismatch
196
197 """
198 if print_property in _print_property_map.keys():
199 _ini_section = _print_property_map[print_property]['ini_section']
200 _ini_option = _print_property_map[print_property]['ini_option']
201 _default_type = self.get_type(_ini_section, _ini_option)
202 if type(value) is types.UnicodeType:
203 value = value.encode('utf-8')
204 if _default_type != type(value):
205 raise x2go_exceptions.X2goClientPrintingException('Type mismatch error for property ,,%s\'\' - is: %s, should be: %s' % (print_property, str(type(value)), str(_default_type)))
206 self.update_value(_ini_section, _ini_option, value)
207 else:
208 raise x2go_exceptions.X2goClientPrintingException('No such X2Go client printing property ,,%s\'\'' % print_property)
209
211 """\
212 Accept a new print action configuration. This includes the print action
213 itself (DIALOG, PDFVIEW, PDFSAVE, PRINT or PRINTCMD) and related printing properties
214 as mapped by the L{_print_property_map} dictionary.
215
216 @param print_action: the print action name
217 @type print_action: C{str}
218 @param print_properties: the printing properties to set for the given print action
219 @type print_properties: C{dict}
220
221 """
222 if print_action == 'DIALOG':
223 self.update_value('General', 'showdialog', True)
224 else:
225 self.update_value('General', 'showdialog', False)
226
227 if print_action == 'PDFVIEW':
228 self.update_value('General', 'pdfview', True)
229 self.update_value('view', 'open', True)
230
231 elif print_action == 'PDFSAVE':
232 self.update_value('General', 'pdfview', True)
233 self.update_value('view', 'open', False)
234
235 elif print_action == 'PRINT':
236 self.update_value('General', 'pdfview', False)
237 self.update_value('print', 'startcmd', False)
238
239 elif print_action == 'PRINTCMD':
240 self.update_value('General', 'pdfview', False)
241 self.update_value('print', 'startcmd', True)
242
243 for print_property in print_properties.keys():
244 self.set_property(print_property, print_properties[print_property])
245