Debian Codegraph @

prnt/filters/hpps

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
4 # (c) Copyright 2003-2015 HP Development Company, L.P.
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #
20 # Author: Gaurav Sood, Sanjay Kumar
21 #
22
23 __version__ = '1.1'
24 __title__ = 'PostScript Finishing Filter (hpps)'
25 __doc__ = "CUPS filter that implements job storage (secure printing), Job Accounting and Born On Date (BOD) features on specific capable printers."
26
27 # StdLib
28 import sys
29 import getopt
30 import os.path
31 import os
32 import syslog
33 import time
34 import tempfile
35 import subprocess
36 import time
37 PY3 = sys.version_info[0] == 3
38 if PY3:
39 import configparser
40 else:
41 import ConfigParser as configparser
42
43 CUPS_FILTER_OK = 0
44 CUPS_FILTER_FAILED = 1
45
46 input_fd = 0
47 output_fd = 1
48
49 job_id = 0
50 pid = os.getpid()
51 config_file = '/etc/hp/hplip.conf'
52 home_dir = ''
53
54
55 def bug(m):
56 log.stderr("ERROR: %s" % m)
57
58 def msg(m):
59 log.stderr("INFO: %s" % m)
60
61 if os.path.exists(config_file):
62 config = configparser.ConfigParser()
63 config.read(config_file)
64
65 try:
66 home_dir = config.get('dirs', 'home')
67 except:
68 bug("Error setting home directory: home= under [dirs] not found.")
69 sys.exit(CUPS_FILTER_FAILED)
70 else:
71 bug("Error setting home directory: /etc/hp/hplip.conf not found")
72 sys.exit(CUPS_FILTER_FAILED)
73
74 if not home_dir or not os.path.exists(home_dir):
75 bug("Error setting home directory: Home directory %s not found." % home_dir)
76 sys.exit(CUPS_FILTER_FAILED)
77
78 sys.path.insert(0, home_dir)
79 os.chdir(home_dir)
80
81
82 # HPLIP
83 try:
84 from base.g import *
85 from base.codes import *
86 from base import device
87 from base import utils
88 from prnt import cups
89 from base.sixext import to_bytes_utf8, to_string_utf8
90 except ImportError as e:
91 bug("Error importing HPLIP modules:%s %s\n" % (pid, e))
92 sys.exit(CUPS_FILTER_FAILED)
93
94
95 def usage(typ='text'):
96 if typ == 'text':
97 utils.log_title(__title__, __version__)
98
99 utils.format_text(USAGE, typ, title=__title__, crumb='pscfilter')
100 sys.exit(CUPS_FILTER_OK)
101
102 try:
103 opts, args = getopt.getopt(sys.argv[1:], 'l:hg', ['level=', 'help', 'help-rest', 'help-man'])
104
105 except getopt.GetoptError:
106 usage()
107
108 for o, a in opts:
109
110 if o in ('-l', '--logging'):
111 log_level = a.lower().strip()
112 log.set_level(log_level)
113
114 elif o == '-g':
115 log.set_level('debug')
116
117 elif o in ('-h', '--help'):
118 usage()
119
120 elif o == '--help-rest':
121 usage('rest')
122
123 elif o == '--help-man':
124 usage('man')
125
126
127 # CUPS provided environment
128 try:
129 ppd_file = os.environ['PPD']
130 except KeyError:
131 bug("Improper environment: Must be run by CUPS.")
132 sys.exit(CUPS_FILTER_FAILED)
133
134 try:
135 job_id, username, title, copies, options = args[0:5]
136 job_id = int(job_id)
137 except IndexError:
138 bug("Invalid command line: invalid arguments.")
139 sys.exit(CUPS_FILTER_FAILED)
140
141 START_JOB = b"\x1b%-12345X@PJL JOBNAME="
142 UEL = b"@PJL ENTER LANGUAGE=POSTSCRIPT\x0a"
143 END_JOB = b"\x1b%-12345X@PJL EOJ\x0a\x1b%-12345X"
144
145 try:
146 ppd_str = to_string_utf8(open(ppd_file, 'rb').read())
147 except IOError:
148 bug("Unabel to read PPD File")
149 sys.exit(CUPS_FILTER_FAILED)
150
151 opts = {}
152
153 for opt in options.split():
154 try:
155 o, v = opt.split('=')
156 except ValueError:
157 continue
158 else:
159 opts[o] = v
160
161 key_list = [x for x in ['HPAccountingInfo', 'HPBOD', 'HPPJLEconoMode', 'HPPJLEconoMode2', 'HPPJLOutputMode', 'HPPJLDryTime',
162 'HPPJLSaturation', 'HPPJLInkBleed', 'HPPJLColorAsGray', 'HPPJLTrueBlack','HPPJLPrintQuality'] if x in ppd_str]
163
164 #output_fd = os.open("/tmp/PSC.out", os.O_WRONLY | os.O_CREAT)
165
166 def header():
167 os.write(output_fd, START_JOB)
168 os.write(output_fd, to_bytes_utf8("hplip_%s_%s\x0a" % (username, job_id)))
169 os.write(output_fd, to_bytes_utf8('@PJL SET USERNAME="%s"\x0a' % username))
170 os.write(output_fd, to_bytes_utf8('@PJL SET JOBNAME="%s"\x0a' % title))
171
172 # --------------------Secure Printing---------------------
173 # Embeds private job PJL's into the print stream
174 # ----------------------------------------------------------
175
176 def secure_print():
177 os.write(output_fd, b"@PJL SET HOLD=ON\x0a")
178 os.write(output_fd, b"@PJL SET HOLDTYPE=PRIVATE\x0a")
179 secpin = ""
180 if "HPDigit" in ppd_str:
181 try:
182 tmp = opts['HPDigit']
183 if "Custom." in tmp:
184 tmp = tmp[7:]
185 if tmp.isdigit() and len(tmp) == 4:
186 secpin += tmp
187 else:
188 secpin = "0000"
189 except KeyError as e:
190 msg("Setting Pin to default")
191 else:
192 szKeyInitials = ['HPFIDigit', 'HPSEDigit', 'HPTHDigit', 'HPFTDigit']
193 for x in szKeyInitials:
194 try:
195 secpin += opts[x]
196 except KeyError:
197 secpin += '0'
198 os.write(output_fd, to_bytes_utf8('@PJL SET HOLDKEY="%s"\x0a' % secpin))
199
200
201 #os.write(output_fd, to_bytes_utf8('@PJL SET COPIES="%s"\x0a' % copies))
202
203 #----------------Job Accouting----------------------------
204 # Embeds the job accouting (1-8) PJL's into print stream
205 # -----------------------------------------------------
206
207 class HPPS_Filter(object):
208 def HPAccountingInfo(self):
209 p = subprocess.Popen(['hostname', '-s'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
210 system_name , err = p.communicate()
211 if system_name == '':
212 system_name = 'unknown_system_name'
213 p = subprocess.Popen(['hostname', '-d'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
214 domain_name, err = p.communicate()
215 if domain_name == '':
216 domain_name = 'unknown_domain_name'
217
218 os.write(output_fd, to_bytes_utf8('@PJL SET JOBATTR=\"JobAcct1=%s\"\x0a' % username))
219 os.write(output_fd, to_bytes_utf8('@PJL SET JOBATTR=\"JobAcct2=%s\"\x0a' % system_name.rstrip(b'\n')))
220 os.write(output_fd, to_bytes_utf8('@PJL SET JOBATTR=\"JobAcct3=%s\"\x0a' % domain_name.rstrip(b'\n')))
221 os.write(output_fd, to_bytes_utf8('@PJL SET JOBATTR=\"JobAcct4=%s\"\x0a' % time.strftime("%Y%m%d%I%M%S", time.localtime())))
222 os.write(output_fd, to_bytes_utf8('@PJL SET JOBATTR=\"JobAcct5=%s\"\x0a' % opts['job-uuid']))
223 os.write(output_fd, to_bytes_utf8('@PJL SET JOBATTR=\"JobAcct6=%s\"\x0a' % "HP Linux Printing"))
224 os.write(output_fd, to_bytes_utf8('@PJL SET JOBATTR=\"JobAcct7=%s\"\x0a' % "HP Linux Printing"))
225 os.write(output_fd, to_bytes_utf8('@PJL SET JOBATTR=\"JobAcct8=%s\"\x0a' % username))
226
227 # -------------------------Born On Date ----------------------------
228 # Embeds the born on date PJL's in to print stream. Uses GMT Time Stamp
229 # Based upon the "BornOnDate_PJL_DMINFO_PJL_TIMESTAMP.doc
230 # -----------------------------------------------------------------
231 def HPBOD(self):
232 BOD_fixed = "0400040101020D10100115" # HEX value of the time stamp OID
233 BOD_time = time.strftime("%Y%m%d%I%M%S", time.gmtime()) # Timestamp GMT
234 BOD_Var = ''
235 for x in BOD_time:
236 BOD_Var = BOD_Var + hex(ord(x))[2:]
237 os.write(output_fd, to_bytes_utf8('@PJL DMINFO ASCIIHEX=\"%s%s\"\x0A' % (BOD_fixed, BOD_Var)))
238
239 # -------------------------Other Features ----------------------------
240 def HPPJLEconoMode(self):
241 if 'HPPJLEconoMode' in options and 'noHPPJLEconoMode' not in options:
242 os.write(output_fd, b"@PJL SET ECONOMODE=ON\x0a")
243 else:
244 os.write(output_fd, b"@PJL SET ECONOMODE=OFF\x0a")
245
246 os.write(output_fd, b"@PJL SET RESOLUTION=600\x0a")
247 os.write(output_fd, b"@PJL SET BITSPERPIXEL=2\x0a")
248
249
250 def HPPJLEconoMode2(self):
251 pq = opts.get('HPPJLEconoMode2', "")
252 if pq == 'yes' or pq == 'on':
253 os.write(output_fd, b"@PJL SET ECONOMODE=ON\x0a")
254 elif pq == 'no' or pq == 'off':
255 os.write(output_fd, b"@PJL SET ECONOMODE=OFF\x0a")
256 else:
257 pass
258 os.write(output_fd, b"@PJL SET RESOLUTION=600\x0a")
259 os.write(output_fd, b"@PJL SET BITSPERPIXEL=1\x0a")
260
261 def HPPJLPrintQuality(self):
262 pq = opts.get('HPPJLPrintQuality',"")
263 if pq == 'FastRes1200':
264 os.write(output_fd, b"@PJL SET RESOLUTION=600\x0a@PJL SET BITSPERPIXEL=2\x0a")
265 elif pq == '600dpi':
266 os.write(output_fd, b"@PJL SET RESOLUTION=600\x0a@PJL SET BITSPERPIXEL=1\x0a")
267 elif pq == 'ProRes1200':
268 os.write(output_fd, b"@PJL SET RESOLUTION=1200\x0a@PJL SET BITSPERPIXEL=1\x0a")
269 else:
270 os.write(output_fd, b"@PJL SET RESOLUTION=600\x0a@PJL SET BITSPERPIXEL=2\x0a")
271
272
273 def HPPJLOutputMode(self):
274 pq = opts.get('HPPJLOutputMode', "")
275 if pq == 'GeneralOffice':
276 os.write(output_fd, b'@PJL SET PRINTQUALITY=DRAFT\x0a')
277 elif pq == 'Professional':
278 os.write(output_fd, b'@PJL SET PRINTQUALITY=NORMAL\x0a')
279 elif pq == 'Presentation':
280 os.write(output_fd, b'@PJL SET PRINTQUALITY=BEST\x0a')
281 elif pq == 'MaximumDPI':
282 os.write(output_fd, b'@PJL SET PRINTQUALITY=MAX\x0a')
283 else:
284 os.write(output_fd, b'@PJL SET PRINTQUALITY=NORMAL\x0a')
285
286 def HPPJLDryTime(self):
287 pq = opts.get('HPPJLDryTime', "")
288 if pq == '0' or pq == 'Default':
289 os.write(output_fd, b'@PJL SET DRYTIME=0\x0a')
290 elif pq == 'Medium':
291 os.write(output_fd, b'@PJL SET DRYTIME=4\x0a')
292 elif pq == 'Long':
293 os.write(output_fd, b'@PJL SET DRYTIME=7\x0a')
294 else:
295 os.write(output_fd, b'@PJL SET DRYTIME=0\x0a')
296
297 def HPPJLSaturation(self):
298 pq = opts.get('HPPJLSaturation', "")
299 if pq == '-2':
300 os.write(output_fd, b'@PJL SET SATURATION=0\x0a')
301 elif pq == '-1':
302 os.write(output_fd, b'@PJL SET SATURATION=2\x0a')
303 elif pq == '0' or pq == 'default':
304 os.write(output_fd, b'@PJL SET SATURATION=4\x0a')
305 elif pq == '+1':
306 os.write(output_fd, b'@PJL SET SATURATION=6\x0a')
307 elif pq == '+2':
308 os.write(output_fd, b'@PJL SET SATURATION=8\x0a')
309 else:
310 os.write(output_fd, b'@PJL SET SATURATION=4\x0a')
311
312 def HPPJLInkBleed(self):
313 pq = opts.get('HPPJLInkBleed', "")
314 if pq == '-2' or pq == 'Default':
315 os.write(output_fd, b'@PJL SET INKBLEED=0\x0a')
316 elif pq == '-1':
317 os.write(output_fd, b'@PJL SET INKBLEED=2\x0a')
318 elif pq == '0' or pq == 'Less':
319 os.write(output_fd, b'@PJL SET INKBLEED=4\x0a')
320 elif pq == '+1':
321 os.write(output_fd, b'@PJL SET INKBLEED=6\x0a')
322 elif pq == 'Least':
323 os.write(output_fd, b'@PJL SET INKBLEED=7\x0a')
324 elif pq == '+2':
325 os.write(output_fd, b'@PJL SET INKBLEED=8\x0a')
326 else:
327 os.write(output_fd, b'@PJL SET INKBLEED=4\x0a')
328
329 def HPPJLColorAsGray(self):
330 pq = opts.get('HPPJLColorAsGray', "")
331 if pq == 'off' or pq == 'False':
332 os.write(output_fd, b'@PJL SET GRAYSCALE=OFF\x0a')
333 elif pq == 'HighQuality':
334 os.write(output_fd, b'@PJL SET GRAYSCALE=COMPOSITE\x0a')
335 os.write(output_fd, b'@PJL SET PLANESINUSE=1\x0a')
336 elif pq == 'BlackInkOnly':
337 os.write(output_fd, b'@PJL SET GRAYSCALE=BLACKONLY\x0a')
338 os.write(output_fd, b'@PJL SET PLANESINUSE=1\x0a')
339 elif pq == 'yes' or pq == 'on' :
340 os.write(output_fd, b'@PJL SET RESOLUTION=600\x0a')
341 os.write(output_fd, b'@PJL SET BITSPERPIXEL=8\x0a')
342 os.write(output_fd, b'@PJL SET GRAYSCALE=COMPOSITE\x0a')
343 elif pq == 'no':
344 os.write(output_fd, b'@PJL SET RESOLUTION=600\x0a')
345 os.write(output_fd, b'@PJL SET BITSPERPIXEL=8\x0a')
346 elif pq == 'Plain' or pq == 'True': # for device 586 and device 586 flow
347 os.write(output_fd, b'@PJL SET PLANESINUSE=1\x0a')
348 # elif pq == 'check': # for device 556 no value is passed
349 # os.write(output_fd, b'')
350 else:
351 os.write(output_fd, b'@PJL SET GRAYSCALE=OFF\x0a')
352
353 def HPPJLTrueBlack(self):
354 pq = opts.get('HPPJLTrueBlack', "")
355 if pq == 'HighQuality':
356 os.write(output_fd, b'@PJL SET GRAYSCALE=COMPOSITE\x0a')
357 elif pq == 'BlackInkOnly':
358 os.write(output_fd, b'@PJL SET GRAYSCALE=BLACKONLY\x0a')
359 else:
360 os.write(output_fd, b'@PJL SET GRAYSCALE=BLACKONLY\x0a')
361
362 try:
363 header()
364 if 'HPPinPrnt' in options and 'noHPPinPrnt' not in options:
365 secure_print()
366 hpps_filter = HPPS_Filter()
367 for x in key_list:
368 (getattr(hpps_filter, x))()
369
370 os.write(output_fd, UEL)
371 while True:
372 data = os.read(0, 4096)
373 if not data:
374 break
375 os.write(output_fd, data)
376 os.write(output_fd, END_JOB)
377
378 except:
379 bug('HPPS filter failure')
380 sys.exit(CUPS_FILTER_FAILED)
381 sys.exit(CUPS_FILTER_OK)
382
383
384
385