root / pykota / trunk / bin / repykota @ 890

Revision 890, 8.7 kB (checked in by jalet, 21 years ago)

Checks --groups option presence instead of --users because --users is the default.

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