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 ZjStream documents.""" |
---|
24 | |
---|
25 | import struct |
---|
26 | |
---|
27 | import pdlparser |
---|
28 | |
---|
29 | class Parser(pdlparser.PDLParser) : |
---|
30 | """A parser for ZjStream documents.""" |
---|
31 | def isValid(self) : |
---|
32 | """Returns True if data is ZjStream, else False.""" |
---|
33 | if self.firstblock[:4] == "ZJZJ" : |
---|
34 | self.logdebug("DEBUG: Input file is in the Zenographics ZjStream (little endian) format.") |
---|
35 | return self.littleEndian() |
---|
36 | elif self.firstblock[:4] == "JZJZ" : |
---|
37 | self.logdebug("DEBUG: Input file is in the Zenographics ZjStream (big endian) format.") |
---|
38 | return self.bigEndian() |
---|
39 | else : |
---|
40 | return False |
---|
41 | |
---|
42 | def littleEndian(self) : |
---|
43 | """Toggles to little endianness.""" |
---|
44 | self.unpackHeader = "<IIIHH" |
---|
45 | return True |
---|
46 | |
---|
47 | def bigEndian(self) : |
---|
48 | """Toggles to big endianness.""" |
---|
49 | self.unpackHeader = ">IIIHH" |
---|
50 | return True |
---|
51 | |
---|
52 | def getJobSize(self) : |
---|
53 | """Computes the number of pages in a ZjStream document.""" |
---|
54 | unpack = struct.unpack |
---|
55 | self.infile.seek(4, 0) # Skip ZJZJ/JZJZ header |
---|
56 | startpagecount = endpagecount = 0 |
---|
57 | while True : |
---|
58 | header = self.infile.read(16) |
---|
59 | if not header : |
---|
60 | break |
---|
61 | try : |
---|
62 | (totalChunkSize, |
---|
63 | chunkType, |
---|
64 | numberOfItems, |
---|
65 | reserved, |
---|
66 | signature) = unpack(self.unpackHeader, header) |
---|
67 | except struct.error : |
---|
68 | raise pdlparser.PDLParserError, "This file doesn't seem to be valid ZjStream datas." |
---|
69 | self.infile.seek(totalChunkSize - len(header), 1) |
---|
70 | if chunkType == 2 : |
---|
71 | #self.logdebug("startPage") |
---|
72 | startpagecount += 1 |
---|
73 | elif chunkType == 3 : |
---|
74 | #self.logdebug("endPage") |
---|
75 | endpagecount += 1 |
---|
76 | #elif chunkType == 0 : |
---|
77 | # self.logdebug("startDoc") |
---|
78 | #elif chunkType == 1 : |
---|
79 | # self.logdebug("endDoc") |
---|
80 | |
---|
81 | #self.logdebug("Chunk size : %s" % totalChunkSize) |
---|
82 | #self.logdebug("Chunk type : 0x%08x" % chunkType) |
---|
83 | #self.logdebug("# items : %s" % numberOfItems) |
---|
84 | #self.logdebug("reserved : 0x%04x" % reserved) |
---|
85 | #self.logdebug("signature : 0x%04x" % signature) |
---|
86 | #self.logdebug("\n") |
---|
87 | |
---|
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)) |
---|
94 | return max(startpagecount, endpagecount) |
---|