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