root / pykota / trunk / bin / pkpgcounter @ 1430

Revision 1430, 5.0 kB (checked in by jalet, 20 years ago)

pkpgcounter added

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
Line 
1#! /usr/bin/env python
2# -*- coding: ISO-8859-15 -*-
3
4# pkpgcounter, a smart software page counter
5#
6# PyKota - Print Quotas for CUPS and LPRng
7#
8# (c) 2003-2004 Jerome Alet <alet@librelogiciel.com>
9# This program is free software; you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation; either version 2 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program; if not, write to the Free Software
21# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22#
23# $Id$
24#
25# $Log$
26# Revision 1.1  2004/04/08 17:07:42  jalet
27# pkpgcounter added
28#
29#
30
31import sys
32import tempfile
33
34def ispostscript(data) :   
35    """Returns 1 if data is PostScript, else 0."""
36    if data.startswith("%!") or \
37       data.startswith("\004%!") or \
38       data.startswith("\033%-12345X%!PS") or \
39       ((data[:128].find("\033%-12345X") != -1) and \
40         ((data.find("LANGUAGE=POSTSCRIPT") != -1) or \
41          (data.find("LANGUAGE = POSTSCRIPT") != -1) or \
42          (data.find("LANGUAGE = Postscript") != -1))) :
43        return 1
44    else :   
45        return 0
46   
47def postscript(infile) :
48    """Count pages in a PostScript document."""
49    pagecount = 0
50    pagenum = None
51    while 1 :
52        line = infile.readline()
53        if not line :
54            break
55        if line.startswith("%%Page: ") :
56            pagecount += 1
57    return pagecount
58   
59def ispcl(data) :   
60    """Returns 1 if data is PCL, else 0."""
61    if data.startswith("\033E\033") or \
62       ((data[:128].find("\033%-12345X") != -1) and \
63         ((data.find("LANGUAGE=PCL") != -1) or \
64          (data.find("LANGUAGE = PCL") != -1) or \
65          (data.find("LANGUAGE = Pcl") != -1))) :
66   
67def pcl(infile) :   
68    """Count pages in a PostScript document."""
69    #
70    # Algorithm from pclcount
71    # (c) 2003, by Eduardo Gielamo Oliveira & Rodolfo Broco Manin
72    # published under the terms of the GNU General Public Licence v2.
73    #
74    # Backported from C to Python by Jerome Alet
75    #
76    tagsends = { "*b" : "W", "(s" : "W", ")s" : "W", "&p" : "X", "&l" : "X" } 
77    copies = 1
78    pagecount = 0
79    tag = None
80    while 1 :
81        char = infile.read(1)   # TODO : speed this up if possible
82        if not char :
83            break
84        if char == "\014" :   
85            pagecount += 1
86        elif char == "\033" :   
87            #
88            #     <ESC>*b###W -> Start of a binary block
89            #     <ESC>(s###W -> Start of a characters description block
90            #     <ESC>)s###W -> Start of a fonts description block
91            #     <ESC>&p###X -> Start of a non printable characters block
92            #     <ESC>&l###X -> Number of copies
93            #
94            tag = infile.read(2)
95            try :
96                tagend = tagsends[tag]
97            except KeyError :   
98                pass    # Unsupported PCL tag
99            else :   
100                # Now read the numeric argument
101                size = 0
102                while 1 :
103                    char = infile.read(1)
104                    if not char.isdigit() :
105                        break
106                    size = (size * 10) + int(char)   
107                if char == tagend :   
108                    if tag == "&l" :
109                        copies = size
110                    else :   
111                        # doing a read will prevent the seek
112                        # for unseekable streams.
113                        # we just ignore the block anyway.
114                        infile.read(size) 
115    return copies * pagecount       
116
117def smartpagecounter(filename) :
118    """Autodetects file format and returns number of pages."""
119    if filename == "-" :
120        # we must read from stdin
121        # but since stdin is not seekable, we have to use a temporary
122        # file instead.
123        infile = tempfile.TemporaryFile()
124        while 1 :
125            data = sys.stdin.read(256 * 1024) 
126            if not data :
127                break
128            infile.write(data)
129        infile.flush()   
130        infile.seek(0)
131    else :   
132        # normal file
133        infile = open(filename, "rb")
134       
135    # Try to detect file type by reading first block of datas   
136    firstblock = infile.read(1024)
137    infile.seek(0)
138    if ispostscript(firstblock) :
139        size = postscript(infile)
140    elif ispcl(firstblock) :   
141        size = pcl(infile)
142    else :   
143        sys.stderr.write("ERROR : Unknown file format for %s\n" % filename)
144        size = 0
145    infile.close()   
146    return size
147   
148if __name__ == "__main__" :   
149    if len(sys.argv) < 2 :
150        sys.argv.append("-")
151       
152    totalsize = 0   
153    for arg in sys.argv[1:] :
154        totalsize += smartpagecounter(arg)
155    print "%s" % totalsize
Note: See TracBrowser for help on using the browser.