root / pykota / trunk / bin / pykotme @ 1099

Revision 1099, 6.4 kB (checked in by jalet, 21 years ago)

Better documentation.
pykotme now displays the current user's account balance.
Some test changed in ldap module.

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