root / pykota / trunk / bin / pykotme @ 1064

Revision 1057, 5.9 kB (checked in by jalet, 21 years ago)

Now includes the pykotme utility

  • 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
3# PyKota Print Quota Quote sender
4#
5# PyKota - Print Quotas for CUPS and LPRng
6#
7# (c) 2003 Jerome Alet <alet@librelogiciel.com>
8# This program is free software; you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation; either version 2 of the License, or
11# (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with this program; if not, write to the Free Software
20# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
21#
22# $Id$
23#
24# $Log$
25# Revision 1.1  2003/07/03 09:44:01  jalet
26# Now includes the pykotme utility
27#
28#
29#
30
31import sys
32import os
33import pwd
34import grp
35
36from pykota import version
37from pykota.tool import PyKotaTool, PyKotaToolError
38from pykota.config import PyKotaConfigError
39from pykota.storage import PyKotaStorageError
40
41__doc__ = """pykotme v%s (C) 2003 C@LL - Conseil Internet & Logiciels Libres
42
43Gives print quotes to users.
44
45command line usage :
46
47  pykotme  [options]  [files]
48
49options :
50
51  -v | --version       Prints pykotme's version number then exits.
52  -h | --help          Prints this message then exits.
53 
54  -P | --printer p     Gives a quote for this printer only. Actually p can
55                       use wildcards characters to select only
56                       some printers. The default value is *, meaning
57                       all printers.
58 
59examples :                             
60
61  $ pykotme --printer apple file1.ps file2.ps
62 
63  This will give a print quote to the current user. The quote will show
64  the price and size of a job consisting in file1.ps and file2.ps
65  which would be sent to the apple printer.
66
67  $ pykotme
68 
69  This will give a quote for a job consisting of what is on standard
70  input. The quote will list the job size, and the price the job
71  would cost on each printer.
72
73
74This program is free software; you can redistribute it and/or modify
75it under the terms of the GNU General Public License as published by
76the Free Software Foundation; either version 2 of the License, or
77(at your option) any later version.
78
79This program is distributed in the hope that it will be useful,
80but WITHOUT ANY WARRANTY; without even the implied warranty of
81MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
82GNU General Public License for more details.
83
84You should have received a copy of the GNU General Public License
85along with this program; if not, write to the Free Software
86Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
87
88Please e-mail bugs to: %s""" % (version.__version__, version.__author__)
89       
90class PyKotMe(PyKotaTool) :       
91    """A class for pykotme."""
92    def main(self, files, options) :
93        """Gives print quotes."""
94        if not files :
95            files = [ sys.stdin ]
96           
97        nbpages = 0   
98        for filename in files :
99            if filename is sys.stdin :
100                stream = filename
101            else :
102                if filename == "-" :
103                    stream = sys.stdin
104                else :   
105                    try :
106                        stream = open(filename, "rb")
107                    except IOError, msg :   
108                        sys.stderr.write("%s\n" % msg)
109                        continue
110            nb = self.getNbPages(stream)
111            if nb is None :
112                sys.stderr.write(_("Unable to compute size of %s in number of pages.\n") % filename)
113            else :
114                nbpages += nb
115            if not (filename is sys.stdin) :       
116                stream.close()
117               
118        printers = self.storage.getMatchingPrinters(options["printer"])
119        if not printers :
120            raise PyKotaToolError, _("There's no printer matching %s") % options["printer"]
121        print _("Job size : %i pages") % nbpages   
122        for printer in printers :
123            cost = (nbpages * float(printer.PricePerPage or 0)) + float(printer.PricePerJob or 0)
124            print _("Cost on printer %s : %.2f") % (printer.Name, cost)
125           
126    def getNbPages(self, stream) :       
127        """Tries to compute the file's size in number of pages, the best we can."""
128        nbpages = [] # in case some user wants to put several %%Pages: entries in the file
129        for line in stream.xreadlines() :
130            line = line.strip()
131            if line.startswith("%%Pages: ") :
132                try :
133                    nbpages.append(int(line[9:]))
134                except ValueError :   
135                    pass
136        if nbpages :           
137            return max(nbpages)
138       
139if __name__ == "__main__" : 
140    try :
141        defaults = { \
142                     "printer" : "*", \
143                   }
144        short_options = "vhP:"
145        long_options = ["help", "version", "printer="]
146       
147        # Initializes the command line tool
148        sender = PyKotMe(doc=__doc__)
149       
150        # parse and checks the command line
151        (options, args) = sender.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=1)
152       
153        # sets long options
154        options["help"] = options["h"] or options["help"]
155        options["version"] = options["v"] or options["version"]
156        options["printer"] = options["P"] or options["printer"] or defaults["printer"]
157       
158        if options["help"] :
159            sender.display_usage_and_quit()
160        elif options["version"] :
161            sender.display_version_and_quit()
162        else :
163            sys.exit(sender.main(args, options))
164    except (PyKotaToolError, PyKotaConfigError, PyKotaStorageError), msg :           
165        sys.stderr.write("%s\n" % msg)
166        sys.stderr.flush()
167        sys.exit(-1)
Note: See TracBrowser for help on using the browser.