root / pykota / trunk / bin / repykota @ 732

Revision 732, 6.1 kB (checked in by jalet, 21 years ago)

Typos

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