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

Revision 564, 3.7 kB (checked in by jerome, 17 years ago)

Changed copyright years.
Removed unnecessary shebang lines.
Changed default encoding to UTF-8 from ISO-8859-15 (only
ascii is used anyway).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1# -*- coding: UTF-8 -*-
2#
3# pkpgcounter : a generic Page Description Language parser
4#
5# (c) 2003, 2004, 2005, 2006, 2007, 2008 Jerome Alet <alet@librelogiciel.com>
6# This program is free software: you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation, either version 3 of the License, or
9# (at your option) any later version.
10#
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
17# along with this program.  If not, see <http://www.gnu.org/licenses/>.
18#
19# $Id$
20#
21
22"""This modules implements a page counter for ZjStream documents."""
23
24import struct
25
26import pdlparser
27
28class Parser(pdlparser.PDLParser) :
29    """A parser for ZjStream documents."""
30    def isValid(self) :   
31        """Returns True if data is ZjStream, else False."""
32        if self.firstblock[:4] == "ZJZJ" :
33            self.format = "Zenographics ZjStream (little endian)"
34            return self.littleEndian()
35        elif self.firstblock[:4] == "JZJZ" :   
36            self.format = "Zenographics ZjStream (big endian)"
37            return self.bigEndian()
38        else :   
39            return False
40       
41    def littleEndian(self) :
42        """Toggles to little endianness."""
43        self.unpackHeader = "<IIIHH"
44        return True
45       
46    def bigEndian(self) :
47        """Toggles to big endianness."""
48        self.unpackHeader = ">IIIHH"
49        return True
50       
51    def getJobSize(self) :
52        """Computes the number of pages in a ZjStream document."""
53        self.infile.seek(4, 0) # Skip ZJZJ/JZJZ header
54        startpagecount = endpagecount = 0
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
62                (totalChunkSize,
63                 chunkType,
64                 numberOfItems,
65                 reserved,
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."
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)
Note: See TracBrowser for help on using the browser.