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

Revision 328, 2.8 kB (checked in by jerome, 18 years ago)

Added preliminary support for the ZjStream? Page Description Language.

  • 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 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 2 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, write to the Free Software
19# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20#
21# $Id$
22#
23
24import sys
25import os
26import mmap
27from struct import unpack
28
29import pdlparser
30
31class Parser(pdlparser.PDLParser) :
32    """A parser for ZjStream documents."""
33    def isValid(self) :   
34        """Returns 1 if data is PCLXL aka PCL6, else 0."""
35        if self.firstblock[:4] == "ZJZJ" :
36            self.littleEndian()
37            return 1
38        elif self.firstblock[:4] == "JZJZ" :   
39            self.bigEndian()
40            return 1
41        else :   
42            return 0
43       
44    def littleEndian(self) :
45        """Toggles to little endianness."""
46        self.unpackType = { 1 : "B", 2 : "<H", 4 : "<I" }
47        self.unpackShort = self.unpackType[2]
48        self.unpackLong = self.unpackType[4]
49        return 0
50       
51    def bigEndian(self) :
52        """Toggles to big endianness."""
53        self.unpackType = { 1 : "B", 2 : ">H", 4 : ">I" }
54        self.unpackShort = self.unpackType[2]
55        self.unpackLong = self.unpackType[4]
56        return 0
57       
58    def getJobSize(self) :
59        """Computes the number of pages in a ZjStream document."""
60        sys.stderr.write("ZjStream is not supported yet, returning 0 pages.\n")
61        return 0
62       
63def test() :       
64    """Test function."""
65    if (len(sys.argv) < 2) or ((not sys.stdin.isatty()) and ("-" not in sys.argv[1:])) :
66        sys.argv.append("-")
67    totalsize = 0   
68    for arg in sys.argv[1:] :
69        if arg == "-" :
70            infile = sys.stdin
71            mustclose = 0
72        else :   
73            infile = open(arg, "rb")
74            mustclose = 1
75        try :
76            parser = Parser(infile, debug=1)
77            totalsize += parser.getJobSize()
78        except pdlparser.PDLParserError, msg :   
79            sys.stderr.write("ERROR: %s\n" % msg)
80            sys.stderr.flush()
81        if mustclose :   
82            infile.close()
83    print "%s" % totalsize
84   
85if __name__ == "__main__" :   
86    test()
Note: See TracBrowser for help on using the browser.