root / pykota / trunk / bin / repykota @ 1017

Revision 1017, 11.9 kB (checked in by jalet, 21 years ago)

New LDAP schema.
Small bug fixes.

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