1 | # -*- coding: utf-8 -*- |
---|
2 | # |
---|
3 | # pkpgcounter : a generic Page Description Language parser |
---|
4 | # |
---|
5 | # (c) 2003, 2004, 2005, 2006, 2007, 2008 Jerome Alet <alet@librelogiciel.com> |
---|
6 | # This program is free software: you can redistribute it and/or modify |
---|
7 | # it under the terms of the GNU General Public License as published by |
---|
8 | # the Free Software Foundation, either version 3 of the License, or |
---|
9 | # (at your option) any later version. |
---|
10 | # |
---|
11 | # This program is distributed in the hope that it will be useful, |
---|
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | # GNU General Public License for more details. |
---|
15 | # |
---|
16 | # You should have received a copy of the GNU General Public License |
---|
17 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
18 | # |
---|
19 | # $Id$ |
---|
20 | # |
---|
21 | |
---|
22 | """This modules implements a page counter for SPL1 documents.""" |
---|
23 | |
---|
24 | import os |
---|
25 | import mmap |
---|
26 | import struct |
---|
27 | |
---|
28 | import pdlparser |
---|
29 | import version |
---|
30 | |
---|
31 | ESCAPECHARS = (chr(0x1b), chr(0x24)) |
---|
32 | |
---|
33 | class Parser(pdlparser.PDLParser) : |
---|
34 | """A parser for SPL1 documents.""" |
---|
35 | format = "SPL1 (aka GDI)" |
---|
36 | def isValid(self) : |
---|
37 | """Returns True if data is SPL1, else False.""" |
---|
38 | if ((self.firstblock[:128].find("\033%-12345X") != -1) and \ |
---|
39 | (self.firstblock.find("$PJL ") != -1) and \ |
---|
40 | ((self.firstblock.find("LANGUAGE=SMART") != -1) or \ |
---|
41 | (self.firstblock.find("LANGUAGE = SMART") != -1))) : |
---|
42 | return True |
---|
43 | else : |
---|
44 | return False |
---|
45 | |
---|
46 | def littleEndian(self) : |
---|
47 | """Toggles to little endianness.""" |
---|
48 | self.unpackType = { 1 : "B", 2 : "<H", 4 : "<I" } |
---|
49 | self.unpackShort = self.unpackType[2] |
---|
50 | self.unpackLong = self.unpackType[4] |
---|
51 | # self.logdebug("Little Endian") |
---|
52 | return 0 |
---|
53 | |
---|
54 | def bigEndian(self) : |
---|
55 | """Toggles to big endianness.""" |
---|
56 | self.unpackType = { 1 : "B", 2 : ">H", 4 : ">I" } |
---|
57 | self.unpackShort = self.unpackType[2] |
---|
58 | self.unpackLong = self.unpackType[4] |
---|
59 | # self.logdebug("Big Endian") |
---|
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("New bitmap") |
---|
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 | self.isbitmap = False |
---|
104 | pos = 0 |
---|
105 | unpack = struct.unpack |
---|
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, |
---|
116 | seqnum) = unpack(">IH", minfile[pos:pos+6]) |
---|
117 | # self.logdebug("Offset : %i Sequence Number : %i" % (offset, seqnum)) |
---|
118 | if not seqnum : |
---|
119 | # Sequence number resets to 0 for each new page. |
---|
120 | self.pagecount += 1 |
---|
121 | pos += 4 + offset |
---|
122 | except struct.error, msg : |
---|
123 | raise pdlparser.PDLParserError, "Unfortunately SPL1 is incompletely recognized (%s). Parsing aborted. Please report the problem to %s" % (msg, version.__authoremail__) |
---|
124 | except IndexError : # EOF ? |
---|
125 | pass |
---|
126 | finally : |
---|
127 | minfile.close() |
---|
128 | return self.pagecount |
---|