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