Changeset 252 for pkpgcounter
- Timestamp:
- 09/03/05 00:40:57 (19 years ago)
- Location:
- pkpgcounter/trunk
- Files:
-
- 1 added
- 12 modified
Legend:
- Unmodified
- Added
- Removed
-
pkpgcounter/trunk/NEWS
r249 r252 22 22 pkpgcounter News : 23 23 24 * 1.62 : 25 26 - Better handling of the number of copies in the PCLXL parser. 27 28 - Better handling of the number of copies in the PCL3/4/5 parser. 29 24 30 * 1.61 : 25 31 -
pkpgcounter/trunk/pkpgpdls/analyzer.py
r235 r252 119 119 ooo) : 120 120 try : 121 return getattr(module, "Parser")(self.infile, self.debug, firstblock, lastblock)121 return module.Parser(self.infile, self.debug, firstblock, lastblock) 122 122 except pdlparser.PDLParserError : 123 123 pass # try next parser -
pkpgcounter/trunk/pkpgpdls/dvi.py
r235 r252 35 35 try : 36 36 if (ord(self.firstblock[0]) == 0xf7) and (ord(self.lastblock[-1]) == 0xdf) : 37 if self.debug : 38 sys.stderr.write("DEBUG: Input file is in the DVI format.\n") 37 self.logdebug("DEBUG: Input file is in the DVI format.") 39 38 return 1 40 39 else : -
pkpgcounter/trunk/pkpgpdls/escp2.py
r235 r252 34 34 self.firstblock.startswith("\n\033@") or \ 35 35 self.firstblock.startswith("\0\0\0\033\1@EJL") : # ESC/P Raster ??? Seen on Stylus Photo 1284 36 if self.debug : 37 sys.stderr.write("DEBUG: Input file is in the ESC/P2 format.\n") 36 self.logdebug("DEBUG: Input file is in the ESC/P2 format.") 38 37 return 1 39 38 else : -
pkpgcounter/trunk/pkpgpdls/ooo.py
r235 r252 23 23 24 24 import sys 25 import os26 25 import zipfile 27 26 … … 40 39 return 0 41 40 else : 42 if self.debug : 43 sys.stderr.write("DEBUG: Input file is in the OpenOffice.org format.\n") 41 self.logdebug("DEBUG: Input file is in the OpenOffice.org format.") 44 42 return 1 45 43 else : -
pkpgcounter/trunk/pkpgpdls/pcl345.py
r249 r252 84 84 self.firstblock.startswith("\033%8\033") or \ 85 85 (self.firstblock.find("\033%-12345X") != -1) : 86 if self.debug : 87 sys.stderr.write("DEBUG: Input file is in the PCL3/4/5 format.\n") 86 self.logdebug("DEBUG: Input file is in the PCL3/4/5 format.") 88 87 return 1 89 88 else : … … 92 91 def setPageDict(self, pages, number, attribute, value) : 93 92 """Initializes a page dictionnary.""" 94 dic t = pages.setdefault(number, { "copies" : 1, "mediasource" : "Main", "mediasize" : "Default", "mediatype" : "Plain", "orientation" : "Portrait"})95 dic t[attribute] = value93 dic = pages.setdefault(number, { "copies" : 1, "mediasource" : "Main", "mediasize" : "Default", "mediatype" : "Plain", "orientation" : "Portrait", "escaped" : []}) 94 dic[attribute] = value 96 95 97 96 def getJobSize(self) : … … 137 136 mediasourcecount = mediasizecount = orientationcount = mediatypecount = 0 138 137 tag = None 138 endmark = chr(0x1b) + chr(0x0c) + chr(0x00) 139 asciilimit = chr(0x80) 139 140 pages = {} 140 141 pos = 0 … … 145 146 pagecount += 1 146 147 elif char == "\033" : 147 starb = ampl = 0 148 # 149 # <ESC>*b###y#m###v###w... -> PCL3 raster graphics 150 # <ESC>*b###W -> Start of a raster data row/block 151 # <ESC>*b###V -> Start of a raster data plane 152 # <ESC>*c###W -> Start of a user defined pattern 153 # <ESC>*i###W -> Start of a viewing illuminant block 154 # <ESC>*l###W -> Start of a color lookup table 155 # <ESC>*m###W -> Start of a download dither matrix block 156 # <ESC>*v###W -> Start of a configure image data block 157 # <ESC>*r1A -> Start Gfx 158 # <ESC>(s###W -> Start of a characters description block 159 # <ESC>)s###W -> Start of a fonts description block 160 # <ESC>(f###W -> Start of a symbol set block 161 # <ESC>&b###W -> Start of configuration data block 162 # <ESC>&l###X -> Number of copies for current page 163 # <ESC>&n###W -> Starts an alphanumeric string ID block 164 # <ESC>&p###X -> Start of a non printable characters block 165 # <ESC>&a2G -> Back side when duplex mode as generated by rastertohp 166 # <ESC>*g###W -> Needed for planes in PCL3 output 167 # <ESC>&l###H (or only 0 ?) -> Eject if NumPlanes > 1, as generated by rastertohp. Also defines mediasource 168 # <ESC>&l###A -> mediasize 169 # <ESC>&l###O -> orientation 170 # <ESC>&l###M -> mediatype 171 # <ESC>*t###R -> gfx resolution 172 # 173 tagstart = minfile[pos] ; pos += 1 174 if tagstart in "E9=YZ" : # one byte PCL tag 175 if tagstart == "E" : 176 resets += 1 177 continue # skip to next tag 178 tag = tagstart + minfile[pos] ; pos += 1 179 if tag == "*b" : 180 starb = 1 181 tagend = "VW" 182 elif tag == "&l" : 183 ampl = 1 184 tagend = "XHAOM" 185 else : 186 try : 187 tagend = tagsends[tag] 188 except KeyError : 189 continue # Unsupported PCL tag 190 # Now read the numeric argument 191 size = 0 192 while 1 : 193 char = minfile[pos] ; pos += 1 194 if not char.isdigit() : 195 break 196 size = (size * 10) + int(char) 197 if char in tagend : 198 if tag == "&l" : 199 if char == "X" : 200 self.setPageDict(pages, pagecount, "copies", size) 201 elif char == "H" : 202 self.setPageDict(pages, pagecount, "mediasource", self.mediasources.get(size, str(size))) 203 mediasourcecount += 1 204 ejects += 1 205 elif char == "A" : 206 self.setPageDict(pages, pagecount, "mediasize", self.mediasizes.get(size, str(size))) 207 mediasizecount += 1 208 elif char == "O" : 209 self.setPageDict(pages, pagecount, "orientation", self.orientations.get(size, str(size))) 210 orientationcount += 1 211 elif char == "M" : 212 self.setPageDict(pages, pagecount, "mediatype", self.mediatypes.get(size, str(size))) 213 mediatypecount += 1 214 elif tag == "*r" : 215 # Special tests for PCL3 216 if (char == "s") and size : 217 while 1 : 218 char = minfile[pos] ; pos += 1 219 if char == "A" : 220 break 221 elif (char == "b") and (minfile[pos] == "C") and not size : 222 ispcl3 = 1 # Certainely a PCL3 file 223 startgfx += (char == "A") and (minfile[pos - 2] in ("0", "1", "2", "3")) # Start Gfx 224 endgfx += (not size) and (char in ("C", "B")) # End Gfx 225 elif tag == "*t" : 226 escstart += 1 227 elif (tag == "&a") and (size == 2) : 228 backsides += 1 # Back side in duplex mode 148 if minfile[pos : pos+8] == r"%-12345X" : 149 endpos = pos + 9 150 while (minfile[endpos] not in endmark) and (minfile[endpos] < asciilimit) : 151 endpos += 1 152 self.setPageDict(pages, pagecount, "escaped", minfile[pos : endpos].replace('\r\n', '\n').split('\n')) 153 self.logdebug("Escaped datas : [%s]" % repr(minfile[pos : endpos])) 154 pos += (endpos - pos) 155 else : 156 starb = ampl = 0 157 # 158 # <ESC>*b###y#m###v###w... -> PCL3 raster graphics 159 # <ESC>*b###W -> Start of a raster data row/block 160 # <ESC>*b###V -> Start of a raster data plane 161 # <ESC>*c###W -> Start of a user defined pattern 162 # <ESC>*i###W -> Start of a viewing illuminant block 163 # <ESC>*l###W -> Start of a color lookup table 164 # <ESC>*m###W -> Start of a download dither matrix block 165 # <ESC>*v###W -> Start of a configure image data block 166 # <ESC>*r1A -> Start Gfx 167 # <ESC>(s###W -> Start of a characters description block 168 # <ESC>)s###W -> Start of a fonts description block 169 # <ESC>(f###W -> Start of a symbol set block 170 # <ESC>&b###W -> Start of configuration data block 171 # <ESC>&l###X -> Number of copies for current page 172 # <ESC>&n###W -> Starts an alphanumeric string ID block 173 # <ESC>&p###X -> Start of a non printable characters block 174 # <ESC>&a2G -> Back side when duplex mode as generated by rastertohp 175 # <ESC>*g###W -> Needed for planes in PCL3 output 176 # <ESC>&l###H (or only 0 ?) -> Eject if NumPlanes > 1, as generated by rastertohp. Also defines mediasource 177 # <ESC>&l###A -> mediasize 178 # <ESC>&l###O -> orientation 179 # <ESC>&l###M -> mediatype 180 # <ESC>*t###R -> gfx resolution 181 # 182 tagstart = minfile[pos] ; pos += 1 183 if tagstart in "E9=YZ" : # one byte PCL tag 184 if tagstart == "E" : 185 resets += 1 186 continue # skip to next tag 187 tag = tagstart + minfile[pos] ; pos += 1 188 if tag == "*b" : 189 starb = 1 190 tagend = "VW" 191 elif tag == "&l" : 192 ampl = 1 193 tagend = "XHAOM" 229 194 else : 230 # we just ignore the block. 231 if tag == "&n" : 232 # we have to take care of the operation id byte 233 # which is before the string itself 234 size += 1 235 pos += size 195 try : 196 tagend = tagsends[tag] 197 except KeyError : 198 continue # Unsupported PCL tag 199 # Now read the numeric argument 200 size = 0 201 while 1 : 202 char = minfile[pos] ; pos += 1 203 if not char.isdigit() : 204 break 205 size = (size * 10) + int(char) 206 if char in tagend : 207 if tag == "&l" : 208 if char == "X" : 209 self.setPageDict(pages, pagecount, "copies", size) 210 elif char == "H" : 211 self.setPageDict(pages, pagecount, "mediasource", self.mediasources.get(size, str(size))) 212 mediasourcecount += 1 213 ejects += 1 214 elif char == "A" : 215 self.setPageDict(pages, pagecount, "mediasize", self.mediasizes.get(size, str(size))) 216 mediasizecount += 1 217 elif char == "O" : 218 self.setPageDict(pages, pagecount, "orientation", self.orientations.get(size, str(size))) 219 orientationcount += 1 220 elif char == "M" : 221 self.setPageDict(pages, pagecount, "mediatype", self.mediatypes.get(size, str(size))) 222 mediatypecount += 1 223 elif tag == "*r" : 224 # Special tests for PCL3 225 if (char == "s") and size : 226 while 1 : 227 char = minfile[pos] ; pos += 1 228 if char == "A" : 229 break 230 elif (char == "b") and (minfile[pos] == "C") and not size : 231 ispcl3 = 1 # Certainely a PCL3 file 232 startgfx += (char == "A") and (minfile[pos - 2] in ("0", "1", "2", "3")) # Start Gfx 233 endgfx += (not size) and (char in ("C", "B")) # End Gfx 234 elif tag == "*t" : 235 escstart += 1 236 elif (tag == "&a") and (size == 2) : 237 backsides += 1 # Back side in duplex mode 238 else : 239 # we just ignore the block. 240 if tag == "&n" : 241 # we have to take care of the operation id byte 242 # which is before the string itself 243 size += 1 244 pos += size 236 245 else : 237 246 if starb : … … 329 338 pass # should be OK. 330 339 elif (not startgfx) and (not endgfx) : 331 pagecount = ejects or pagecount 340 if ejects : 341 pagecount = ejects 332 342 elif startgfx == endgfx : 333 343 pagecount = startgfx … … 339 349 pagecount = abs(startgfx - endgfx) 340 350 341 if self.debug : 342 for pnum in range(pagecount) : 343 # if no number of copies defined, take the preceding one else the one set before any page else 1. 344 page = pages.get(pnum, pages.get(pnum - 1, pages.get(0, { "copies" : 1, "mediasource" : "Main", "mediasize" : "Default", "mediatype" : "Plain", "orientation" : "Portrait"}))) 345 sys.stderr.write("%s*%s*%s*%s*%s\n" % (page["copies"], page["mediatype"], page["mediasize"], page["orientation"], page["mediasource"])) 351 pjlstatements = [] 352 for pnum in range(pagecount) : 353 # if no number of copies defined, take the preceding one else the one set before any page else 1. 354 page = pages.get(pnum, pages.get(pnum - 1, pages.get(0, { "copies" : 1, "mediasource" : "Main", "mediasize" : "Default", "mediatype" : "Plain", "orientation" : "Portrait", "escaped" : []}))) 355 pjlstuff = page["escaped"] 356 if not pjlstuff : 357 pjlcopies = 1 358 else : 359 pjlstatements.extend(pjlstuff) 360 newpjl = "\n".join(pjlstatements) 361 copiesstatement = newpjl.rfind("@PJL SET COPIES=") 362 qtystatement = newpjl.rfind("@PJL SET QTY=") 363 if copiesstatement > qtystatement : 364 # we use the COPIES= statement 365 try : 366 pjlcopies = int(newpjl[copiesstatement+16:].split()[0].strip()) 367 except : 368 pjlcopies = 1 369 elif qtystatement > copiesstatement : 370 # we use the QTY= statement 371 try : 372 pjlcopies = int(newpjl[qtystatement+13:].split()[0].strip()) 373 except : 374 pjlcopies = 1 375 else : 376 # both can't be equal unless they both equal -1 (not found) 377 pjlcopies = 1 378 copies = pjlcopies * page["copies"] 379 pagecount += (copies - 1) 380 self.logdebug("%s*%s*%s*%s*%s" % (copies, \ 381 page["mediatype"], \ 382 page["mediasize"], \ 383 page["orientation"], \ 384 page["mediasource"])) 346 385 347 386 return pagecount -
pkpgcounter/trunk/pkpgpdls/pclxl.py
r248 r252 28 28 29 29 import pdlparser 30 import pjl 30 31 31 32 class Parser(pdlparser.PDLParser) : … … 82 83 ((self.firstblock.find("LANGUAGE=PCLXL") != -1) or \ 83 84 (self.firstblock.find("LANGUAGE = PCLXL") != -1))) : 84 if self.debug : 85 sys.stderr.write("DEBUG: Input file is in the PCLXL (aka PCL6) format.\n") 85 self.logdebug("DEBUG: Input file is in the PCLXL (aka PCL6) format.") 86 86 return 1 87 87 else : … … 116 116 elif val == 0x28 : 117 117 orientation = ord(minfile[pos - 2]) 118 orien ationlabel = self.orientations.get(orientation, str(orientation))118 orientationlabel = self.orientations.get(orientation, str(orientation)) 119 119 pos = pos - 4 120 120 elif val == 0x27 : … … 246 246 def reservedForFutureUse(self) : 247 247 """Outputs something when a reserved byte is encountered.""" 248 if self.debug : 249 sys.stderr.write("Byte at %s is out of the PCLXL Protocol Class 2.0 Specification\n" % self.pos) 248 self.logdebug("Byte at %s is out of the PCLXL Protocol Class 2.0 Specification" % self.pos) 250 249 return 0 251 250 … … 255 254 if self.minfile[pos : pos+8] == r"%-12345X" : 256 255 endpos = pos + 9 257 endmark = chr(0x0c) + chr(0x00) 256 endmark = chr(0x0c) + chr(0x00) + chr(0x1b) 258 257 asciilimit = chr(0x80) 259 258 while (self.minfile[endpos] not in endmark) and (self.minfile[endpos] < asciilimit) : … … 264 263 stuff = self.escapedStuff.setdefault(self.pagecount, []) 265 264 stuff.append(self.minfile[pos : endpos]) 266 if self.debug : 267 sys.stderr.write("Escaped datas : [%s]\n" % repr(self.minfile[pos : endpos])) 265 self.logdebug("Escaped datas : [%s]" % repr(self.minfile[pos : endpos])) 268 266 return endpos - pos 269 267 … … 489 487 else : 490 488 colormode = "Black" 489 490 defaultpjlcopies = 1 491 oldpjlcopies = -1 491 492 for pnum in range(1, self.pagecount + 1) : 492 493 # if no number of copies defined, take 1, as explained … … 496 497 # to decrease the total number of pages in this case. 497 498 page = self.pages.get(pnum, self.pages.get(1, { "copies" : 1 })) 498 copies = page["copies"] 499 pjlstuff = self.escapedStuff.get(pnum, []) 500 if pjlstuff : 501 pjlparser = pjl.PJLParser("".join(pjlstuff)) 502 nbdefaultcopies = int(pjlparser.default_variables.get("COPIES", -1)) 503 nbcopies = int(pjlparser.environment_variables.get("COPIES", -1)) 504 nbdefaultqty = int(pjlparser.default_variables.get("QTY", -1)) 505 nbqty = int(pjlparser.environment_variables.get("QTY", -1)) 506 if nbdefaultcopies > -1 : 507 defaultpjlcopies = nbdefaultcopies 508 if nbdefaultqty > -1 : 509 defaultpjlcopies = nbdefaultqty 510 if nbcopies > -1 : 511 oldpjlcopies = pjlcopies = nbcopies 512 elif nbqty > -1 : 513 oldpjlcopies = pjlcopies = nbqty 514 else : 515 if oldpjlcopies == -1 : 516 oldpjlcopies = defaultpjlcopies 517 pjlcopies = oldpjlcopies 518 else : 519 if oldpjlcopies == -1 : 520 pjlcopies = defaultpjlcopies 521 else : 522 pjlcopies = oldpjlcopies 523 copies = pjlcopies * page["copies"] 499 524 self.pagecount += (copies - 1) 500 if self.debug : 501 sys.stderr.write("%s*%s*%s*%s*%s*%s\n" % (copies, 502 page["mediatype"], 503 page["mediasize"], 504 page["orientation"], 505 page["mediasource"], 506 colormode)) 525 self.logdebug("%s*%s*%s*%s*%s*%s" % (copies, 526 page["mediatype"], 527 page["mediasize"], 528 page["orientation"], 529 page["mediasource"], 530 colormode)) 507 531 return self.pagecount 508 532 -
pkpgcounter/trunk/pkpgpdls/pdf.py
r243 r252 47 47 ((self.firstblock[:128].find("\033%-12345X") != -1) and (self.firstblock.upper().find("LANGUAGE=PDF") != -1)) or \ 48 48 (self.firstblock.find("%PDF-") != -1) : 49 if self.debug : 50 sys.stderr.write("DEBUG: Input file is in the PDF format.\n") 49 self.logdebug("DEBUG: Input file is in the PDF format.") 51 50 return 1 52 51 else : … … 99 98 colorregexp = re.compile(r"(/ColorSpace) ?(/DeviceRGB|/DeviceCMYK)[/ \t\r\n]", re.I) 100 99 pagecount = 0 101 for obj ectin objects.values() :102 content = "".join(obj ect.content)100 for obj in objects.values() : 101 content = "".join(obj.content) 103 102 count = len(newpageregexp.findall(content)) 104 103 pagecount += count 105 104 if colorregexp.match(content) : 106 105 self.iscolor = 1 107 if self.debug : 108 sys.stderr.write("ColorSpace : %s\n" % content) 106 self.logdebug("ColorSpace : %s" % content) 109 107 return pagecount 110 108 -
pkpgcounter/trunk/pkpgpdls/pdlparser.py
r247 r252 69 69 psyco.bind(self.getJobSize) 70 70 71 def logdebug(self, message) : 72 """Logs a debug message if needed.""" 73 if self.debug : 74 sys.stderr.write("%s\n" % message) 75 71 76 def isValid(self) : 72 77 """Returns 1 if data is in the expected format, else 0.""" -
pkpgcounter/trunk/pkpgpdls/postscript.py
r248 r252 39 39 (self.firstblock.find("LANGUAGE = Postscript") != -1))) or \ 40 40 (self.firstblock.find("%!PS-Adobe") != -1) : 41 if self.debug : 42 sys.stderr.write("DEBUG: Input file is in the PostScript format.\n") 41 self.logdebug("DEBUG: Input file is in the PostScript format.") 43 42 return 1 44 43 else : … … 47 46 def throughGhostScript(self) : 48 47 """Get the count through GhostScript, useful for non-DSC compliant PS files.""" 49 if self.debug : 50 sys.stderr.write("Internal parser sucks, using GhostScript instead...\n") 48 self.logdebug("Internal parser sucks, using GhostScript instead...") 51 49 self.infile.seek(0) 52 50 command = 'gs -sDEVICE=bbox -dNOPAUSE -dBATCH -dQUIET - 2>&1 | grep -c "%%HiResBoundingBox:" 2>/dev/null' … … 81 79 pages = {} 82 80 pages[0] = { "copies" : 1 } 81 previousline = "" 83 82 for line in self.infile.xreadlines() : 84 83 if line.startswith(r"%%Page: ") : … … 130 129 copies = page["copies"] 131 130 pagecount += (copies - 1) 132 if self.debug : 133 sys.stderr.write("%s * page #%s\n" % (copies, pnum)) 131 self.logdebug("%s * page #%s" % (copies, pnum)) 134 132 return pagecount 135 133 -
pkpgcounter/trunk/pkpgpdls/tiff.py
r235 r252 36 36 bigendian = (chr(0x4d)*2) + chr(0) + chr(0x2a) 37 37 if self.firstblock[:4] in (littleendian, bigendian) : 38 if self.debug : 39 sys.stderr.write("DEBUG: Input file is in the TIFF format.\n") 38 self.logdebug("DEBUG: Input file is in the TIFF format.") 40 39 return 1 41 40 else : -
pkpgcounter/trunk/pkpgpdls/version.py
r248 r252 20 20 # 21 21 22 __version__ = "1.6 1"22 __version__ = "1.62" 23 23 24 24 __doc__ = """pkpgcounter : a generic Page Description Languages parser."""