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