root / pykota / trunk / bin / repykota @ 895

Revision 895, 8.8 kB (checked in by jalet, 21 years ago)

(anything or 0) = anything !!! Go back to school Jerome !

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