[193] | 1 | #! /usr/bin/env python |
---|
| 2 | # -*- coding: ISO-8859-15 -*- |
---|
[191] | 3 | # |
---|
| 4 | # pkpgcounter : a generic Page Description Language parser |
---|
| 5 | # |
---|
[303] | 6 | # (c) 2003, 2004, 2005, 2006 Jerome Alet <alet@librelogiciel.com> |
---|
[191] | 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 |
---|
[211] | 19 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
[191] | 20 | # |
---|
| 21 | # $Id$ |
---|
| 22 | # |
---|
[193] | 23 | |
---|
[357] | 24 | """This modules implements a page counter for ESC/P2 documents.""" |
---|
| 25 | |
---|
[193] | 26 | import sys |
---|
| 27 | |
---|
[235] | 28 | import pdlparser |
---|
[193] | 29 | |
---|
[220] | 30 | class Parser(pdlparser.PDLParser) : |
---|
[193] | 31 | """A parser for ESC/P2 documents.""" |
---|
[220] | 32 | def isValid(self) : |
---|
| 33 | """Returns 1 if data is ESC/P2, else 0.""" |
---|
| 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 |
---|
[252] | 38 | self.logdebug("DEBUG: Input file is in the ESC/P2 format.") |
---|
[220] | 39 | return 1 |
---|
| 40 | else : |
---|
| 41 | return 0 |
---|
| 42 | |
---|
[193] | 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 | def test() : |
---|
| 79 | """Test function.""" |
---|
[196] | 80 | if (len(sys.argv) < 2) or ((not sys.stdin.isatty()) and ("-" not in sys.argv[1:])) : |
---|
| 81 | sys.argv.append("-") |
---|
| 82 | totalsize = 0 |
---|
| 83 | for arg in sys.argv[1:] : |
---|
| 84 | if arg == "-" : |
---|
| 85 | infile = sys.stdin |
---|
| 86 | mustclose = 0 |
---|
| 87 | else : |
---|
| 88 | infile = open(arg, "rb") |
---|
| 89 | mustclose = 1 |
---|
| 90 | try : |
---|
[220] | 91 | parser = Parser(infile, debug=1) |
---|
[196] | 92 | totalsize += parser.getJobSize() |
---|
[200] | 93 | except pdlparser.PDLParserError, msg : |
---|
[196] | 94 | sys.stderr.write("ERROR: %s\n" % msg) |
---|
| 95 | sys.stderr.flush() |
---|
| 96 | if mustclose : |
---|
| 97 | infile.close() |
---|
| 98 | print "%s" % totalsize |
---|
[193] | 99 | |
---|
| 100 | if __name__ == "__main__" : |
---|
| 101 | test() |
---|