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

Revision 196, 2.9 kB (checked in by jerome, 19 years ago)

Added test functions

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