root / pkpgcounter / trunk / pdlanalyzer / escp2.py @ 220

Revision 220, 3.4 kB (checked in by jerome, 19 years ago)

Big improvements on readability + maintainability

  • 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 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
26from pdlanalyzer import 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            if self.debug : 
37                sys.stderr.write("DEBUG: Input file is in the ESC/P2 format.\n")
38            return 1
39        else :   
40            return 0
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       
77def test() :       
78    """Test function."""
79    if (len(sys.argv) < 2) or ((not sys.stdin.isatty()) and ("-" not in sys.argv[1:])) :
80        sys.argv.append("-")
81    totalsize = 0   
82    for arg in sys.argv[1:] :
83        if arg == "-" :
84            infile = sys.stdin
85            mustclose = 0
86        else :   
87            infile = open(arg, "rb")
88            mustclose = 1
89        try :
90            parser = Parser(infile, debug=1)
91            totalsize += parser.getJobSize()
92        except pdlparser.PDLParserError, msg :   
93            sys.stderr.write("ERROR: %s\n" % msg)
94            sys.stderr.flush()
95        if mustclose :   
96            infile.close()
97    print "%s" % totalsize
98   
99if __name__ == "__main__" :   
100    test()
Note: See TracBrowser for help on using the browser.