root / pykota / trunk / bin / repykota @ 813

Revision 813, 7.7 kB (checked in by jalet, 22 years ago)

Check to see if the printer was ever used at all, and displays "unknown"
as the pagecounter value in this casCheck to see if the printer was ever used at all, and displays "unknown"
as the pagecounter value in this case.

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