root / pykota / trunk / bin / pykotme @ 2926

Revision 2909, 6.1 kB (checked in by jerome, 18 years ago)

Updated URLs.

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