root / pykota / trunk / bin / repykota @ 1021

Revision 1021, 12.1 kB (checked in by jalet, 21 years ago)

Deletion of the second user which is not needed anymore.
Added a debug configuration field in /etc/pykota.conf
All queries can now be sent to the logger in debug mode, this will
greatly help improve performance when time for this will come.

  • 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
3# PyKota Print Quota Reports generator
4#
5# PyKota - Print Quotas for CUPS and LPRng
6#
7# (c) 2003 Jerome Alet <alet@librelogiciel.com>
8# This program is free software; you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation; either version 2 of the License, or
11# (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with this program; if not, write to the Free Software
20# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
21#
22# $Id$
23#
24# $Log$
25# Revision 1.37  2003/06/10 16:37:54  jalet
26# Deletion of the second user which is not needed anymore.
27# Added a debug configuration field in /etc/pykota.conf
28# All queries can now be sent to the logger in debug mode, this will
29# greatly help improve performance when time for this will come.
30#
31# Revision 1.36  2003/06/06 14:21:08  jalet
32# New LDAP schema.
33# Small bug fixes.
34#
35# Revision 1.35  2003/05/28 13:56:24  jalet
36# Unneeded code.
37#
38# Revision 1.34  2003/04/29 22:03:38  jalet
39# Better error handling.
40#
41# Revision 1.33  2003/04/28 08:25:48  jalet
42# Small display bug in repykota was fixed.
43#
44# Revision 1.32  2003/04/23 22:13:56  jalet
45# Preliminary support for LPRng added BUT STILL UNTESTED.
46#
47# Revision 1.31  2003/04/23 09:58:17  jalet
48# Prices per page/job are now displayed
49#
50# Revision 1.30  2003/04/18 08:34:51  jalet
51# Minor fix.
52#
53# Revision 1.29  2003/04/18 08:29:30  jalet
54# Minor bug fix
55#
56# Revision 1.28  2003/04/17 09:36:30  jalet
57# Bad alignment in output
58#
59# Revision 1.27  2003/04/17 09:26:21  jalet
60# repykota now reports account balances too.
61#
62# Revision 1.26  2003/04/16 12:35:49  jalet
63# Groups quota work now !
64#
65# Revision 1.25  2003/04/15 11:30:57  jalet
66# More work done on money print charging.
67# Minor bugs corrected.
68# All tools now access to the storage as priviledged users, repykota excepted.
69#
70# Revision 1.24  2003/04/14 20:05:20  jalet
71# Reversed test.
72#
73# Revision 1.23  2003/04/11 14:42:54  jalet
74# Translations
75#
76# Revision 1.21  2003/04/10 21:47:20  jalet
77# Job history added. Upgrade script neutralized for now !
78#
79# Revision 1.20  2003/04/08 21:31:39  jalet
80# (anything or 0) = anything !!! Go back to school Jerome !
81#
82# Revision 1.19  2003/04/08 21:13:44  jalet
83# Prepare --groups option to work.
84#
85# Revision 1.18  2003/04/08 21:10:18  jalet
86# Checks --groups option presence instead of --users because --users is the default.
87#
88# Revision 1.17  2003/03/29 13:45:27  jalet
89# GPL paragraphs were incorrectly (from memory) copied into the sources.
90# Two README files were added.
91# Upgrade script for PostgreSQL pre 1.01 schema was added.
92#
93# Revision 1.16  2003/03/09 23:56:21  jalet
94# Option noquota added to do accounting only.
95#
96# Revision 1.15  2003/03/09 23:39:14  jalet
97# Simplified translations.
98#
99# Revision 1.14  2003/02/27 09:04:02  jalet
100# Missing translation
101#
102# Revision 1.13  2003/02/27 08:44:01  jalet
103# Check to see if the printer was ever used at all, and displays "unknown"
104# as the pagecounter value in this casCheck to see if the printer was ever used at all, and displays "unknown"
105# as the pagecounter value in this case.
106#
107# Revision 1.12  2003/02/17 23:02:23  jalet
108# getGraceDelay for printer
109#
110# Revision 1.11  2003/02/10 12:12:34  jalet
111# Translations.
112#
113# Revision 1.10  2003/02/10 12:07:30  jalet
114# Now repykota should output the recorded total page number for each printer too.
115#
116# Revision 1.9  2003/02/09 13:40:29  jalet
117# typo
118#
119# Revision 1.8  2003/02/09 12:56:53  jalet
120# Internationalization begins...
121#
122# Revision 1.7  2003/02/08 23:17:20  jalet
123# repykota now outputs life time page counters and the total pages printed by
124# all users/groups on each printer.
125#
126# Revision 1.6  2003/02/07 23:39:16  jalet
127# Typos
128#
129# Revision 1.5  2003/02/07 08:38:36  jalet
130# Missing conversion.
131# empty line between two printers
132#
133# Revision 1.4  2003/02/07 08:34:15  jalet
134# Test wrt date limit was wrong
135#
136# Revision 1.3  2003/02/07 00:08:52  jalet
137# Typos
138#
139# Revision 1.2  2003/02/06 23:58:05  jalet
140# repykota should be ok
141#
142#
143#
144
145import sys
146
147from mx import DateTime
148
149from pykota import version
150from pykota.tool import PyKotaTool, PyKotaToolError
151from pykota.config import PyKotaConfigError
152from pykota.storage import PyKotaStorageError
153
154__doc__ = """repykota v%s (C) 2003 C@LL - Conseil Internet & Logiciels Libres
155
156Generates print quota reports.
157
158command line usage :
159
160  repykota [options]
161
162options :
163
164  -v | --version       Prints repykota's version number then exits.
165  -h | --help          Prints this message then exits.
166 
167  -u | --users         Generates a report on users quota, this is
168                       the default.
169 
170  -g | --groups        Generates a report on group quota instead of users.
171 
172  -P | --printer p     Report quotas on this printer only. Actually p can
173                       use wildcards characters to select only
174                       some printers. The default value is *, meaning
175                       all printers.
176 
177examples :                             
178
179  $ repykota --printer lp
180 
181  This will print the quota status for all users who use the lp printer.
182
183  $ repykota
184 
185  This will print the quota status for all users on all printers.
186
187This program is free software; you can redistribute it and/or modify
188it under the terms of the GNU General Public License as published by
189the Free Software Foundation; either version 2 of the License, or
190(at your option) any later version.
191
192This program is distributed in the hope that it will be useful,
193but WITHOUT ANY WARRANTY; without even the implied warranty of
194MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
195GNU General Public License for more details.
196
197You should have received a copy of the GNU General Public License
198along with this program; if not, write to the Free Software
199Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
200
201Please e-mail bugs to: %s""" % (version.__version__, version.__author__)
202       
203class RePyKota(PyKotaTool) :       
204    """A class for repykota."""
205    def main(self, options) :
206        """Print Quota reports generator."""
207        printers = self.storage.getMatchingPrinters(options["printer"])
208        if not printers :
209            raise PyKotaToolError, _("There's no printer matching %s") % options["printer"]
210        for (printerid, printer) in printers :
211            print _("*** Report for %s quota on printer %s") % ((options["groups"] and "group") or "user", printer)
212            print _("Pages grace time: %i days") % self.config.getGraceDelay(printer)
213            prices = self.storage.getPrinterPrices(printerid)
214            if prices is None :
215                (perpage, perjob) = (None, None)
216            else :   
217                (perpage, perjob) = prices
218            if perjob is not None :
219                print _("Price per job: %.3f") % perjob
220            if perpage is not None :   
221                print _("Price per page: %.3f") % perpage
222            total = 0
223            totalmoney = 0.0
224            if options["groups"] :
225                print _("Group           used    soft    hard    balance grace         total       paid") 
226                print "------------------------------------------------------------------------------"
227                for (ident, name) in self.storage.getPrinterGroups(printerid) :
228                    quota = self.storage.getGroupPQuota(ident, printerid) 
229                    balance = self.storage.getGroupBalance(ident)
230                    limitby = self.storage.getGroupLimitBy(ident)
231                    (pages, money) = self.printQuota(name, quota, balance, limitby)
232                    total += pages
233                    totalmoney += money
234            else :
235                # default is user quota report
236                print _("User            used    soft    hard    balance grace         total       paid") 
237                print "------------------------------------------------------------------------------"
238                for (ident, name) in self.storage.getPrinterUsers(printerid) :
239                    quota = self.storage.getUserPQuota(ident, printerid)
240                    balance = self.storage.getUserBalance(ident)
241                    limitby = self.storage.getUserLimitBy(ident)
242                    (pages, money) = self.printQuota(name, quota, balance, limitby)
243                    total += pages
244                    totalmoney += money
245            if total or totalmoney :       
246                print (" " * 50) + (_("Total : %9i") % total) + ("%11s" % ("%7.2f" % totalmoney)[:11])
247            printerpagecounter = self.storage.getPrinterPageCounter(printerid)
248            try :
249                msg = "%9i" % printerpagecounter["pagecounter"]
250            except TypeError :     
251                msg = _("unknown")
252            print (" " * 51) + (_("Real : %s") % msg)
253            print       
254        if options["groups"] :   
255            print _("Totals may be inaccurate if some users are members of several groups.")
256                       
257    def printQuota(self, name, quota, balance, limitby) :
258        """Prints the quota information."""
259        if quota is not None :
260            lifepagecounter = quota["lifepagecounter"] or 0
261            pagecounter = quota["pagecounter"] or 0
262            softlimit = quota["softlimit"]
263            hardlimit = quota["hardlimit"]
264            datelimit = quota["datelimit"]
265            if balance is not None :
266                (balance, lifetimepaid) = balance
267            else :   
268                (balance, lifetimepaid) = (0.0, 0.0)
269            if datelimit is not None :
270                now = DateTime.now()
271                datelimit = DateTime.ISO.ParseDateTime(datelimit)
272                if now >= datelimit :
273                    datelimit = "DENY"
274            else :   
275                datelimit = ""
276            if limitby == "balance" :   
277                reached = (((balance <= 0) and "+") or "-") + "B"
278            else :
279                reached = (((softlimit is not None) and (pagecounter >= softlimit) and "+") or "-") + "Q"
280            balance = balance or 0.0   
281            lifetimepaid = lifetimepaid or 0.0
282            strbalance = ("%5.2f" % balance)[:10]
283            strlifetimepaid = ("%6.2f" % lifetimepaid)[:10]
284            print "%-9.9s %s %7i %7s %7s %10s %-10.10s %8i %10s" % (name, reached, pagecounter, str(softlimit), str(hardlimit), strbalance, str(datelimit)[:10], lifepagecounter, strlifetimepaid)
285            return (lifepagecounter, lifetimepaid)
286        else :   
287            return (0, 0)
288       
289                   
290if __name__ == "__main__" : 
291    try :
292        defaults = { \
293                     "printer" : "*", \
294                   }
295        short_options = "vhugP:"
296        long_options = ["help", "version", "users", "groups", "printer="]
297       
298        # Initializes the command line tool
299        reporter = RePyKota(doc=__doc__)
300       
301        # parse and checks the command line
302        (options, args) = reporter.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=1)
303       
304        # sets long options
305        options["help"] = options["h"] or options["help"]
306        options["version"] = options["v"] or options["version"]
307        options["users"] = options["u"] or options["users"]
308        options["groups"] = options["g"] or options["groups"]
309        options["printer"] = options["P"] or options["printer"] or defaults["printer"]
310       
311        if options["help"] :
312            reporter.display_usage_and_quit()
313        elif options["version"] :
314            reporter.display_version_and_quit()
315        elif options["users"] and options["groups"] :   
316            raise PyKotaToolError, _("incompatible options, see help.")
317        elif args :   
318            raise PyKotaToolError, _("unused arguments [%s]. Aborting.") % ", ".join(args)
319        else :
320            sys.exit(reporter.main(options))
321    except (PyKotaToolError, PyKotaConfigError, PyKotaStorageError), msg :           
322        sys.stderr.write("%s\n" % msg)
323        sys.stderr.flush()
324        sys.exit(-1)
Note: See TracBrowser for help on using the browser.