root / pykota / trunk / bin / pykotme @ 2727

Revision 2622, 6.3 kB (checked in by jerome, 19 years ago)

Added 2006 to the copyright's years.

  • 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# PyKota Print Quota Quote sender
5#
6# PyKota - Print Quotas for CUPS and LPRng
7#
8# (c) 2003, 2004, 2005, 2006 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22#
23# $Id$
24#
25#
26
27import sys
28import os
29import pwd
30
31from pykota.tool import PyKotaTool, PyKotaToolError, PyKotaCommandLineError, crashed, N_
32
33try :
34    from pkpgpdls import analyzer, pdlparser
35except ImportError : # TODO : Remove the try/except after release 1.24.
36    sys.stderr.write("ERROR: pkpgcounter is now distributed separately, please grab it from http://www.librelogiciel.com/software/pkpgcounter/action_Download\n")
37   
38
39__doc__ = N_("""pykotme v%(__version__)s (c) %(__years__)s %(__author__)s
40
41Gives print quotes to users.
42
43command line usage :
44
45  pykotme  [options]  [files]
46
47options :
48
49  -v | --version       Prints pykotme's version number then exits.
50  -h | --help          Prints this message then exits.
51 
52  -P | --printer p     Gives a quote for this printer only. Actually p can
53                       use wildcards characters to select only
54                       some printers. The default value is *, meaning
55                       all printers.
56                       You can specify several names or wildcards,
57                       by separating them with commas.
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 --printer apple,hplaser <file1.ps
68 
69  This will give a print quote to the current user. The quote will show
70  the price and size of a job consisting in file1.ps as read from
71  standard input, which would be sent to the apple or hplaser
72  printer.
73
74  $ pykotme
75 
76  This will give a quote for a job consisting of what is on standard
77  input. The quote will list the job size, and the price the job
78  would cost on each printer.
79""")
80       
81       
82class PyKotMe(PyKotaTool) :       
83    """A class for pykotme."""
84    def main(self, files, options) :
85        """Gives print quotes."""
86        if (not sys.stdin.isatty()) and ("-" not in files) :
87            files.append("-")
88        totalsize = 0   
89        for filename in files :   
90            try :
91                parser = analyzer.PDLAnalyzer(filename)
92                totalsize += parser.getJobSize()
93            except pdlparser.PDLParserError, msg :   
94                self.printInfo(msg)
95           
96        printers = self.storage.getMatchingPrinters(options["printer"])
97        if not printers :
98            raise PyKotaCommandLineError, _("There's no printer matching %s") % options["printer"]
99           
100        username = pwd.getpwuid(os.getuid())[0]
101        user = self.storage.getUser(username)
102        if user.Exists and user.LimitBy and (user.LimitBy.lower() == "balance"):
103            print _("Your account balance : %.2f") % (user.AccountBalance or 0.0)
104           
105        print _("Job size : %i pages") % totalsize   
106        if user.Exists :
107            if user.LimitBy == "noprint" :
108                print _("Your account settings forbid you to print at this time.")
109            else :   
110                for printer in printers :
111                    userpquota = self.storage.getUserPQuota(user, printer)
112                    if userpquota.Exists :
113                        if printer.MaxJobSize and (totalsize > printer.MaxJobSize) :
114                            print _("You are not allowed to print so many pages on printer %s at this time.") % printer.Name
115                        else :   
116                            cost = userpquota.computeJobPrice(totalsize)
117                            msg = _("Cost on printer %s : %.2f") % (printer.Name, cost)
118                            if printer.PassThrough :
119                                msg = "%s (%s)" % (msg, _("won't be charged, printer is in passthrough mode"))
120                            elif user.LimitBy == "nochange" :   
121                                msg = "%s (%s)" % (msg, _("won't be charged, your account is immutable"))
122                            print msg   
123           
124if __name__ == "__main__" : 
125    retcode = 0
126    try :
127        defaults = { \
128                     "printer" : "*", \
129                   }
130        short_options = "vhP:"
131        long_options = ["help", "version", "printer="]
132       
133        # Initializes the command line tool
134        sender = PyKotMe(doc=__doc__)
135        sender.deferredInit()
136       
137        # parse and checks the command line
138        (options, args) = sender.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=1)
139       
140        # sets long options
141        options["help"] = options["h"] or options["help"]
142        options["version"] = options["v"] or options["version"]
143        options["printer"] = options["P"] or options["printer"] or defaults["printer"]
144       
145        if options["help"] :
146            sender.display_usage_and_quit()
147        elif options["version"] :
148            sender.display_version_and_quit()
149        else :
150            retcode = sender.main(args, options)
151    except KeyboardInterrupt :       
152        sys.stderr.write("\nInterrupted with Ctrl+C !\n")
153        retcode = -3
154    except PyKotaCommandLineError, msg :   
155        sys.stderr.write("%s : %s\n" % (sys.argv[0], msg))
156        retcode = -2
157    except SystemExit :       
158        pass
159    except :
160        try :
161            sender.crashed("pykotme failed")
162        except :   
163            crashed("pykotme failed")
164        retcode = -1
165
166    try :
167        sender.storage.close()
168    except (TypeError, NameError, AttributeError) :   
169        pass
170       
171    sys.exit(retcode)   
Note: See TracBrowser for help on using the browser.