#! /usr/bin/env python # PyKota Print Quota Reports generator # # PyKota - Print Quotas for CUPS # # (c) 2003 Jerome Alet # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. # # $Id$ # # $Log$ # Revision 1.31 2003/04/23 09:58:17 jalet # Prices per page/job are now displayed # # Revision 1.30 2003/04/18 08:34:51 jalet # Minor fix. # # Revision 1.29 2003/04/18 08:29:30 jalet # Minor bug fix # # Revision 1.28 2003/04/17 09:36:30 jalet # Bad alignment in output # # Revision 1.27 2003/04/17 09:26:21 jalet # repykota now reports account balances too. # # Revision 1.26 2003/04/16 12:35:49 jalet # Groups quota work now ! # # Revision 1.25 2003/04/15 11:30:57 jalet # More work done on money print charging. # Minor bugs corrected. # All tools now access to the storage as priviledged users, repykota excepted. # # Revision 1.24 2003/04/14 20:05:20 jalet # Reversed test. # # Revision 1.23 2003/04/11 14:42:54 jalet # Translations # # Revision 1.21 2003/04/10 21:47:20 jalet # Job history added. Upgrade script neutralized for now ! # # Revision 1.20 2003/04/08 21:31:39 jalet # (anything or 0) = anything !!! Go back to school Jerome ! # # Revision 1.19 2003/04/08 21:13:44 jalet # Prepare --groups option to work. # # Revision 1.18 2003/04/08 21:10:18 jalet # Checks --groups option presence instead of --users because --users is the default. # # Revision 1.17 2003/03/29 13:45:27 jalet # GPL paragraphs were incorrectly (from memory) copied into the sources. # Two README files were added. # Upgrade script for PostgreSQL pre 1.01 schema was added. # # Revision 1.16 2003/03/09 23:56:21 jalet # Option noquota added to do accounting only. # # Revision 1.15 2003/03/09 23:39:14 jalet # Simplified translations. # # Revision 1.14 2003/02/27 09:04:02 jalet # Missing translation # # Revision 1.13 2003/02/27 08:44:01 jalet # Check to see if the printer was ever used at all, and displays "unknown" # as the pagecounter value in this casCheck to see if the printer was ever used at all, and displays "unknown" # as the pagecounter value in this case. # # Revision 1.12 2003/02/17 23:02:23 jalet # getGraceDelay for printer # # Revision 1.11 2003/02/10 12:12:34 jalet # Translations. # # Revision 1.10 2003/02/10 12:07:30 jalet # Now repykota should output the recorded total page number for each printer too. # # Revision 1.9 2003/02/09 13:40:29 jalet # typo # # Revision 1.8 2003/02/09 12:56:53 jalet # Internationalization begins... # # Revision 1.7 2003/02/08 23:17:20 jalet # repykota now outputs life time page counters and the total pages printed by # all users/groups on each printer. # # Revision 1.6 2003/02/07 23:39:16 jalet # Typos # # Revision 1.5 2003/02/07 08:38:36 jalet # Missing conversion. # empty line between two printers # # Revision 1.4 2003/02/07 08:34:15 jalet # Test wrt date limit was wrong # # Revision 1.3 2003/02/07 00:08:52 jalet # Typos # # Revision 1.2 2003/02/06 23:58:05 jalet # repykota should be ok # # # import sys from mx import DateTime from pykota import version from pykota.tool import PyKotaTool, PyKotaToolError __doc__ = """repykota v%s (C) 2003 C@LL - Conseil Internet & Logiciels Libres Generates print quota reports. command line usage : repykota [options] options : -v | --version Prints repykota's version number then exits. -h | --help Prints this message then exits. -u | --users Generates a report on users quota, this is the default. -g | --groups Generates a report on group quota instead of users. -P | --printer p Report quotas on this printer only. Actually p can use wildcards characters to select only some printers. The default value is *, meaning all printers. examples : $ repykota --printer lp This will print the quota status for all users who use the lp printer. $ repykota This will print the quota status for all users on all printers. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. Please e-mail bugs to: %s""" % (version.__version__, version.__author__) class RePyKota(PyKotaTool) : """A class for repykota.""" def __init__(self, doc) : """Initializes the tool as a non-priviledged storage user.""" PyKotaTool.__init__(self, asadmin=0, doc=doc) def main(self, options) : """Print Quota reports generator.""" printers = self.storage.getMatchingPrinters(options["printer"]) if not printers : raise PyKotaToolError, _("There's no printer matching %s") % options["printer"] for (printerid, printer) in printers : print _("*** Report for %s quota on printer %s") % ((options["groups"] and "group") or "user", printer) print _("Pages grace time: %i days") % self.config.getGraceDelay(printer) prices = self.storage.getPrinterPrices(printerid) if prices is None : (perpage, perjob) = None else : (perpage, perjob) = prices if perjob is not None : print _("Price per job: %.3f") % perjob if perpage is not None : print _("Price per page: %.3f") % perpage total = 0 totalmoney = 0.0 if options["groups"] : print _("Group used soft hard balance grace total paid") print "------------------------------------------------------------------------------" for (ident, name) in self.storage.getPrinterGroups(printerid) : quota = self.storage.getGroupPQuota(ident, printerid) balance = self.storage.getGroupBalance(ident) limitby = self.storage.getGroupLimitBy(ident) (pages, money) = self.printQuota(name, quota, balance, limitby) total += pages totalmoney += money else : # default is user quota report print _("User used soft hard balance grace total paid") print "------------------------------------------------------------------------------" for (ident, name) in self.storage.getPrinterUsers(printerid) : quota = self.storage.getUserPQuota(ident, printerid) balance = self.storage.getUserBalance(ident) limitby = self.storage.getUserLimitBy(ident) (pages, money) = self.printQuota(name, quota, balance, limitby) total += pages totalmoney += money if total or totalmoney : print (" " * 50) + (_("Total : %9i") % total) + ("%11s" % ("%7.2f" % totalmoney)[:11]) printerpagecounter = self.storage.getPrinterPageCounter(printerid) try : msg = "%9i" % printerpagecounter["pagecounter"] except TypeError : msg = _("unknown") print (" " * 51) + (_("Real : %s") % msg) print if options["groups"] : print _("Totals may be inaccurate if some users are members of several groups.") def printQuota(self, name, quota, balance, limitby) : """Prints the quota information.""" if quota is not None : lifepagecounter = quota["lifepagecounter"] or 0 pagecounter = quota["pagecounter"] or 0 softlimit = quota["softlimit"] hardlimit = quota["hardlimit"] datelimit = quota["datelimit"] if balance is not None : (balance, lifetimepaid) = balance else : (balance, lifetimepaid) = 0.0 if datelimit is not None : now = DateTime.now() datelimit = DateTime.ISO.ParseDateTime(datelimit) if now >= datelimit : datelimit = "DENY" else : datelimit = "" if limitby == "balance" : reached = (((balance <= 0) and "+") or "-") + "B" else : reached = (((softlimit is not None) and (pagecounter >= softlimit) and "+") or "-") + "Q" balance = balance or 0.0 lifetimepaid = lifetimepaid or 0.0 strbalance = ("%5.2f" % balance)[:10] strlifetimepaid = ("%6.2f" % lifetimepaid)[:10] 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) return (lifepagecounter, lifetimepaid) if __name__ == "__main__" : try : defaults = { \ "printer" : "*", \ } short_options = "vhugP:" long_options = ["help", "version", "users", "groups", "printer="] # Initializes the command line tool reporter = RePyKota(doc=__doc__) # parse and checks the command line (options, args) = reporter.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=1) # sets long options options["help"] = options["h"] or options["help"] options["version"] = options["v"] or options["version"] options["users"] = options["u"] or options["users"] options["groups"] = options["g"] or options["groups"] options["printer"] = options["P"] or options["printer"] or defaults["printer"] if options["help"] : reporter.display_usage_and_quit() elif options["version"] : reporter.display_version_and_quit() elif options["users"] and options["groups"] : raise PyKotaToolError, _("incompatible options, see help.") elif args : raise PyKotaToolError, _("unused arguments [%s]. Aborting.") % ", ".join(args) else : sys.exit(reporter.main(options)) except PyKotaToolError, msg : sys.stderr.write("%s\n" % msg) sys.stderr.flush() sys.exit(-1)