root / pykota / trunk / bin / pykotme @ 1796

Revision 1796, 7.4 kB (checked in by jalet, 20 years ago)

Renders help translatable

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