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