scripts/impression/filter_hp.py
2015-04-14 00:00:45 +02:00

122 lines
3.5 KiB
Python
Executable file

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import subprocess
import sys
DEFAULT_BAC = 7
# Commande ghostscript de conversion en PCL
params = ['gs', '-dNOPAUSE', '-dBATCH', '-sDEVICE=pxlmono',
#'-sPAPERSIZE=A4',
'-dDuplex=true',
'-dMediaPosition=%d' % DEFAULT_BAC,
'-sOutputFile=%stdout%',
'-sstdout=/dev/null',
'-f', # To be continued
]
def tweak(source, dest, **opt_):
"""Tweak un fichier pcl (source est un descripteur de fichier en lecture)
en rajoutant les options PJL nécessaires, et en modifiant le PCL
pour que les deux premières pages (première feuille) soient prises depuis
un autre bac"""
raw = False
opt = {
# Nombre de pages depuis le début à piocher depuis bac séparé
'manual_feed_count': 0,
'copies': 1,
'finish': 'STAPLE',
#'hold': 'STORE',
'hold': 'OFF',
'jobname': 'None',
'staple': 'NONE',
'hole': 'NONE',
}
opt.update(opt_)
while not raw:
l = source.readline()
if l.startswith('@PJL ENTER LANGUAGE'):
raw = True # Derniere ligne avant les trucs degueux
dest.write("""@PJL SET JOBNAME = "%(jobname)s"
@PJL SET HOLD = "%(hold)s"
@PJL SET QTY = %(copies)d
@PJL SET PROCESSINGACTION=APPEND
@PJL SET PROCESSINGTYPE="STAPLING"
@PJL SET PROCESSINGOPTION="%(staple)s"
@PJL SET PROCESSINGBOUNDARY=MOPY
@PJL SET PROCESSINGTYPE="PUNCH"
@PJL SET PROCESSINGOPTION="%(hole)s"
@PJL SET PROCESSINGBOUNDARY=MOPY
""" % opt)
dest.write(l)
# Entrée manuelle sur les n premières pages. On remplace n fois
s_string = '\xf8\x25\xc0' + chr(DEFAULT_BAC)
r_string = '\xf8\x25\xc0\x01'
count = opt['manual_feed_count']
#
while True:
x = source.read(102400)
#print "Read 100ko"
#sys.stdout.flush()
if not x:
return
while count:
y = x.replace(s_string, r_string, 1)
if x != y:
x = y
count -= 1
else:
break
dest.write(x)
if __name__ == '__main__':
opt = {}
try:
opt['jobname'] = sys.argv[3]
except ValueError:
pass
# Pour le nombre de copies
opt['copies'] = int(sys.argv[4])
# Pour les agraphes et les trous
optns = sys.argv[5].split()
for options in optns:
option = options.split("=")
if option[0]=="HPPunchingOptions":
if option[1]=="2HolePunchLeft":
opt['hole']= "LEFT_2PT_DIN"
elif option[1]=="2HolePunchRight":
opt['hole']= "RIGHT_2PT_DIN"
elif option[1]=="2HolePunchTop":
opt['hole']= "TOP_2PT_DIN"
elif option[1]=="2HolePunchBottom":
opt['hole']= "BT_2PT_DIN"
elif option[1]=="4HolePunchLeft":
opt['hole']= "LEFT_4PT_DIN"
elif option[1]=="4HolePunchRight":
opt['hole']= "RIGHT_4PT_DIN"
if option[0]=="HPStaplerOptions":
if option[1]=="TopLeft":
opt['staple']= "LEFT_1PT_ANGLED"
elif option[1]=="TopRight":
opt['staple']= "RIGHT_1PT_ANGLED"
elif option[1]=="Left":
opt['staple']= "LEFT_2PT"
elif option[1]=="Right":
opt['staple']= "RIGHT_2PT"
fname = '-'
proc = subprocess.Popen(params + [fname],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
stdin=sys.stdin)
tweak(proc.stdout, sys.stdout, **opt)