root / pkpgcounter / trunk / pkpgpdls / zjstream.py @ 532

Revision 532, 3.8 kB (checked in by jerome, 16 years ago)

Moved try/except out of the main loop to increase performance.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
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
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.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        self.infile.seek(4, 0) # Skip ZJZJ/JZJZ header
55        startpagecount = endpagecount = 0
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
63                (totalChunkSize,
64                 chunkType,
65                 numberOfItems,
66                 reserved,
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."
88           
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))
95        return max(startpagecount, endpagecount)
Note: See TracBrowser for help on using the browser.