root / pykota / trunk / bin / pykotme @ 1772

Revision 1584, 7.2 kB (checked in by jalet, 20 years ago)

Better dispatching of error messages

  • 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 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# $Log$
26# Revision 1.13  2004/07/01 19:56:42  jalet
27# Better dispatching of error messages
28#
29# Revision 1.12  2004/06/18 13:34:49  jalet
30# Now all tracebacks include PyKota's version number
31#
32# Revision 1.11  2004/06/07 18:43:40  jalet
33# Fixed over-verbose exits when displaying help or version number
34#
35# Revision 1.10  2004/06/03 21:50:34  jalet
36# Improved error logging.
37# crashrecipient directive added.
38# Now exports the job's size in bytes too.
39#
40# Revision 1.9  2004/05/21 20:53:34  jalet
41# Now pykotme doesn't spawn a new process anymore to compute job's size, but
42# use the PDLAnalyzer class directly
43#
44# Revision 1.8  2004/05/10 07:23:21  jalet
45# pykotme now uses pkpgcounter to compute the job's size.
46#
47# Revision 1.7  2004/01/12 22:43:40  jalet
48# New formula to compute a job's price
49#
50# Revision 1.6  2004/01/08 14:10:32  jalet
51# Copyright year changed.
52#
53# Revision 1.5  2003/10/09 21:25:25  jalet
54# Multiple printer names or wildcards can be passed on the command line
55# separated with commas.
56# Beta phase.
57#
58# Revision 1.4  2003/10/07 09:07:27  jalet
59# Character encoding added to please latest version of Python
60#
61# Revision 1.3  2003/07/29 20:55:17  jalet
62# 1.14 is out !
63#
64# Revision 1.2  2003/07/25 10:41:29  jalet
65# Better documentation.
66# pykotme now displays the current user's account balance.
67# Some test changed in ldap module.
68#
69# Revision 1.1  2003/07/03 09:44:01  jalet
70# Now includes the pykotme utility
71#
72#
73#
74
75import sys
76import os
77import pwd
78
79from pykota import version
80from pykota.tool import PyKotaTool, PyKotaToolError, crashed
81from pykota.config import PyKotaConfigError
82from pykota.storage import PyKotaStorageError
83from pykota.pdlanalyzer import PDLAnalyzer, PDLAnalyzerError
84
85__doc__ = """pykotme v%s (c) 2003-2004 C@LL - Conseil Internet & Logiciels Libres
86
87Gives print quotes to users.
88
89command line usage :
90
91  pykotme  [options]  [files]
92
93options :
94
95  -v | --version       Prints pykotme's version number then exits.
96  -h | --help          Prints this message then exits.
97 
98  -P | --printer p     Gives a quote for this printer only. Actually p can
99                       use wildcards characters to select only
100                       some printers. The default value is *, meaning
101                       all printers.
102                       You can specify several names or wildcards,
103                       by separating them with commas.
104 
105examples :                             
106
107  $ pykotme --printer apple file1.ps file2.ps
108 
109  This will give a print quote to the current user. The quote will show
110  the price and size of a job consisting in file1.ps and file2.ps
111  which would be sent to the apple printer.
112 
113  $ pykotme --printer apple,hplaser <file1.ps
114 
115  This will give a print quote to the current user. The quote will show
116  the price and size of a job consisting in file1.ps as read from
117  standard input, which would be sent to the apple or hplaser
118  printer.
119
120  $ pykotme
121 
122  This will give a quote for a job consisting of what is on standard
123  input. The quote will list the job size, and the price the job
124  would cost on each printer.
125
126
127This program is free software; you can redistribute it and/or modify
128it under the terms of the GNU General Public License as published by
129the Free Software Foundation; either version 2 of the License, or
130(at your option) any later version.
131
132This program is distributed in the hope that it will be useful,
133but WITHOUT ANY WARRANTY; without even the implied warranty of
134MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
135GNU General Public License for more details.
136
137You should have received a copy of the GNU General Public License
138along with this program; if not, write to the Free Software
139Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
140
141Please e-mail bugs to: %s""" % (version.__version__, version.__author__)
142       
143       
144class PyKotMe(PyKotaTool) :       
145    """A class for pykotme."""
146    def main(self, files, options) :
147        """Gives print quotes."""
148        if (not sys.stdin.isatty()) and ("-" not in files) :
149            files.append("-")
150        totalsize = 0   
151        for filename in files :   
152            try :
153                parser = PDLAnalyzer(filename)
154                totalsize += parser.getJobSize()
155            except PDLAnalyzerError, msg :   
156                self.printInfo(msg)
157           
158        # get current user
159        uid = os.geteuid()
160        username = pwd.getpwuid(uid)[0]
161        user = self.storage.getUser(username)
162        if user.Exists and user.LimitBy and (user.LimitBy.lower() == "balance"):
163            print _("Your account balance : %.2f") % (user.AccountBalance or 0.0)
164           
165        printers = self.storage.getMatchingPrinters(options["printer"])
166        if not printers :
167            raise PyKotaToolError, _("There's no printer matching %s") % options["printer"]
168           
169        print _("Job size : %i pages") % totalsize   
170        for printer in printers :
171            userpquota = self.storage.getUserPQuota(user, printer)
172            cost = userpquota.computeJobPrice(totalsize)
173            print _("Cost on printer %s : %.2f") % (printer.Name, cost)
174           
175if __name__ == "__main__" : 
176    retcode = 0
177    try :
178        defaults = { \
179                     "printer" : "*", \
180                   }
181        short_options = "vhP:"
182        long_options = ["help", "version", "printer="]
183       
184        # Initializes the command line tool
185        sender = PyKotMe(doc=__doc__)
186       
187        # parse and checks the command line
188        (options, args) = sender.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=1)
189       
190        # sets long options
191        options["help"] = options["h"] or options["help"]
192        options["version"] = options["v"] or options["version"]
193        options["printer"] = options["P"] or options["printer"] or defaults["printer"]
194       
195        if options["help"] :
196            sender.display_usage_and_quit()
197        elif options["version"] :
198            sender.display_version_and_quit()
199        else :
200            retcode = sender.main(args, options)
201    except SystemExit :       
202        pass
203    except :
204        try :
205            sender.crashed("pykotme failed")
206        except :   
207            crashed("pykotme failed")
208        retcode = -1
209
210    try :
211        sender.storage.close()
212    except (TypeError, NameError, AttributeError) :   
213        pass
214       
215    sys.exit(retcode)   
Note: See TracBrowser for help on using the browser.