root / pykota / trunk / bin / pykotme @ 2352

Revision 2352, 5.4 kB (checked in by jerome, 19 years ago)

Fix to make pykotme and pykotme.cgi work with the now external pkpgcounter.
Removed unnecessary import in pkmail.
Removed deleted files from the installation tree.
Severity: Critical if you upgraded to 1.23alpha17 already.

  • 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 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, 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 PyKotaToolError, _("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            for printer in printers :
108                userpquota = self.storage.getUserPQuota(user, printer)
109                if userpquota.Exists :
110                    cost = userpquota.computeJobPrice(totalsize)
111                    print _("Cost on printer %s : %.2f") % (printer.Name, cost)
112           
113if __name__ == "__main__" : 
114    retcode = 0
115    try :
116        defaults = { \
117                     "printer" : "*", \
118                   }
119        short_options = "vhP:"
120        long_options = ["help", "version", "printer="]
121       
122        # Initializes the command line tool
123        sender = PyKotMe(doc=__doc__)
124        sender.deferredInit()
125       
126        # parse and checks the command line
127        (options, args) = sender.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=1)
128       
129        # sets long options
130        options["help"] = options["h"] or options["help"]
131        options["version"] = options["v"] or options["version"]
132        options["printer"] = options["P"] or options["printer"] or defaults["printer"]
133       
134        if options["help"] :
135            sender.display_usage_and_quit()
136        elif options["version"] :
137            sender.display_version_and_quit()
138        else :
139            retcode = sender.main(args, options)
140    except KeyboardInterrupt :       
141        sys.stderr.write("\nInterrupted with Ctrl+C !\n")
142    except SystemExit :       
143        pass
144    except :
145        try :
146            sender.crashed("pykotme failed")
147        except :   
148            crashed("pykotme failed")
149        retcode = -1
150
151    try :
152        sender.storage.close()
153    except (TypeError, NameError, AttributeError) :   
154        pass
155       
156    sys.exit(retcode)   
Note: See TracBrowser for help on using the browser.