root / pkpgcounter / trunk / pkpgpdls / escp2.py @ 387

Revision 387, 3.4 kB (checked in by jerome, 16 years ago)

Code cleanups.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Auth Date Id Rev
Line 
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
26import sys
27
28import pdlparser
29
30class 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       
78def test() :       
79    """Test function."""
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 :
91            parser = Parser(infile, debug=1)
92            totalsize += parser.getJobSize()
93        except pdlparser.PDLParserError, msg :   
94            sys.stderr.write("ERROR: %s\n" % msg)
95            sys.stderr.flush()
96        if mustclose :   
97            infile.close()
98    print "%s" % totalsize
99   
100if __name__ == "__main__" :   
101    test()
Note: See TracBrowser for help on using the browser.