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