[328] | 1 | #! /usr/bin/env python |
---|
| 2 | # -*- coding: ISO-8859-15 -*- |
---|
| 3 | # |
---|
| 4 | # pkpgcounter : a generic Page Description Language parser |
---|
| 5 | # |
---|
[443] | 6 | # (c) 2003, 2004, 2005, 2006, 2007 Jerome Alet <alet@librelogiciel.com> |
---|
[463] | 7 | # This program is free software: you can redistribute it and/or modify |
---|
[328] | 8 | # it under the terms of the GNU General Public License as published by |
---|
[463] | 9 | # the Free Software Foundation, either version 3 of the License, or |
---|
[328] | 10 | # (at your option) any later version. |
---|
[463] | 11 | # |
---|
[328] | 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 |
---|
[463] | 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
[328] | 19 | # |
---|
| 20 | # $Id$ |
---|
| 21 | # |
---|
| 22 | |
---|
[357] | 23 | """This modules implements a page counter for ZjStream documents.""" |
---|
| 24 | |
---|
[491] | 25 | import struct |
---|
[328] | 26 | |
---|
| 27 | import pdlparser |
---|
| 28 | |
---|
| 29 | class Parser(pdlparser.PDLParser) : |
---|
| 30 | """A parser for ZjStream documents.""" |
---|
| 31 | def isValid(self) : |
---|
[387] | 32 | """Returns True if data is ZjStream, else False.""" |
---|
[522] | 33 | if self.firstblock[:4] == "ZJZJ" : |
---|
[329] | 34 | self.logdebug("DEBUG: Input file is in the Zenographics ZjStream (little endian) format.") |
---|
[491] | 35 | return self.littleEndian() |
---|
[522] | 36 | elif self.firstblock[:4] == "JZJZ" : |
---|
[329] | 37 | self.logdebug("DEBUG: Input file is in the Zenographics ZjStream (big endian) format.") |
---|
[491] | 38 | return self.bigEndian() |
---|
[328] | 39 | else : |
---|
[387] | 40 | return False |
---|
[328] | 41 | |
---|
| 42 | def littleEndian(self) : |
---|
| 43 | """Toggles to little endianness.""" |
---|
[491] | 44 | self.unpackHeader = "<IIIHH" |
---|
| 45 | return True |
---|
[328] | 46 | |
---|
| 47 | def bigEndian(self) : |
---|
| 48 | """Toggles to big endianness.""" |
---|
[491] | 49 | self.unpackHeader = ">IIIHH" |
---|
| 50 | return True |
---|
[328] | 51 | |
---|
| 52 | def getJobSize(self) : |
---|
| 53 | """Computes the number of pages in a ZjStream document.""" |
---|
[491] | 54 | self.infile.seek(4, 0) # Skip ZJZJ/JZJZ header |
---|
[329] | 55 | startpagecount = endpagecount = 0 |
---|
[532] | 56 | unpackHeader = self.unpackHeader |
---|
| 57 | unpack = struct.unpack |
---|
| 58 | try : |
---|
| 59 | while True : |
---|
| 60 | header = self.infile.read(16) |
---|
| 61 | if not header : |
---|
| 62 | break |
---|
[491] | 63 | (totalChunkSize, |
---|
| 64 | chunkType, |
---|
| 65 | numberOfItems, |
---|
| 66 | reserved, |
---|
[532] | 67 | signature) = unpack(unpackHeader, header) |
---|
| 68 | self.infile.seek(totalChunkSize - len(header), 1) |
---|
| 69 | if chunkType == 2 : |
---|
| 70 | #self.logdebug("startPage") |
---|
| 71 | startpagecount += 1 |
---|
| 72 | elif chunkType == 3 : |
---|
| 73 | #self.logdebug("endPage") |
---|
| 74 | endpagecount += 1 |
---|
| 75 | #elif chunkType == 0 : |
---|
| 76 | # self.logdebug("startDoc") |
---|
| 77 | #elif chunkType == 1 : |
---|
| 78 | # self.logdebug("endDoc") |
---|
| 79 | # |
---|
| 80 | #self.logdebug("Chunk size : %s" % totalChunkSize) |
---|
| 81 | #self.logdebug("Chunk type : 0x%08x" % chunkType) |
---|
| 82 | #self.logdebug("# items : %s" % numberOfItems) |
---|
| 83 | #self.logdebug("reserved : 0x%04x" % reserved) |
---|
| 84 | #self.logdebug("signature : 0x%04x" % signature) |
---|
| 85 | #self.logdebug("\n") |
---|
| 86 | except struct.error : |
---|
| 87 | raise pdlparser.PDLParserError, "This file doesn't seem to be valid ZjStream datas." |
---|
[329] | 88 | |
---|
[491] | 89 | # Number of endpage commands should be sufficient, |
---|
| 90 | # but we never know : someone could try to cheat the printer |
---|
| 91 | # by starting a page but not ending it, and ejecting it manually |
---|
| 92 | # later on. Not sure if the printers would support this, but |
---|
| 93 | # taking the max value works around the problem in any case. |
---|
| 94 | self.logdebug("StartPage : %i EndPage : %i" % (startpagecount, endpagecount)) |
---|
[329] | 95 | return max(startpagecount, endpagecount) |
---|