/usr/share/pyshared/pkpgpdls/qpdl.py is in pkpgcounter 3.50-7.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 | # -*- coding: ISO-8859-15 -*-
#
# pkpgcounter : a generic Page Description Language parser
#
# (c) 2003, 2004, 2005, 2006, 2007 Jerome Alet <alet@librelogiciel.com>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# $Id: qpdl.py 374 2007-12-09 14:26:15Z jerome $
#
"""This modules implements a page counter for QPDL (aka SPL2) documents."""
import sys
import os
import mmap
from struct import unpack
import pdlparser
import pjl
class Parser(pdlparser.PDLParser) :
"""A parser for QPDL (aka SPL2) documents."""
format = "QPDL (aka SPL2)"
mediasizes = {
# The first values are identical to that of PCLXL
0 : "Letter",
1 : "Legal",
2 : "A4",
3 : "Executive",
4 : "Ledger",
5 : "A3",
6 : "COM10Envelope",
7 : "MonarchEnvelope",
8 : "C5Envelope",
9 : "DLEnvelope",
10 : "JB4",
11 : "JB5",
12 : "B5Envelope",
12 : "B5",
14 : "JPostcard",
15 : "JDoublePostcard",
16 : "A5",
17 : "A6",
18 : "JB6",
21 : "Custom",
23 : "C6",
24 : "Folio",
}
mediasources = {
# Again, values are identical to that of PCLXL
0 : "Default",
1 : "Auto",
2 : "Manual",
3 : "MultiPurpose",
4 : "UpperCassette",
5 : "LowerCassette",
6 : "EnvelopeTray",
7 : "ThirdCassette",
}
def isValid(self) :
"""Returns True if data is QPDL aka SPL2, else False."""
if ((self.firstblock[:128].find("\033%-12345X") != -1) and \
((self.firstblock.find("LANGUAGE=QPDL") != -1) or \
(self.firstblock.find("LANGUAGE = QPDL") != -1))) :
return True
else :
return False
def beginPage(self, nextpos) :
"""Indicates the beginning of a new page, and extracts media information."""
self.pagecount += 1
copies = unpack(self.unpackShort, self.minfile[nextpos+1:nextpos+3])[0]
mediasize = ord(self.minfile[nextpos+3])
mediasource = ord(self.minfile[nextpos+8])
duplexmode = unpack(self.unpackShort, self.minfile[nextpos+10:nextpos+12])[0]
self.pages[self.pagecount] = { "copies" : copies,
"mediasize" : self.mediasizes.get(mediasize, str(mediasize)),
"mediasource" : self.mediasources.get(mediasource, str(mediasource)),
"duplex" : duplexmode,
}
return 16 # Length of a page header
def endPage(self, nextpos) :
"""Indicates the end of a page."""
epcopies = unpack(self.unpackShort, self.minfile[nextpos:nextpos+2])[0]
bpcopies = self.pages[self.pagecount]["copies"]
if epcopies != bpcopies :
self.logdebug("ERROR: discrepancy between beginPage (%i) and endPage (%i) copies" % (bpcopies, epcopies))
return 2 # Length of a page footer
def beginBand(self, nextpos) :
"""Indicates the beginning of a new band."""
bandlength = unpack(self.unpackLong, self.minfile[nextpos+6:nextpos+10])[0]
return bandlength + 10 # Length of a band header - length of checksum
def littleEndian(self) :
"""Toggles to little endianness."""
self.unpackType = { 1 : "B", 2 : "<H", 4 : "<I" }
self.unpackShort = self.unpackType[2]
self.unpackLong = self.unpackType[4]
return 0
def bigEndian(self) :
"""Toggles to big endianness."""
self.unpackType = { 1 : "B", 2 : ">H", 4 : ">I" }
self.unpackShort = self.unpackType[2]
self.unpackLong = self.unpackType[4]
return 0
def escape(self, nextpos) :
"""Handles the ESC code."""
pos = endpos = nextpos
minfile = self.minfile
if minfile[pos : pos+8] == r"%-12345X" :
endpos = pos + 9
endmark = chr(0x0c) + chr(0x00) + chr(0x1b)
asciilimit = chr(0x80)
quotes = 0
while (minfile[endpos] not in endmark) and \
((minfile[endpos] < asciilimit) or (quotes % 2)) :
if minfile[endpos] == '"' :
quotes += 1
endpos += 1
# Store this in a per page mapping.
# NB : First time will be at page 0 (i.e. **before** page 1) !
stuff = self.escapedStuff.setdefault(self.pagecount, [])
stuff.append(minfile[pos : endpos])
self.logdebug("Escaped datas : [%s]" % repr(minfile[pos : endpos]))
return endpos - pos
def maybeEOF(self, nextpos) :
"""Tries to detect the EOF marker."""
if self.minfile[nextpos:nextpos+9] == self.eofmarker :
return 9
else :
return 0
def getJobSize(self) :
"""Counts pages in a QPDL (SPL2) document.
Algorithm by Jerome Alet.
The documentation used for this was :
Spécification Technique (documentation non officielle)
Le Language SPL2
par Aurélien Croc
http://splix.ap2c.org
"""
# Initialize table of tags
self.tags = [ lambda pos : 0 ] * 256
self.tags[0x00] = self.beginPage
self.tags[0x01] = self.endPage
self.tags[0x09] = self.maybeEOF
self.tags[0x0c] = self.beginBand
self.tags[0x1b] = self.escape # The escape code
self.eofmarker = "\033%-12345X"
infileno = self.infile.fileno()
self.pages = { 0 : { "copies" : 1,
"orientation" : "Default",
"mediatype" : "Plain",
"mediasize" : "Default",
"mediasource" : "Default",
"duplex" : None,
}
}
self.minfile = minfile = mmap.mmap(infileno, os.fstat(infileno)[6], prot=mmap.PROT_READ, flags=mmap.MAP_SHARED)
self.pagecount = 0
self.escapedStuff = {} # For escaped datas, mostly PJL commands
self.bigEndian()
pos = 0
tags = self.tags
try :
try :
while 1 :
tag = ord(minfile[pos])
pos += 1
pos += tags[tag](pos)
except IndexError : # EOF ?
pass
finally :
self.minfile.close()
defaultduplexmode = "Simplex"
defaultpapersize = ""
defaultpjlcopies = 1
oldpjlcopies = -1
oldduplexmode = ""
oldpapersize = ""
for pnum in range(1, self.pagecount + 1) :
# NB : is number of copies is 0, the page won't be output
# but the formula below is still correct : we want
# to decrease the total number of pages in this case.
page = self.pages.get(pnum, self.pages.get(1, { "copies" : 1, "mediasize" : "Default", "duplex" : None }))
pjlstuff = self.escapedStuff.get(pnum, self.escapedStuff.get(0, []))
if pjlstuff :
pjlparser = pjl.PJLParser("".join(pjlstuff))
nbdefaultcopies = int(pjlparser.default_variables.get("COPIES", -1))
nbcopies = int(pjlparser.environment_variables.get("COPIES", -1))
nbdefaultqty = int(pjlparser.default_variables.get("QTY", -1))
nbqty = int(pjlparser.environment_variables.get("QTY", -1))
if nbdefaultcopies > -1 :
defaultpjlcopies = nbdefaultcopies
if nbdefaultqty > -1 :
defaultpjlcopies = nbdefaultqty
if nbcopies > -1 :
pjlcopies = nbcopies
elif nbqty > -1 :
pjlcopies = nbqty
else :
if oldpjlcopies == -1 :
pjlcopies = defaultpjlcopies
else :
pjlcopies = oldpjlcopies
if page["duplex"] :
duplexmode = page["duplex"]
else :
defaultdm = pjlparser.default_variables.get("DUPLEX", "")
if defaultdm :
if defaultdm.upper() == "ON" :
defaultduplexmode = "Duplex"
else :
defaultduplexmode = "Simplex"
envdm = pjlparser.environment_variables.get("DUPLEX", "")
if envdm :
if envdm.upper() == "ON" :
duplexmode = "Duplex"
else :
duplexmode = "Simplex"
else :
if not oldduplexmode :
duplexmode = defaultduplexmode
else :
duplexmode = oldduplexmode
defaultps = pjlparser.default_variables.get("PAPER", "")
if defaultps :
defaultpapersize = defaultps
envps = pjlparser.environment_variables.get("PAPER", "")
if envps :
papersize = envps
else :
if not oldpapersize :
papersize = defaultpapersize
else :
papersize = oldpapersize
else :
if oldpjlcopies == -1 :
pjlcopies = defaultpjlcopies
else :
pjlcopies = oldpjlcopies
if not oldduplexmode :
duplexmode = defaultduplexmode
else :
duplexmode = oldduplexmode
if not oldpapersize :
papersize = defaultpapersize
else :
papersize = oldpapersize
duplexmode = oldduplexmode
papersize = oldpapersize or page["mediasize"]
if page["mediasize"] != "Default" :
papersize = page["mediasize"]
if not duplexmode :
duplexmode = oldduplexmode or defaultduplexmode
oldpjlcopies = pjlcopies
oldduplexmode = duplexmode
oldpapersize = papersize
copies = max(pjlcopies, page["copies"]) # Was : pjlcopies * page["copies"]
self.pagecount += (copies - 1)
self.logdebug("%s*%s*%s*%s" % (copies,
papersize,
page["mediasource"],
duplexmode))
return self.pagecount
|