root / pykota / trunk / bin / repykota @ 805

Revision 805, 7.3 kB (checked in by jalet, 21 years ago)

getGraceDelay for printer

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