[473] | 1 | #! /usr/bin/env python |
---|
| 2 | # -*- coding: ISO-8859-15 -*- |
---|
| 3 | # |
---|
| 4 | # pkpgcounter : a generic Page Description Language parser |
---|
| 5 | # |
---|
| 6 | # (c) 2003, 2004, 2005, 2006, 2007 Jerome Alet <alet@librelogiciel.com> |
---|
| 7 | # This program is free software: you can redistribute it and/or modify |
---|
| 8 | # it under the terms of the GNU General Public License as published by |
---|
| 9 | # the Free Software Foundation, either version 3 of the License, or |
---|
| 10 | # (at your option) any later version. |
---|
| 11 | # |
---|
| 12 | # This program is distributed in the hope that it will be useful, |
---|
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 15 | # GNU General Public License for more details. |
---|
| 16 | # |
---|
| 17 | # You should have received a copy of the GNU General Public License |
---|
| 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
| 19 | # |
---|
| 20 | # $Id$ |
---|
| 21 | # |
---|
| 22 | |
---|
| 23 | """This modules implements a page counter for TIFF documents.""" |
---|
| 24 | |
---|
| 25 | import sys |
---|
| 26 | import os |
---|
| 27 | import mmap |
---|
| 28 | from struct import unpack |
---|
| 29 | |
---|
| 30 | import pdlparser |
---|
| 31 | import pjl |
---|
| 32 | |
---|
| 33 | class Parser(pdlparser.PDLParser) : |
---|
| 34 | """A parser for ESC/PAGES03 documents.""" |
---|
| 35 | def isValid(self) : |
---|
| 36 | """Returns True if data is TIFF, else False.""" |
---|
| 37 | if self.firstblock.startswith("\033\1@EJL") and \ |
---|
| 38 | (self.firstblock.find("=ESC/PAGES03\n") != -1) : |
---|
| 39 | self.logdebug("DEBUG: Input file is in the ESC/PAGES03 format.") |
---|
| 40 | return True |
---|
| 41 | else : |
---|
| 42 | return False |
---|
| 43 | |
---|
| 44 | def getJobSize(self) : |
---|
| 45 | """Counts pages in an ESC/PAGES03 document. |
---|
| 46 | |
---|
| 47 | Algorithm by Jerome Alet. |
---|
| 48 | Reverse engineered the file format. |
---|
| 49 | """ |
---|
| 50 | infileno = self.infile.fileno() |
---|
| 51 | minfile = mmap.mmap(infileno, os.fstat(infileno)[6], prot=mmap.PROT_READ, flags=mmap.MAP_SHARED) |
---|
| 52 | pagecount = 0 |
---|
| 53 | marker = "=ESC/PAGES03\n" |
---|
| 54 | startpos = minfile.find(marker) |
---|
| 55 | startsequence = chr(0x1d) |
---|
| 56 | if startpos == -1 : |
---|
| 57 | raise pdlparser.PDLParserError, "Invalid ESC/PAGES03 file." |
---|
| 58 | startpos += len(marker) |
---|
| 59 | if minfile[startpos] != startsequence : |
---|
| 60 | raise pdlparser.PDLParserError, "Invalid ESC/PAGES03 file." |
---|
| 61 | endsequence = "eps{I" |
---|
| 62 | lgendsequence = len(endsequence) |
---|
| 63 | try : |
---|
| 64 | try : |
---|
| 65 | while True : |
---|
| 66 | if minfile[startpos] == startsequence : |
---|
| 67 | #self.logdebug("Start sequence at %08x" % startpos) |
---|
| 68 | skiplen = 0 |
---|
| 69 | while True : |
---|
| 70 | startpos += 1 |
---|
| 71 | c = minfile[startpos] |
---|
| 72 | if not c.isdigit() : |
---|
| 73 | #self.logdebug("stop on %02x at %08x" % (ord(c), startpos)) |
---|
| 74 | break |
---|
| 75 | else : |
---|
| 76 | skiplen = (skiplen * 10) + int(c) |
---|
| 77 | if minfile[startpos:startpos+lgendsequence] == endsequence : |
---|
| 78 | #self.logdebug("skipped %i bytes at %08x until %08x" % (skiplen, startpos, startpos+skiplen+lgendsequence)) |
---|
| 79 | startpos += skiplen + lgendsequence |
---|
| 80 | else : |
---|
| 81 | #self.logdebug("Problem at %08x" % startpos) |
---|
| 82 | pass |
---|
| 83 | else : |
---|
| 84 | if minfile[startpos:startpos+6] == "\033\1@EJL" : |
---|
| 85 | # self.logdebug("EJL found at %08x" % startpos) |
---|
| 86 | ejlparser = pjl.EJLParser(minfile[startpos:]) |
---|
| 87 | pagecount = ejlparser.environment_variables.get("PAGES", "1") |
---|
| 88 | if pagecount.startswith('"') and pagecount.endswith('"') : |
---|
| 89 | pagecount = pagecount[1:-1] |
---|
| 90 | pagecount = int(pagecount) |
---|
| 91 | if pagecount <= 0 : |
---|
| 92 | pagecount = 1 # TODO : 1000000 ;-) |
---|
| 93 | break |
---|
| 94 | else : |
---|
| 95 | #self.logdebug("Skipped byte at %08x" % startpos) |
---|
| 96 | pass |
---|
| 97 | startpos += 1 |
---|
| 98 | except IndexError : |
---|
| 99 | pass |
---|
| 100 | finally : |
---|
| 101 | minfile.close() |
---|
| 102 | return pagecount |
---|
| 103 | |
---|
| 104 | if __name__ == "__main__" : |
---|
| 105 | pdlparser.test(Parser) |
---|