root / pykota / trunk / bin / repykota @ 914

Revision 912, 9.1 kB (checked in by jalet, 22 years ago)

Reversed test.

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