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 ESC/P2 documents.""" |
---|
24 | |
---|
25 | import sys |
---|
26 | |
---|
27 | import pdlparser |
---|
28 | |
---|
29 | class Parser(pdlparser.PDLParser) : |
---|
30 | """A parser for ESC/P2 documents.""" |
---|
31 | def isValid(self) : |
---|
32 | """Returns True if data is ESC/P2, else False.""" |
---|
33 | if self.firstblock.startswith("\033@") or \ |
---|
34 | self.firstblock.startswith("\033*") or \ |
---|
35 | self.firstblock.startswith("\n\033@") or \ |
---|
36 | self.firstblock.startswith("\0\0\0\033\1@EJL") : # ESC/P Raster ??? Seen on Stylus Photo 1284 |
---|
37 | self.logdebug("DEBUG: Input file is in the ESC/P2 format.") |
---|
38 | return True |
---|
39 | else : |
---|
40 | return False |
---|
41 | |
---|
42 | def getJobSize(self) : |
---|
43 | """Counts pages in an ESC/P2 document.""" |
---|
44 | # with Gimpprint, at least, for each page there |
---|
45 | # are two Reset Printer sequences (ESC + @) |
---|
46 | marker1 = "\033@" |
---|
47 | |
---|
48 | # with other software or printer driver, we |
---|
49 | # may prefer to search for "\r\n\fESCAPE" |
---|
50 | # or "\r\fESCAPE" |
---|
51 | marker2r = "\r\f\033" |
---|
52 | marker2rn = "\r\n\f\033" |
---|
53 | |
---|
54 | # and ghostscript's stcolor for example seems to |
---|
55 | # output ESC + @ + \f for each page plus one |
---|
56 | marker3 = "\033@\f" |
---|
57 | |
---|
58 | # while ghostscript's escp driver outputs instead |
---|
59 | # \f + ESC + @ |
---|
60 | marker4 = "\f\033@" |
---|
61 | |
---|
62 | data = self.infile.read() |
---|
63 | pagecount1 = data.count(marker1) |
---|
64 | pagecount2 = max(data.count(marker2r), data.count(marker2rn)) |
---|
65 | pagecount3 = data.count(marker3) |
---|
66 | pagecount4 = data.count(marker4) |
---|
67 | |
---|
68 | if pagecount2 : |
---|
69 | return pagecount2 |
---|
70 | elif pagecount3 > 1 : |
---|
71 | return pagecount3 - 1 |
---|
72 | elif pagecount4 : |
---|
73 | return pagecount4 |
---|
74 | else : |
---|
75 | return int(pagecount1 / 2) |
---|
76 | |
---|
77 | if __name__ == "__main__" : |
---|
78 | pdlparser.test(Parser) |
---|