root / pykota / trunk / bin / repykota @ 734

Revision 734, 6.3 kB (checked in by jalet, 21 years ago)

Missing conversion.
empty line between two printers

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