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