root / pykota / trunk / bin / pykotme @ 1526

Revision 1526, 7.1 kB (checked in by jalet, 20 years ago)

Fixed over-verbose exits when displaying help or version number

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