root / pykota / trunk / bin / repykota @ 772

Revision 772, 6.9 kB (checked in by jalet, 21 years ago)

Internationalization begins...

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