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

Revision 463, 4.9 kB (checked in by jerome, 17 years ago)

Licensing terms changed to GNU GPL v3.0 or higher.
Removed old PCL3/4/5 parser which for a long time now wasn't used
anymore, and for which I was not the original copyright owner.
Version number bumped to 3.00alpha to reflect licensing changes.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
RevLine 
[408]1#! /usr/bin/env python
2# -*- coding: ISO-8859-15 -*-
3#
4# pkpgcounter : a generic Page Description Language parser
5#
[443]6# (c) 2003, 2004, 2005, 2006, 2007 Jerome Alet <alet@librelogiciel.com>
[463]7# This program is free software: you can redistribute it and/or modify
[408]8# it under the terms of the GNU General Public License as published by
[463]9# the Free Software Foundation, either version 3 of the License, or
[408]10# (at your option) any later version.
[463]11#
[408]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
[463]18# along with this program.  If not, see <http://www.gnu.org/licenses/>.
[408]19#
20# $Id$
21#
22
23"""This modules implements a page counter for SPL1 documents."""
24
25import sys
26import os
27import mmap
28from struct import unpack
29
30import pdlparser
[410]31import version
[408]32
33ESCAPECHARS = (chr(0x1b), chr(0x24))
34
35class Parser(pdlparser.PDLParser) :
36    """A parser for SPL1 documents."""
37    def isValid(self) :   
38        """Returns True if data is QPDL aka SPL2, else False."""
39        if ((self.firstblock[:128].find("\033%-12345X") != -1) and \
40            (self.firstblock.find("$PJL ") != -1) and \
41             ((self.firstblock.find("LANGUAGE=SMART") != -1) or \
42              (self.firstblock.find("LANGUAGE = SMART") != -1))) :
43            self.logdebug("DEBUG: Input file is in the SPL1 (aka SPL12) format.")
44            return True
45        else :   
46            return False
47           
48    def littleEndian(self) :
49        """Toggles to little endianness."""
50        self.unpackType = { 1 : "B", 2 : "<H", 4 : "<I" }
51        self.unpackShort = self.unpackType[2]
52        self.unpackLong = self.unpackType[4]
53        return 0
54       
55    def bigEndian(self) :
56        """Toggles to big endianness."""
57        self.unpackType = { 1 : "B", 2 : ">H", 4 : ">I" }
58        self.unpackShort = self.unpackType[2]
59        self.unpackLong = self.unpackType[4]
60        return 0
61   
62    def escape(self, nextpos) :   
63        """Handles the ESC code."""
[409]64        self.isbitmap = False
[408]65        pos = endpos = nextpos
66        minfile = self.minfile
67        if minfile[pos : pos+8] == r"%-12345X" :
68            endpos = pos + 9
69        elif minfile[pos-1] in ESCAPECHARS :   
70            endpos = pos
71        else :   
72            return 0
73        endmark = (chr(0x1b), chr(0x00))
74        asciilimit = chr(0x80)
75        quotes = 0
76        while (minfile[endpos] not in endmark) and \
77               ((minfile[endpos] < asciilimit) or (quotes % 2)) :
78            if minfile[endpos] == '"' :
79                quotes += 1
80            endpos += 1
81           
82        # Store this in a per page mapping.   
83        # NB : First time will be at page 0 (i.e. **before** page 1) !
84        stuff = self.escapedStuff.setdefault(self.pagecount, [])
[409]85        datas = minfile[pos-1 : endpos]
86        stuff.append(datas)
87        if datas.endswith("$PJL BITMAP START\r\n") :
88            self.isbitmap = True
89        self.logdebug("Escaped datas : [%s]" % repr(datas))
[408]90        return endpos - pos + 1
91       
92    def getJobSize(self) :
93        """Counts pages in an SPL1 document.
94       
95           Algorithm by Jerome Alet.
96        """
97        infileno = self.infile.fileno()
[409]98        self.minfile = minfile = mmap.mmap(infileno, os.fstat(infileno)[6], prot=mmap.PROT_READ, flags=mmap.MAP_SHARED)
[408]99        self.pagecount = 0
100        self.escapedStuff = {}   # For escaped datas, mostly PJL commands
101        self.bigEndian()
102       
[409]103        self.isbitmap = False
[408]104        pos = 0
105        try :
106            try :
107                while 1 :
[409]108                    tag = minfile[pos]
[408]109                    if tag in ESCAPECHARS :
110                        pos += self.escape(pos+1)
111                    else :   
[409]112                        if not self.isbitmap :
[410]113                            raise pdlparser.PDLParserError, "Unfortunately SPL1 is incompletely recognized. Parsing aborted. Please report the problem to %s" % version.__authoremail__
[409]114                        offset = unpack(self.unpackLong, minfile[pos:pos+4])[0]
115                        sequencenum = unpack(self.unpackShort, minfile[pos+4:pos+6])[0]
116                        codesop = " ".join([ "%02x" % ord(v) for v in minfile[pos+6:pos+12]])
117                        if codesop != "06 00 00 80 13 40" :
[410]118                            raise pdlparser.PDLParserError, "Unfortunately SPL1 is incompletely recognized. Parsing aborted. Please report the problem to %s" % version.__authoremail__
[409]119                        if not sequencenum :
120                            self.pagecount += 1
[408]121                        pos += 4 + offset
122            except IndexError : # EOF ?           
123                pass
124        finally :
[409]125            minfile.close()
[408]126        return self.pagecount
127       
128if __name__ == "__main__" :   
[415]129    pdlparser.test(Parser)
Note: See TracBrowser for help on using the browser.