root / pykota / trunk / bin / repykota @ 946

Revision 946, 10.8 kB (checked in by jalet, 21 years ago)

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