root / pykota / trunk / bin / repykota @ 912

Revision 912, 9.1 kB (checked in by jalet, 21 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
RevLine 
[696]1#! /usr/bin/env python
2
[731]3# PyKota Print Quota Reports generator
[696]4#
5# PyKota - Print Quotas for CUPS
6#
7# (c) 2003 Jerome Alet <alet@librelogiciel.com>
[873]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.
[696]12#
[873]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.
[696]21#
22# $Id$
23#
24# $Log$
[912]25# Revision 1.24  2003/04/14 20:05:20  jalet
26# Reversed test.
27#
[905]28# Revision 1.23  2003/04/11 14:42:54  jalet
29# Translations
[904]30#
[900]31# Revision 1.21  2003/04/10 21:47:20  jalet
32# Job history added. Upgrade script neutralized for now !
33#
[895]34# Revision 1.20  2003/04/08 21:31:39  jalet
35# (anything or 0) = anything !!! Go back to school Jerome !
36#
[891]37# Revision 1.19  2003/04/08 21:13:44  jalet
38# Prepare --groups option to work.
39#
[890]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#
[873]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#
[843]48# Revision 1.16  2003/03/09 23:56:21  jalet
49# Option noquota added to do accounting only.
50#
[842]51# Revision 1.15  2003/03/09 23:39:14  jalet
52# Simplified translations.
53#
[814]54# Revision 1.14  2003/02/27 09:04:02  jalet
55# Missing translation
56#
[813]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#
[805]62# Revision 1.12  2003/02/17 23:02:23  jalet
63# getGraceDelay for printer
64#
[793]65# Revision 1.11  2003/02/10 12:12:34  jalet
66# Translations.
67#
[791]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#
[775]71# Revision 1.9  2003/02/09 13:40:29  jalet
72# typo
73#
[772]74# Revision 1.8  2003/02/09 12:56:53  jalet
75# Internationalization begins...
76#
[770]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#
[752]81# Revision 1.6  2003/02/07 23:39:16  jalet
82# Typos
83#
[734]84# Revision 1.5  2003/02/07 08:38:36  jalet
85# Missing conversion.
86# empty line between two printers
87#
[733]88# Revision 1.4  2003/02/07 08:34:15  jalet
89# Test wrt date limit was wrong
90#
[732]91# Revision 1.3  2003/02/07 00:08:52  jalet
92# Typos
93#
[731]94# Revision 1.2  2003/02/06 23:58:05  jalet
95# repykota should be ok
[696]96#
97#
98#
99
[731]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 
[752]134  This will print the quota status for all users who use the lp printer.
[731]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."""
[900]160        printers = self.storage.getMatchingPrinters(options["printer"])
161        if not printers :
[772]162            raise PyKotaToolError, _("There's no printer matching %s") % options["printer"]
[900]163        for (printerid, printer) in printers :
[912]164            print _("*** Report for %s quota on printer %s") % ((options["groups"] and "group") or "user", printer)
[805]165            print _("Pages grace time: %idays") % self.config.getGraceDelay(printer)
[770]166            total = 0
[890]167            if options["groups"] :
168                print _("Group            used     soft     hard   grace        total")
169                print "------------------------------------------------------------"
[900]170                for name in self.storage.getPrinterGroups(printerid) :
171                    quota = self.storage.getGroupPQuota(ident, printerid) 
[890]172                    total += self.printQuota(name, quota)
173            else :
174                # default is user quota report
[772]175                print _("User             used     soft     hard   grace        total")
[770]176                print "------------------------------------------------------------"
[900]177                for (ident, name) in self.storage.getPrinterUsers(printerid) :
178                    quota = self.storage.getUserPQuota(ident, printerid)
[770]179                    total += self.printQuota(name, quota)
180            if total :       
[793]181                print (" " * 43) + (_("Total : %9i") % total)
[900]182            printerpagecounter = self.storage.getPrinterPageCounter(printerid)
[813]183            if printerpagecounter is None :   
[814]184                msg = _("unknown")
[813]185            else :
[900]186                msg = "%9i" % printerpagecounter["pagecounter"]
[813]187            print (" " * 44) + (_("Real : %s") % msg)
[734]188            print       
[731]189                       
190    def printQuota(self, name, quota) :
191        """Prints the quota information."""
192        if quota is not None :
[770]193            lifepagecounter = quota["lifepagecounter"]
[731]194            pagecounter = quota["pagecounter"]
[843]195            softlimit = quota["softlimit"]
196            hardlimit = quota["hardlimit"]
[731]197            datelimit = quota["datelimit"]
198            if datelimit is not None :
199                now = DateTime.now()
200                datelimit = DateTime.ISO.ParseDateTime(datelimit)
[733]201                if now >= datelimit :
[731]202                    datelimit = "DENY"
203            else :   
204                datelimit = ""
[843]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)
[770]207            return lifepagecounter
[905]208       
[731]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"]
[895]227        options["users"] = options["u"] or options["users"]
228        options["groups"] = options["g"] or options["groups"]
[731]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"] :   
[842]236            raise PyKotaToolError, _("incompatible options, see help.")
[731]237        elif options["groups"] :   
[842]238            raise PyKotaToolError, _("option --groups is currently not implemented.")
[731]239        elif args :   
[842]240            raise PyKotaToolError, _("unused arguments [%s]. Aborting.") % ", ".join(args)
[731]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.