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 3 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, see <http://www.gnu.org/licenses/>. |
---|
19 | # |
---|
20 | # $Id$ |
---|
21 | # |
---|
22 | |
---|
23 | """This modules implements a page counter for SPL1 documents.""" |
---|
24 | |
---|
25 | import sys |
---|
26 | import os |
---|
27 | import mmap |
---|
28 | from struct import unpack |
---|
29 | |
---|
30 | import pdlparser |
---|
31 | import version |
---|
32 | |
---|
33 | ESCAPECHARS = (chr(0x1b), chr(0x24)) |
---|
34 | |
---|
35 | class 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.""" |
---|
64 | self.isbitmap = False |
---|
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, []) |
---|
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)) |
---|
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() |
---|
98 | self.minfile = minfile = mmap.mmap(infileno, os.fstat(infileno)[6], prot=mmap.PROT_READ, flags=mmap.MAP_SHARED) |
---|
99 | self.pagecount = 0 |
---|
100 | self.escapedStuff = {} # For escaped datas, mostly PJL commands |
---|
101 | self.bigEndian() |
---|
102 | |
---|
103 | self.isbitmap = False |
---|
104 | pos = 0 |
---|
105 | try : |
---|
106 | try : |
---|
107 | while 1 : |
---|
108 | tag = minfile[pos] |
---|
109 | if tag in ESCAPECHARS : |
---|
110 | pos += self.escape(pos+1) |
---|
111 | else : |
---|
112 | if not self.isbitmap : |
---|
113 | raise pdlparser.PDLParserError, "Unfortunately SPL1 is incompletely recognized. Parsing aborted. Please report the problem to %s" % version.__authoremail__ |
---|
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" : |
---|
118 | raise pdlparser.PDLParserError, "Unfortunately SPL1 is incompletely recognized. Parsing aborted. Please report the problem to %s" % version.__authoremail__ |
---|
119 | if not sequencenum : |
---|
120 | self.pagecount += 1 |
---|
121 | pos += 4 + offset |
---|
122 | except IndexError : # EOF ? |
---|
123 | pass |
---|
124 | finally : |
---|
125 | minfile.close() |
---|
126 | return self.pagecount |
---|
127 | |
---|
128 | if __name__ == "__main__" : |
---|
129 | pdlparser.test(Parser) |
---|