1 | # PyKota |
---|
2 | # -*- coding: ISO-8859-15 -*- |
---|
3 | # |
---|
4 | # PyKota : Print Quotas for CUPS and LPRng |
---|
5 | # |
---|
6 | # (c) 2003, 2004, 2005, 2006, 2007 Jerome Alet <alet@librelogiciel.com> |
---|
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 |
---|
19 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
20 | # |
---|
21 | # $Id$ |
---|
22 | # |
---|
23 | # |
---|
24 | |
---|
25 | """This module defines bases classes used by all reporters.""" |
---|
26 | |
---|
27 | import os |
---|
28 | import imp |
---|
29 | from mx import DateTime |
---|
30 | |
---|
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 | |
---|
49 | def getPrinterTitle(self, printer) : |
---|
50 | """Returns the formatted title for a given printer.""" |
---|
51 | return (_("Report for %s quota on printer %s") % ((self.isgroup and "group") or "user", printer.Name)) + (" (%s)" % printer.Description) |
---|
52 | |
---|
53 | def getPrinterGraceDelay(self, printer) : |
---|
54 | """Returns the formatted grace delay for a given printer.""" |
---|
55 | return _("Pages grace time: %i days") % self.tool.config.getGraceDelay(printer.Name) |
---|
56 | |
---|
57 | def getPrinterPrices(self, printer) : |
---|
58 | """Returns the formatted prices for a given printer.""" |
---|
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) : |
---|
62 | """Returns the correct header depending on users vs users groups.""" |
---|
63 | if self.isgroup : |
---|
64 | return _("Group overcharge used soft hard balance grace total paid warn") |
---|
65 | else : |
---|
66 | return _("User overcharge used soft hard balance grace total paid warn") |
---|
67 | |
---|
68 | def getPrinterRealPageCounter(self, printer) : |
---|
69 | """Returns the formatted real page counter for a given printer.""" |
---|
70 | msg = _("unknown") |
---|
71 | if printer.LastJob.Exists : |
---|
72 | try : |
---|
73 | msg = "%9i" % printer.LastJob.PrinterPageCounter |
---|
74 | except TypeError : |
---|
75 | pass |
---|
76 | return _("Real : %s") % msg |
---|
77 | |
---|
78 | def getTotals(self, total, totalmoney) : |
---|
79 | """Returns the formatted totals.""" |
---|
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) |
---|
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) |
---|
96 | |
---|
97 | if (not entry.LimitBy) or (entry.LimitBy.lower() == "quota") : |
---|
98 | if (quota.HardLimit is not None) and (pagecounter >= quota.HardLimit) : |
---|
99 | datelimit = "DENY" |
---|
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() |
---|
104 | datelimit = DateTime.ISO.ParseDateTime(str(quota.DateLimit)[:19]) |
---|
105 | if now >= datelimit : |
---|
106 | datelimit = "DENY" |
---|
107 | else : |
---|
108 | datelimit = "" |
---|
109 | reached = (((quota.SoftLimit is not None) and (pagecounter >= quota.SoftLimit) and "+") or "-") + "Q" |
---|
110 | else : |
---|
111 | if entry.LimitBy.lower() == "balance" : |
---|
112 | balancezero = self.tool.config.getBalanceZero() |
---|
113 | if balance == balancezero : |
---|
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" |
---|
122 | elif balance < balancezero : |
---|
123 | datelimit = "DENY" |
---|
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" |
---|
133 | datelimit = "" |
---|
134 | elif entry.LimitBy.lower() == "nochange" : |
---|
135 | reached = "NC" |
---|
136 | datelimit = "" |
---|
137 | else : |
---|
138 | # noprint |
---|
139 | reached = "NP" |
---|
140 | datelimit = "DENY" |
---|
141 | |
---|
142 | strbalance = ("%5.2f" % balance)[:10] |
---|
143 | strlifetimepaid = ("%6.2f" % lifetimepaid)[:10] |
---|
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) |
---|
150 | |
---|
151 | def openReporter(tool, reporttype, printers, ugnames, isgroup) : |
---|
152 | """Returns a reporter instance of the proper reporter.""" |
---|
153 | try : |
---|
154 | reporterbackend = imp.load_source("reporterbackend", |
---|
155 | os.path.join(os.path.dirname(__file__), |
---|
156 | "reporters", |
---|
157 | "%s.py" % reporttype.lower())) |
---|
158 | except ImportError : |
---|
159 | raise PyKotaReporterError, _("Unsupported reporter backend %s") % reporttype |
---|
160 | else : |
---|
161 | return reporterbackend.Reporter(tool, printers, ugnames, isgroup) |
---|