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

Revision 303, 3.4 kB (checked in by jerome, 18 years ago)

Changed copyright years.
v1.74 is out

  • 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
24import sys
25
26import pdlparser
27
28class Parser(pdlparser.PDLParser) :
29    """A parser for ESC/P2 documents."""
30    def isValid(self) :       
31        """Returns 1 if data is ESC/P2, else 0."""
32        if self.firstblock.startswith("\033@") or \
33           self.firstblock.startswith("\033*") or \
34           self.firstblock.startswith("\n\033@") or \
35           self.firstblock.startswith("\0\0\0\033\1@EJL") : # ESC/P Raster ??? Seen on Stylus Photo 1284
36            self.logdebug("DEBUG: Input file is in the ESC/P2 format.")
37            return 1
38        else :   
39            return 0
40           
41    def getJobSize(self) :   
42        """Counts pages in an ESC/P2 document."""
43        # with Gimpprint, at least, for each page there
44        # are two Reset Printer sequences (ESC + @)
45        marker1 = "\033@"
46       
47        # with other software or printer driver, we
48        # may prefer to search for "\r\n\fESCAPE"
49        # or "\r\fESCAPE"
50        marker2r = "\r\f\033"
51        marker2rn = "\r\n\f\033"
52       
53        # and ghostscript's stcolor for example seems to
54        # output ESC + @ + \f for each page plus one
55        marker3 = "\033@\f"
56       
57        # while ghostscript's escp driver outputs instead
58        # \f + ESC + @
59        marker4 = "\f\033@"
60       
61        data = self.infile.read()
62        pagecount1 = data.count(marker1)
63        pagecount2 = max(data.count(marker2r), data.count(marker2rn))
64        pagecount3 = data.count(marker3)
65        pagecount4 = data.count(marker4)
66           
67        if pagecount2 :   
68            return pagecount2
69        elif pagecount3 > 1 :     
70            return pagecount3 - 1
71        elif pagecount4 :   
72            return pagecount4
73        else :   
74            return int(pagecount1 / 2)       
75       
76def test() :       
77    """Test function."""
78    if (len(sys.argv) < 2) or ((not sys.stdin.isatty()) and ("-" not in sys.argv[1:])) :
79        sys.argv.append("-")
80    totalsize = 0   
81    for arg in sys.argv[1:] :
82        if arg == "-" :
83            infile = sys.stdin
84            mustclose = 0
85        else :   
86            infile = open(arg, "rb")
87            mustclose = 1
88        try :
89            parser = Parser(infile, debug=1)
90            totalsize += parser.getJobSize()
91        except pdlparser.PDLParserError, msg :   
92            sys.stderr.write("ERROR: %s\n" % msg)
93            sys.stderr.flush()
94        if mustclose :   
95            infile.close()
96    print "%s" % totalsize
97   
98if __name__ == "__main__" :   
99    test()
Note: See TracBrowser for help on using the browser.