root / pykota / trunk / bin / repykota @ 952

Revision 952, 11.5 kB (checked in by jalet, 21 years ago)

Preliminary support for LPRng added BUT STILL UNTESTED.

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