[1048] | 1 | # PyKota |
---|
[1144] | 2 | # -*- coding: ISO-8859-15 -*- |
---|
[1048] | 3 | # |
---|
| 4 | # PyKota : Print Quotas for CUPS and LPRng |
---|
| 5 | # |
---|
[3133] | 6 | # (c) 2003, 2004, 2005, 2006, 2007 Jerome Alet <alet@librelogiciel.com> |
---|
[1048] | 7 | # This program is free software; you can redistribute it and/or modify |
---|
| 8 | # it under the terms of the GNU General Public License as published by |
---|
| 9 | # the Free Software Foundation; either version 2 of the License, or |
---|
| 10 | # (at your option) any later version. |
---|
| 11 | # |
---|
| 12 | # This program is distributed in the hope that it will be useful, |
---|
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 15 | # GNU General Public License for more details. |
---|
| 16 | # |
---|
| 17 | # You should have received a copy of the GNU General Public License |
---|
| 18 | # along with this program; if not, write to the Free Software |
---|
[2302] | 19 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
[1048] | 20 | # |
---|
| 21 | # $Id$ |
---|
| 22 | # |
---|
[2064] | 23 | # |
---|
[1048] | 24 | |
---|
[3184] | 25 | """This module defines bases classes used by all reporters.""" |
---|
| 26 | |
---|
[2945] | 27 | import os |
---|
| 28 | import imp |
---|
[1240] | 29 | from mx import DateTime |
---|
| 30 | |
---|
[1048] | 31 | class PyKotaReporterError(Exception): |
---|
| 32 | """An exception for Reporter related stuff.""" |
---|
| 33 | def __init__(self, message = ""): |
---|
| 34 | self.message = message |
---|
| 35 | Exception.__init__(self, message) |
---|
| 36 | def __repr__(self): |
---|
| 37 | return self.message |
---|
| 38 | __str__ = __repr__ |
---|
| 39 | |
---|
| 40 | class BaseReporter : |
---|
| 41 | """Base class for all reports.""" |
---|
| 42 | def __init__(self, tool, printers, ugnames, isgroup) : |
---|
| 43 | """Initialize local datas.""" |
---|
| 44 | self.tool = tool |
---|
| 45 | self.printers = printers |
---|
| 46 | self.ugnames = ugnames |
---|
| 47 | self.isgroup = isgroup |
---|
| 48 | |
---|
[1235] | 49 | def getPrinterTitle(self, printer) : |
---|
[3184] | 50 | """Returns the formatted title for a given printer.""" |
---|
[1582] | 51 | return (_("Report for %s quota on printer %s") % ((self.isgroup and "group") or "user", printer.Name)) + (" (%s)" % printer.Description) |
---|
[1235] | 52 | |
---|
| 53 | def getPrinterGraceDelay(self, printer) : |
---|
[3184] | 54 | """Returns the formatted grace delay for a given printer.""" |
---|
[1235] | 55 | return _("Pages grace time: %i days") % self.tool.config.getGraceDelay(printer.Name) |
---|
| 56 | |
---|
| 57 | def getPrinterPrices(self, printer) : |
---|
[3184] | 58 | """Returns the formatted prices for a given printer.""" |
---|
[1235] | 59 | return (_("Price per job: %.3f") % (printer.PricePerJob or 0.0), _("Price per page: %.3f") % (printer.PricePerPage or 0.0)) |
---|
| 60 | |
---|
| 61 | def getReportHeader(self) : |
---|
[3184] | 62 | """Returns the correct header depending on users vs users groups.""" |
---|
[1235] | 63 | if self.isgroup : |
---|
[2054] | 64 | return _("Group overcharge used soft hard balance grace total paid warn") |
---|
[1235] | 65 | else : |
---|
[2054] | 66 | return _("User overcharge used soft hard balance grace total paid warn") |
---|
[1235] | 67 | |
---|
| 68 | def getPrinterRealPageCounter(self, printer) : |
---|
[3184] | 69 | """Returns the formatted real page counter for a given printer.""" |
---|
[1692] | 70 | msg = _("unknown") |
---|
| 71 | if printer.LastJob.Exists : |
---|
| 72 | try : |
---|
| 73 | msg = "%9i" % printer.LastJob.PrinterPageCounter |
---|
| 74 | except TypeError : |
---|
| 75 | pass |
---|
[1235] | 76 | return _("Real : %s") % msg |
---|
| 77 | |
---|
| 78 | def getTotals(self, total, totalmoney) : |
---|
[3184] | 79 | """Returns the formatted totals.""" |
---|
[1235] | 80 | return (_("Total : %9i") % (total or 0.0), ("%11s" % ("%7.2f" % (totalmoney or 0.0))[:11])) |
---|
| 81 | |
---|
| 82 | def getQuota(self, entry, quota) : |
---|
| 83 | """Prints the quota information.""" |
---|
| 84 | lifepagecounter = int(quota.LifePageCounter or 0) |
---|
| 85 | pagecounter = int(quota.PageCounter or 0) |
---|
| 86 | balance = float(entry.AccountBalance or 0.0) |
---|
| 87 | lifetimepaid = float(entry.LifeTimePaid or 0.0) |
---|
[2054] | 88 | if not hasattr(entry, "OverCharge") : |
---|
| 89 | overcharge = _("N/A") # Not available for groups |
---|
| 90 | else : |
---|
| 91 | overcharge = float(entry.OverCharge or 0.0) |
---|
| 92 | if not hasattr(quota, "WarnCount") : |
---|
| 93 | warncount = _("N/A") # Not available for groups |
---|
| 94 | else : |
---|
| 95 | warncount = int(quota.WarnCount or 0) |
---|
[1582] | 96 | |
---|
[2452] | 97 | if (not entry.LimitBy) or (entry.LimitBy.lower() == "quota") : |
---|
| 98 | if (quota.HardLimit is not None) and (pagecounter >= quota.HardLimit) : |
---|
[1235] | 99 | datelimit = "DENY" |
---|
[1418] | 100 | elif (quota.HardLimit is None) and (quota.SoftLimit is not None) and (pagecounter >= quota.SoftLimit) : |
---|
| 101 | datelimit = "DENY" |
---|
| 102 | elif quota.DateLimit is not None : |
---|
| 103 | now = DateTime.now() |
---|
[3050] | 104 | datelimit = DateTime.ISO.ParseDateTime(str(quota.DateLimit)[:19]) |
---|
[1418] | 105 | if now >= datelimit : |
---|
| 106 | datelimit = "DENY" |
---|
[2452] | 107 | else : |
---|
[1418] | 108 | datelimit = "" |
---|
| 109 | reached = (((quota.SoftLimit is not None) and (pagecounter >= quota.SoftLimit) and "+") or "-") + "Q" |
---|
[1235] | 110 | else : |
---|
[2452] | 111 | if entry.LimitBy.lower() == "balance" : |
---|
[2692] | 112 | balancezero = self.tool.config.getBalanceZero() |
---|
| 113 | if balance == balancezero : |
---|
[2452] | 114 | if entry.OverCharge > 0 : |
---|
| 115 | datelimit = "DENY" |
---|
| 116 | reached = "+B" |
---|
| 117 | else : |
---|
| 118 | # overcharging by a negative or nul factor means user is always allowed to print |
---|
| 119 | # TODO : do something when printer prices are negative as well ! |
---|
| 120 | datelimit = "" |
---|
| 121 | reached = "-B" |
---|
[2692] | 122 | elif balance < balancezero : |
---|
[1235] | 123 | datelimit = "DENY" |
---|
[2452] | 124 | reached = "+B" |
---|
| 125 | elif balance <= self.tool.config.getPoorMan() : |
---|
| 126 | datelimit = "WARNING" |
---|
| 127 | reached = "?B" |
---|
| 128 | else : |
---|
| 129 | datelimit = "" |
---|
| 130 | reached = "-B" |
---|
| 131 | elif entry.LimitBy.lower() == "noquota" : |
---|
| 132 | reached = "NQ" |
---|
[1235] | 133 | datelimit = "" |
---|
[2452] | 134 | elif entry.LimitBy.lower() == "nochange" : |
---|
| 135 | reached = "NC" |
---|
| 136 | datelimit = "" |
---|
| 137 | else : |
---|
| 138 | # noprint |
---|
| 139 | reached = "NP" |
---|
| 140 | datelimit = "DENY" |
---|
[1235] | 141 | |
---|
| 142 | strbalance = ("%5.2f" % balance)[:10] |
---|
| 143 | strlifetimepaid = ("%6.2f" % lifetimepaid)[:10] |
---|
[2054] | 144 | strovercharge = ("%5s" % overcharge)[:5] |
---|
| 145 | strwarncount = ("%4s" % warncount)[:4] |
---|
| 146 | return (lifepagecounter, lifetimepaid, entry.Name, reached, \ |
---|
| 147 | pagecounter, str(quota.SoftLimit), str(quota.HardLimit), \ |
---|
| 148 | strbalance, str(datelimit)[:10], lifepagecounter, \ |
---|
| 149 | strlifetimepaid, strovercharge, strwarncount) |
---|
[1235] | 150 | |
---|
[1048] | 151 | def openReporter(tool, reporttype, printers, ugnames, isgroup) : |
---|
| 152 | """Returns a reporter instance of the proper reporter.""" |
---|
| 153 | try : |
---|
[2945] | 154 | reporterbackend = imp.load_source("reporterbackend", |
---|
| 155 | os.path.join(os.path.dirname(__file__), |
---|
| 156 | "reporters", |
---|
| 157 | "%s.py" % reporttype.lower())) |
---|
[1048] | 158 | except ImportError : |
---|
| 159 | raise PyKotaReporterError, _("Unsupported reporter backend %s") % reporttype |
---|
| 160 | else : |
---|
[1240] | 161 | return reporterbackend.Reporter(tool, printers, ugnames, isgroup) |
---|