root / pykota / trunk / bin / repykota @ 949

Revision 949, 10.9 kB (checked in by jalet, 21 years ago)

Minor fix.

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