root / pykota / trunk / bin / pykotme @ 1889

Revision 1803, 7.5 kB (checked in by jalet, 20 years ago)

Postponed string interpolation to help message's output method

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