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 TIFF documents.""" |
---|
23 | |
---|
24 | import sys |
---|
25 | import os |
---|
26 | import mmap |
---|
27 | from struct import unpack |
---|
28 | |
---|
29 | import pdlparser |
---|
30 | import pjl |
---|
31 | |
---|
32 | class Parser(pdlparser.PDLParser) : |
---|
33 | """A parser for ESC/PageS03 documents.""" |
---|
34 | format = "ESC/PageS03" |
---|
35 | def isValid(self) : |
---|
36 | """Returns True if data is TIFF, else False.""" |
---|
37 | if self.firstblock.startswith("\033\1@EJL") and \ |
---|
38 | (self.firstblock.find("=ESC/PAGES03\n") != -1) : |
---|
39 | return True |
---|
40 | else : |
---|
41 | return False |
---|
42 | |
---|
43 | def getJobSize(self) : |
---|
44 | """Counts pages in an ESC/PageS03 document. |
---|
45 | |
---|
46 | Algorithm by Jerome Alet. |
---|
47 | Reverse engineered the file format. |
---|
48 | """ |
---|
49 | infileno = self.infile.fileno() |
---|
50 | minfile = mmap.mmap(infileno, os.fstat(infileno)[6], prot=mmap.PROT_READ, flags=mmap.MAP_SHARED) |
---|
51 | pagecount = 0 |
---|
52 | marker = "=ESC/PAGES03\n" |
---|
53 | startpos = minfile.find(marker) |
---|
54 | startsequence = chr(0x1d) |
---|
55 | if startpos == -1 : |
---|
56 | raise pdlparser.PDLParserError, "Invalid ESC/PageS03 file." |
---|
57 | startpos += len(marker) |
---|
58 | if minfile[startpos] != startsequence : |
---|
59 | raise pdlparser.PDLParserError, "Invalid ESC/PageS03 file." |
---|
60 | endsequence = "eps{I" |
---|
61 | lgendsequence = len(endsequence) |
---|
62 | try : |
---|
63 | try : |
---|
64 | while True : |
---|
65 | if minfile[startpos] == startsequence : |
---|
66 | skiplen = 0 |
---|
67 | while True : |
---|
68 | startpos += 1 |
---|
69 | c = minfile[startpos] |
---|
70 | if not c.isdigit() : |
---|
71 | break |
---|
72 | else : |
---|
73 | skiplen = (skiplen * 10) + int(c) |
---|
74 | if minfile[startpos:startpos+lgendsequence] == endsequence : |
---|
75 | startpos += (skiplen + lgendsequence) |
---|
76 | else : |
---|
77 | if minfile[startpos:startpos+6] == "\033\1@EJL" : |
---|
78 | # Probably near the end of the file. |
---|
79 | # Test suite was too small to be sure. |
---|
80 | ejlparser = pjl.EJLParser(minfile[startpos:]) |
---|
81 | pagecount = ejlparser.environment_variables.get("PAGES", "1") |
---|
82 | if pagecount.startswith('"') and pagecount.endswith('"') : |
---|
83 | pagecount = pagecount[1:-1] |
---|
84 | pagecount = int(pagecount) |
---|
85 | if pagecount <= 0 : |
---|
86 | pagecount = 1 # TODO : 0 or 1000000 ??? ;-) |
---|
87 | break |
---|
88 | startpos += 1 |
---|
89 | except IndexError : |
---|
90 | pass |
---|
91 | finally : |
---|
92 | minfile.close() |
---|
93 | return pagecount |
---|