root / pykota / trunk / bin / pykotme @ 1517

Revision 1517, 6.9 kB (checked in by jalet, 20 years ago)

Improved error logging.
crashrecipient directive added.
Now exports the job's size in bytes too.

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