root / pkpgcounter / trunk / pkpgpdls / spl1.py @ 443

Revision 443, 5.0 kB (checked in by jerome, 17 years ago)

Changed copyright years.

  • 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 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
24"""This modules implements a page counter for SPL1 documents."""
25
26import sys
27import os
28import mmap
29from struct import unpack
30
31import pdlparser
32import pjl
33import version
34
35ESCAPECHARS = (chr(0x1b), chr(0x24))
36
37class Parser(pdlparser.PDLParser) :
38    """A parser for SPL1 documents."""
39    def isValid(self) :   
40        """Returns True if data is QPDL aka SPL2, else False."""
41        if ((self.firstblock[:128].find("\033%-12345X") != -1) and \
42            (self.firstblock.find("$PJL ") != -1) and \
43             ((self.firstblock.find("LANGUAGE=SMART") != -1) or \
44              (self.firstblock.find("LANGUAGE = SMART") != -1))) :
45            self.logdebug("DEBUG: Input file is in the SPL1 (aka SPL12) format.")
46            return True
47        else :   
48            return False
49           
50    def littleEndian(self) :
51        """Toggles to little endianness."""
52        self.unpackType = { 1 : "B", 2 : "<H", 4 : "<I" }
53        self.unpackShort = self.unpackType[2]
54        self.unpackLong = self.unpackType[4]
55        return 0
56       
57    def bigEndian(self) :
58        """Toggles to big endianness."""
59        self.unpackType = { 1 : "B", 2 : ">H", 4 : ">I" }
60        self.unpackShort = self.unpackType[2]
61        self.unpackLong = self.unpackType[4]
62        return 0
63   
64    def escape(self, nextpos) :   
65        """Handles the ESC code."""
66        self.isbitmap = False
67        pos = endpos = nextpos
68        minfile = self.minfile
69        if minfile[pos : pos+8] == r"%-12345X" :
70            endpos = pos + 9
71        elif minfile[pos-1] in ESCAPECHARS :   
72            endpos = pos
73        else :   
74            return 0
75        endmark = (chr(0x1b), chr(0x00))
76        asciilimit = chr(0x80)
77        quotes = 0
78        while (minfile[endpos] not in endmark) and \
79               ((minfile[endpos] < asciilimit) or (quotes % 2)) :
80            if minfile[endpos] == '"' :
81                quotes += 1
82            endpos += 1
83           
84        # Store this in a per page mapping.   
85        # NB : First time will be at page 0 (i.e. **before** page 1) !
86        stuff = self.escapedStuff.setdefault(self.pagecount, [])
87        datas = minfile[pos-1 : endpos]
88        stuff.append(datas)
89        if datas.endswith("$PJL BITMAP START\r\n") :
90            self.isbitmap = True
91        self.logdebug("Escaped datas : [%s]" % repr(datas))
92        return endpos - pos + 1
93       
94    def getJobSize(self) :
95        """Counts pages in an SPL1 document.
96       
97           Algorithm by Jerome Alet.
98        """
99        infileno = self.infile.fileno()
100        self.minfile = minfile = mmap.mmap(infileno, os.fstat(infileno)[6], prot=mmap.PROT_READ, flags=mmap.MAP_SHARED)
101        self.pagecount = 0
102        self.escapedStuff = {}   # For escaped datas, mostly PJL commands
103        self.bigEndian()
104       
105        self.isbitmap = False
106        pos = 0
107        try :
108            try :
109                while 1 :
110                    tag = minfile[pos]
111                    if tag in ESCAPECHARS :
112                        pos += self.escape(pos+1)
113                    else :   
114                        if not self.isbitmap :
115                            raise pdlparser.PDLParserError, "Unfortunately SPL1 is incompletely recognized. Parsing aborted. Please report the problem to %s" % version.__authoremail__
116                        offset = unpack(self.unpackLong, minfile[pos:pos+4])[0]
117                        sequencenum = unpack(self.unpackShort, minfile[pos+4:pos+6])[0]
118                        codesop = " ".join([ "%02x" % ord(v) for v in minfile[pos+6:pos+12]])
119                        if codesop != "06 00 00 80 13 40" :
120                            raise pdlparser.PDLParserError, "Unfortunately SPL1 is incompletely recognized. Parsing aborted. Please report the problem to %s" % version.__authoremail__
121                        if not sequencenum :
122                            self.pagecount += 1
123                        pos += 4 + offset
124            except IndexError : # EOF ?           
125                pass
126        finally :
127            minfile.close()
128        return self.pagecount
129       
130if __name__ == "__main__" :   
131    pdlparser.test(Parser)
Note: See TracBrowser for help on using the browser.