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

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

Fixed handling of number of copies as defined through PJL statements.

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