[1048] | 1 | #! /usr/bin/env python |
---|
| 2 | |
---|
| 3 | # PyKota - Print Quotas for CUPS and LPRng |
---|
| 4 | # |
---|
| 5 | # (c) 2003 Jerome Alet <alet@librelogiciel.com> |
---|
| 6 | # This program is free software; you can redistribute it and/or modify |
---|
| 7 | # it under the terms of the GNU General Public License as published by |
---|
| 8 | # the Free Software Foundation; either version 2 of the License, or |
---|
| 9 | # (at your option) any later version. |
---|
| 10 | # |
---|
| 11 | # This program is distributed in the hope that it will be useful, |
---|
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 14 | # GNU General Public License for more details. |
---|
| 15 | # |
---|
| 16 | # You should have received a copy of the GNU General Public License |
---|
| 17 | # along with this program; if not, write to the Free Software |
---|
| 18 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
| 19 | # |
---|
| 20 | # $Id$ |
---|
| 21 | # |
---|
| 22 | # $Log$ |
---|
| 23 | # Revision 1.1 2003/06/30 12:46:15 jalet |
---|
| 24 | # Extracted reporting code. |
---|
| 25 | # |
---|
| 26 | # |
---|
| 27 | # |
---|
| 28 | |
---|
| 29 | import sys |
---|
| 30 | |
---|
| 31 | from mx import DateTime |
---|
| 32 | |
---|
| 33 | from pykota.reporter import BaseReporter, PyKotaReporterError |
---|
| 34 | |
---|
| 35 | class Reporter(BaseReporter) : |
---|
| 36 | """Base class for all PyKota command line tools.""" |
---|
| 37 | def generateReport(self) : |
---|
| 38 | """Produces a simple text report.""" |
---|
| 39 | self.report = [] |
---|
| 40 | for printer in self.printers : |
---|
| 41 | self.report.append(_("*** Report for %s quota on printer %s") % ((self.isgroup and "group") or "user", printer.Name)) |
---|
| 42 | self.report.append(_("Pages grace time: %i days") % self.tool.config.getGraceDelay(printer.Name)) |
---|
| 43 | if printer.PricePerJob is not None : |
---|
| 44 | self.report.append(_("Price per job: %.3f") % printer.PricePerJob) |
---|
| 45 | if printer.PricePerPage is not None : |
---|
| 46 | self.report.append(_("Price per page: %.3f") % printer.PricePerPage) |
---|
| 47 | total = 0 |
---|
| 48 | totalmoney = 0.0 |
---|
| 49 | if self.isgroup : |
---|
| 50 | self.report.append(_("Group used soft hard balance grace total paid")) |
---|
| 51 | self.report.append("------------------------------------------------------------------------------") |
---|
| 52 | for (group, grouppquota) in self.tool.storage.getPrinterGroupsAndQuotas(printer, self.ugnames) : |
---|
| 53 | (pages, money) = self.printQuota(group, grouppquota) |
---|
| 54 | total += pages |
---|
| 55 | totalmoney += money |
---|
| 56 | else : |
---|
| 57 | # default is user quota report |
---|
| 58 | self.report.append(_("User used soft hard balance grace total paid")) |
---|
| 59 | self.report.append("------------------------------------------------------------------------------") |
---|
| 60 | for (user, userpquota) in self.tool.storage.getPrinterUsersAndQuotas(printer, self.ugnames) : |
---|
| 61 | (pages, money) = self.printQuota(user, userpquota) |
---|
| 62 | total += pages |
---|
| 63 | totalmoney += money |
---|
| 64 | if total or totalmoney : |
---|
| 65 | self.report.append((" " * 50) + (_("Total : %9i") % total) + ("%11s" % ("%7.2f" % totalmoney)[:11])) |
---|
| 66 | try : |
---|
| 67 | msg = "%9i" % printer.LastJob.PrinterPageCounter |
---|
| 68 | except TypeError : |
---|
| 69 | msg = _("unknown") |
---|
| 70 | self.report.append((" " * 51) + (_("Real : %s") % msg)) |
---|
| 71 | self.report.append("") |
---|
| 72 | if self.isgroup : |
---|
| 73 | self.report.append(_("Totals may be inaccurate if some users are members of several groups.")) |
---|
| 74 | return "\n".join(self.report) |
---|
| 75 | |
---|
| 76 | def printQuota(self, entry, quota) : |
---|
| 77 | """Prints the quota information.""" |
---|
| 78 | lifepagecounter = int(quota.LifePageCounter or 0) |
---|
| 79 | pagecounter = int(quota.PageCounter or 0) |
---|
| 80 | balance = float(entry.AccountBalance or 0.0) |
---|
| 81 | lifetimepaid = float(entry.LifeTimePaid or 0.0) |
---|
| 82 | |
---|
| 83 | if quota.DateLimit is not None : |
---|
| 84 | now = DateTime.now() |
---|
| 85 | datelimit = DateTime.ISO.ParseDateTime(quota.DateLimit) |
---|
| 86 | if now >= datelimit : |
---|
| 87 | datelimit = "DENY" |
---|
| 88 | elif (quota.HardLimit is not None) and (pagecounter >= quota.HardLimit) : |
---|
| 89 | datelimit = "DENY" |
---|
| 90 | elif (quota.HardLimit is None) and (quota.SoftLimit is not None) and (pagecounter >= quota.SoftLimit) : |
---|
| 91 | datelimit = "DENY" |
---|
| 92 | else : |
---|
| 93 | datelimit = "" |
---|
| 94 | |
---|
| 95 | if entry.LimitBy.lower() == "balance" : |
---|
| 96 | reached = (((balance <= 0) and "+") or "-") + "B" |
---|
| 97 | else : |
---|
| 98 | reached = (((quota.SoftLimit is not None) and (pagecounter >= quota.SoftLimit) and "+") or "-") + "Q" |
---|
| 99 | |
---|
| 100 | strbalance = ("%5.2f" % balance)[:10] |
---|
| 101 | strlifetimepaid = ("%6.2f" % lifetimepaid)[:10] |
---|
| 102 | self.report.append("%-9.9s %s %7i %7s %7s %10s %-10.10s %8i %10s" % (entry.Name, reached, pagecounter, str(quota.SoftLimit), str(quota.HardLimit), strbalance, str(datelimit)[:10], lifepagecounter, strlifetimepaid)) |
---|
| 103 | return (lifepagecounter, lifetimepaid) |
---|
| 104 | |
---|