Ticket #80: zjstream-patched.py

File zjstream-patched.py, 4.5 kB (added by jerome, 11 years ago)

Patched an old version of zjstream.py

Line 
1
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: zjstream.py 374 2007-12-09 14:26:15Z jerome $
21#
22
23"""This modules implements a page counter for ZjStream documents."""
24
25import struct
26
27import pdlparser
28
29class 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.format = "Zenographics ZjStream (little endian)"
35            return self.littleEndian()
36        elif self.firstblock[:4] == "JZJZ" :   
37            self.format = "Zenographics ZjStream (big endian)"
38            return self.bigEndian()
39        elif (self.firstblock.find("\033%-12345XZJZJ") != -1) :
40            self.format = "Zenographics ZjStream (little endian)"
41            return self.littleEndian()
42        elif (self.firstblock.find("\033%-12345XJZJZ") != -1) :
43            self.format = "Zenographics ZjStream (big endian)"
44            return self.bigEndian()
45        else :   
46            return False
47       
48    def littleEndian(self) :
49        """Toggles to little endianness."""
50        self.unpackHeader = "<IIIHH"
51        return True
52       
53    def bigEndian(self) :
54        """Toggles to big endianness."""
55        self.unpackHeader = ">IIIHH"
56        return True
57       
58    def getJobSize(self) :
59        """Computes the number of pages in a ZjStream document."""
60        poslittle=self.firstblock.find("\033%-12345XZJZJ")
61        posbig=self.firstblock.find("\033%-12345XJZJZ")
62        if posbig==-1 and poslittle==-1 :
63             self.infile.seek(4, 0) # Skip ZJZJ/JZJZ header
64        elif posbig==-1 :                       
65                         self.infile.seek(poslittle+13, 0) # Skip little endian header with PJL
66        else:   
67             self.infile.seek(posbig+13, 0) # Skip big endian header with PJL
68                     
69        startpagecount = endpagecount = 0
70        unpackHeader = self.unpackHeader
71        unpack = struct.unpack
72        try :
73            while True :
74                header = self.infile.read(16)
75                if not header :
76                    break
77                (totalChunkSize,
78                 chunkType,
79                 numberOfItems,
80                 reserved,
81                 signature) = unpack(unpackHeader, header)
82                self.infile.seek(totalChunkSize - len(header), 1)
83                if chunkType == 2 :   
84                    #self.logdebug("startPage")
85                    startpagecount += 1
86                elif chunkType == 3 :
87                    #self.logdebug("endPage")
88                    endpagecount += 1
89                #elif chunkType == 0 :
90                #    self.logdebug("startDoc")
91                #elif chunkType == 1 :   
92                #    self.logdebug("endDoc")
93                #   
94                #self.logdebug("Chunk size : %s" % totalChunkSize)
95                #self.logdebug("Chunk type : 0x%08x" % chunkType)
96                #self.logdebug("# items : %s" % numberOfItems)
97                #self.logdebug("reserved : 0x%04x" % reserved)
98                #self.logdebug("signature : 0x%04x" % signature)
99                #self.logdebug("\n")
100        except struct.error :
101            raise pdlparser.PDLParserError, "This file doesn't seem to be valid ZjStream datas."
102           
103        # Number of endpage commands should be sufficient,
104        # but we never know : someone could try to cheat the printer
105        # by starting a page but not ending it, and ejecting it manually
106        # later on. Not sure if the printers would support this, but
107        # taking the max value works around the problem in any case.
108        self.logdebug("StartPage : %i    EndPage : %i" % (startpagecount, endpagecount))
109        return max(startpagecount, endpagecount)