root / pykota / trunk / bin / pykotme @ 2225

Revision 2216, 5.8 kB (checked in by jerome, 20 years ago)

Now exits with no traceback in case of Ctrl+C

  • 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22#
23# $Id$
24#
25#
26
27import sys
28import os
29import pwd
30
31from pykota.tool import PyKotaTool, PyKotaToolError, crashed, N_
32from pykota.pdlanalyzer import PDLAnalyzer, PDLAnalyzerError
33
34__doc__ = N_("""pykotme v%s (c) 2003, 2004, 2005 C@LL - Conseil Internet & Logiciels Libres
35
36Gives print quotes to users.
37
38command line usage :
39
40  pykotme  [options]  [files]
41
42options :
43
44  -v | --version       Prints pykotme's version number then exits.
45  -h | --help          Prints this message then exits.
46 
47  -P | --printer p     Gives a quote for this printer only. Actually p can
48                       use wildcards characters to select only
49                       some printers. The default value is *, meaning
50                       all printers.
51                       You can specify several names or wildcards,
52                       by separating them with commas.
53 
54examples :                             
55
56  $ pykotme --printer apple file1.ps file2.ps
57 
58  This will give a print quote to the current user. The quote will show
59  the price and size of a job consisting in file1.ps and file2.ps
60  which would be sent to the apple printer.
61 
62  $ pykotme --printer apple,hplaser <file1.ps
63 
64  This will give a print quote to the current user. The quote will show
65  the price and size of a job consisting in file1.ps as read from
66  standard input, which would be sent to the apple or hplaser
67  printer.
68
69  $ pykotme
70 
71  This will give a quote for a job consisting of what is on standard
72  input. The quote will list the job size, and the price the job
73  would cost on each printer.
74
75This program is free software; you can redistribute it and/or modify
76it under the terms of the GNU General Public License as published by
77the Free Software Foundation; either version 2 of the License, or
78(at your option) any later version.
79
80This program is distributed in the hope that it will be useful,
81but WITHOUT ANY WARRANTY; without even the implied warranty of
82MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
83GNU General Public License for more details.
84
85You should have received a copy of the GNU General Public License
86along with this program; if not, write to the Free Software
87Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
88
89Please e-mail bugs to: %s""")
90       
91       
92class PyKotMe(PyKotaTool) :       
93    """A class for pykotme."""
94    def main(self, files, options) :
95        """Gives print quotes."""
96        if (not sys.stdin.isatty()) and ("-" not in files) :
97            files.append("-")
98        totalsize = 0   
99        for filename in files :   
100            try :
101                parser = PDLAnalyzer(filename)
102                totalsize += parser.getJobSize()
103            except PDLAnalyzerError, msg :   
104                self.printInfo(msg)
105           
106        # get current user
107        username = pwd.getpwuid(os.geteuid())[0]
108        user = self.storage.getUser(username)
109        if user.Exists and user.LimitBy and (user.LimitBy.lower() == "balance"):
110            print _("Your account balance : %.2f") % (user.AccountBalance or 0.0)
111           
112        printers = self.storage.getMatchingPrinters(options["printer"])
113        if not printers :
114            raise PyKotaToolError, _("There's no printer matching %s") % options["printer"]
115           
116        print _("Job size : %i pages") % totalsize   
117        for printer in printers :
118            userpquota = self.storage.getUserPQuota(user, printer)
119            cost = userpquota.computeJobPrice(totalsize)
120            print _("Cost on printer %s : %.2f") % (printer.Name, cost)
121           
122if __name__ == "__main__" : 
123    retcode = 0
124    try :
125        defaults = { \
126                     "printer" : "*", \
127                   }
128        short_options = "vhP:"
129        long_options = ["help", "version", "printer="]
130       
131        # Initializes the command line tool
132        sender = PyKotMe(doc=__doc__)
133        sender.deferredInit()
134       
135        # parse and checks the command line
136        (options, args) = sender.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=1)
137       
138        # sets long options
139        options["help"] = options["h"] or options["help"]
140        options["version"] = options["v"] or options["version"]
141        options["printer"] = options["P"] or options["printer"] or defaults["printer"]
142       
143        if options["help"] :
144            sender.display_usage_and_quit()
145        elif options["version"] :
146            sender.display_version_and_quit()
147        else :
148            retcode = sender.main(args, options)
149    except KeyboardInterrupt :       
150        sys.stderr.write("\nInterrupted with Ctrl+C !\n")
151    except SystemExit :       
152        pass
153    except :
154        try :
155            sender.crashed("pykotme failed")
156        except :   
157            crashed("pykotme failed")
158        retcode = -1
159
160    try :
161        sender.storage.close()
162    except (TypeError, NameError, AttributeError) :   
163        pass
164       
165    sys.exit(retcode)   
Note: See TracBrowser for help on using the browser.