root / pykota / trunk / bin / repykota @ 791

Revision 791, 7.2 kB (checked in by jalet, 21 years ago)

Now repykota should output the recorded total page number for each printer too.

  • 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# You're welcome to redistribute this software under the
9# terms of the GNU General Public Licence version 2.0
10# or, at your option, any higher version.
11#
12# You can read the complete GNU GPL in the file COPYING
13# which should come along with this software, or visit
14# the Free Software Foundation's WEB site http://www.fsf.org
15#
16# $Id$
17#
18# $Log$
19# Revision 1.10  2003/02/10 12:07:30  jalet
20# Now repykota should output the recorded total page number for each printer too.
21#
22# Revision 1.9  2003/02/09 13:40:29  jalet
23# typo
24#
25# Revision 1.8  2003/02/09 12:56:53  jalet
26# Internationalization begins...
27#
28# Revision 1.7  2003/02/08 23:17:20  jalet
29# repykota now outputs life time page counters and the total pages printed by
30# all users/groups on each printer.
31#
32# Revision 1.6  2003/02/07 23:39:16  jalet
33# Typos
34#
35# Revision 1.5  2003/02/07 08:38:36  jalet
36# Missing conversion.
37# empty line between two printers
38#
39# Revision 1.4  2003/02/07 08:34:15  jalet
40# Test wrt date limit was wrong
41#
42# Revision 1.3  2003/02/07 00:08:52  jalet
43# Typos
44#
45# Revision 1.2  2003/02/06 23:58:05  jalet
46# repykota should be ok
47#
48#
49#
50
51import sys
52
53from mx import DateTime
54
55from pykota import version
56from pykota.tool import PyKotaTool, PyKotaToolError
57
58__doc__ = """repykota v%s (C) 2003 C@LL - Conseil Internet & Logiciels Libres
59
60Generates print quota reports.
61
62command line usage :
63
64  repykota [options]
65
66options :
67
68  -v | --version       Prints repykota's version number then exits.
69  -h | --help          Prints this message then exits.
70 
71  -u | --users         Generates a report on users quota, this is
72                       the default.
73 
74  -g | --groups        Generates a report on group quota instead of users.
75 
76  -P | --printer p     Report quotas on this printer only. Actually p can
77                       use wildcards characters to select only
78                       some printers. The default value is *, meaning
79                       all printers.
80 
81examples :                             
82
83  $ repykota --printer lp
84 
85  This will print the quota status for all users who use the lp printer.
86
87  $ repykota
88 
89  This will print the quota status for all users on all printers.
90
91This program is free software; you can redistribute it and/or modify
92it under the terms of the GNU General Public License as published by
93the Free Software Foundation; either version 2 of the License, or
94(at your option) any later version.
95
96This program is distributed in the hope that it will be useful,
97but WITHOUT ANY WARRANTY; without even the implied warranty of
98MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
99GNU General Public License for more details.
100
101You should have received a copy of the GNU General Public License
102along with this program; if not, write to the Free Software
103Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
104
105Please e-mail bugs to: %s""" % (version.__version__, version.__author__)
106       
107class RePyKota(PyKotaTool) :       
108    """A class for repykota."""
109    def main(self, options) :
110        """Print Quota reports generator."""
111        printernames = self.storage.getMatchingPrinters(options["printer"])
112        if not printernames :
113            raise PyKotaToolError, _("There's no printer matching %s") % options["printer"]
114        for (printer, printerpagecounter) in printernames :
115            print _("*** Report for %s quota on printer %s") % ((options["users"] and "user") or "group", printer)
116            print _("Pages grace time: %idays") % self.config.getGraceDelay()
117            total = 0
118            if options["users"] :
119                print _("User             used     soft     hard   grace        total")
120                print "------------------------------------------------------------"
121                for name in self.storage.getPrinterUsers(printer) :
122                    quota = self.storage.getUserPQuota(name, printer)
123                    total += self.printQuota(name, quota)
124            else :
125                print _("Group            used     soft     hard   grace        total")
126                print "------------------------------------------------------------"
127                for name in self.storage.getPrinterGroups(printer) :
128                    quota = self.storage.getGroupPQuota(name, printer) 
129                    total += self.printQuota(name, quota)
130            if total :       
131                print (" " * 43) + ("Total : %9i" % total)
132            print (" " * 44) + ("Real : %9i" % printerpagecounter)
133            print       
134                       
135    def printQuota(self, name, quota) :
136        """Prints the quota information."""
137        if quota is not None :
138            lifepagecounter = quota["lifepagecounter"]
139            pagecounter = quota["pagecounter"]
140            softlimit = quota["softlimit"] or 0
141            hardlimit = quota["hardlimit"] or 0
142            datelimit = quota["datelimit"]
143            if datelimit is not None :
144                now = DateTime.now()
145                datelimit = DateTime.ISO.ParseDateTime(datelimit)
146                if now >= datelimit :
147                    datelimit = "DENY"
148            else :   
149                datelimit = ""
150            reached = ((pagecounter >= softlimit) and "+") or "-"
151            print "%-10.10s %c %8i %8i %8i %10s %9i" % (name, reached, pagecounter, softlimit, hardlimit, str(datelimit)[:10], lifepagecounter)
152            return lifepagecounter
153       
154                   
155if __name__ == "__main__" : 
156    try :
157        defaults = { \
158                     "users"  : 1, \
159                     "groups" : 0, \
160                     "printer" : "*", \
161                   }
162        short_options = "vhugP:"
163        long_options = ["help", "version", "users", "groups", "printer="]
164       
165        # Initializes the command line tool
166        reporter = RePyKota(doc=__doc__)
167       
168        # parse and checks the command line
169        (options, args) = reporter.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=1)
170       
171        # sets long options
172        options["help"] = options["h"] or options["help"]
173        options["version"] = options["v"] or options["version"]
174        options["users"] = options["u"] or options["users"] or defaults["users"]
175        options["groups"] = options["g"] or options["groups"] or defaults["groups"]
176        options["printer"] = options["P"] or options["printer"] or defaults["printer"]
177       
178        if options["help"] :
179            reporter.display_usage_and_quit()
180        elif options["version"] :
181            reporter.display_version_and_quit()
182        elif options["users"] and options["groups"] :   
183            raise PyKotaToolError, _("repykota: options --users and --groups are incompatible.")
184        elif options["groups"] :   
185            raise PyKotaToolError, _("repykota: option --groups is currently not implemented.")
186        elif args :   
187            raise PyKotaToolError, _("repykota: unused arguments [%s]. Aborting.") % ", ".join(args)
188        else :
189            sys.exit(reporter.main(options))
190    except PyKotaToolError, msg :           
191        sys.stderr.write("%s\n" % msg)
192        sys.stderr.flush()
193        sys.exit(-1)
Note: See TracBrowser for help on using the browser.