root / pykota / trunk / bin / repykota @ 908

Revision 905, 9.0 kB (checked in by jalet, 22 years ago)

Translations

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