1 | # -*- coding: utf-8 -*-*- |
---|
2 | # |
---|
3 | # PyKota : Print Quotas for CUPS |
---|
4 | # |
---|
5 | # (c) 2003, 2004, 2005, 2006, 2007, 2008 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 3 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, see <http://www.gnu.org/licenses/>. |
---|
18 | # |
---|
19 | # $Id$ |
---|
20 | # |
---|
21 | # |
---|
22 | |
---|
23 | """This module defines bases classes used by all reporters.""" |
---|
24 | |
---|
25 | import os |
---|
26 | import imp |
---|
27 | from mx import DateTime |
---|
28 | |
---|
29 | from pykota.errors import PyKotaReporterError |
---|
30 | |
---|
31 | class BaseReporter : |
---|
32 | """Base class for all reports.""" |
---|
33 | def __init__(self, tool, printers, ugnames, isgroup) : |
---|
34 | """Initialize local datas.""" |
---|
35 | self.tool = tool |
---|
36 | self.printers = printers |
---|
37 | self.ugnames = ugnames |
---|
38 | self.isgroup = isgroup |
---|
39 | |
---|
40 | def getPrinterTitle(self, printer) : |
---|
41 | """Returns the formatted title for a given printer.""" |
---|
42 | return (_("Report for %s quota on printer %s") % ((self.isgroup and "group") or "user", printer.Name)) + (" (%s)" % printer.Description) |
---|
43 | |
---|
44 | def getPrinterGraceDelay(self, printer) : |
---|
45 | """Returns the formatted grace delay for a given printer.""" |
---|
46 | return _("Pages grace time: %i days") % self.tool.config.getGraceDelay(printer.Name) |
---|
47 | |
---|
48 | def getPrinterPrices(self, printer) : |
---|
49 | """Returns the formatted prices for a given printer.""" |
---|
50 | return (_("Price per job: %.3f") % (printer.PricePerJob or 0.0), _("Price per page: %.3f") % (printer.PricePerPage or 0.0)) |
---|
51 | |
---|
52 | def getReportHeader(self) : |
---|
53 | """Returns the correct header depending on users vs users groups.""" |
---|
54 | if self.isgroup : |
---|
55 | return _("Group overcharge used soft hard balance grace total paid warn") |
---|
56 | else : |
---|
57 | return _("User overcharge used soft hard balance grace total paid warn") |
---|
58 | |
---|
59 | def getPrinterRealPageCounter(self, printer) : |
---|
60 | """Returns the formatted real page counter for a given printer.""" |
---|
61 | msg = _("unknown") |
---|
62 | if printer.LastJob.Exists : |
---|
63 | try : |
---|
64 | msg = "%9i" % printer.LastJob.PrinterPageCounter |
---|
65 | except TypeError : |
---|
66 | pass |
---|
67 | return _("Real : %s") % msg |
---|
68 | |
---|
69 | def getTotals(self, total, totalmoney) : |
---|
70 | """Returns the formatted totals.""" |
---|
71 | return (_("Total : %9i") % (total or 0.0), ("%11s" % ("%7.2f" % (totalmoney or 0.0))[:11])) |
---|
72 | |
---|
73 | def getQuota(self, entry, quota) : |
---|
74 | """Prints the quota information.""" |
---|
75 | lifepagecounter = int(quota.LifePageCounter or 0) |
---|
76 | pagecounter = int(quota.PageCounter or 0) |
---|
77 | balance = float(entry.AccountBalance or 0.0) |
---|
78 | lifetimepaid = float(entry.LifeTimePaid or 0.0) |
---|
79 | if not hasattr(entry, "OverCharge") : |
---|
80 | overcharge = _("N/A") # Not available for groups |
---|
81 | else : |
---|
82 | overcharge = float(entry.OverCharge or 0.0) |
---|
83 | if not hasattr(quota, "WarnCount") : |
---|
84 | warncount = _("N/A") # Not available for groups |
---|
85 | else : |
---|
86 | warncount = int(quota.WarnCount or 0) |
---|
87 | |
---|
88 | if (not entry.LimitBy) or (entry.LimitBy.lower() == "quota") : |
---|
89 | if (quota.HardLimit is not None) and (pagecounter >= quota.HardLimit) : |
---|
90 | datelimit = "DENY" |
---|
91 | elif (quota.HardLimit is None) and (quota.SoftLimit is not None) and (pagecounter >= quota.SoftLimit) : |
---|
92 | datelimit = "DENY" |
---|
93 | elif quota.DateLimit is not None : |
---|
94 | now = DateTime.now() |
---|
95 | datelimit = DateTime.ISO.ParseDateTime(str(quota.DateLimit)[:19]) |
---|
96 | if now >= datelimit : |
---|
97 | datelimit = "DENY" |
---|
98 | else : |
---|
99 | datelimit = "" |
---|
100 | reached = (((quota.SoftLimit is not None) and (pagecounter >= quota.SoftLimit) and "+") or "-") + "Q" |
---|
101 | else : |
---|
102 | if entry.LimitBy.lower() == "balance" : |
---|
103 | balancezero = self.tool.config.getBalanceZero() |
---|
104 | if balance == balancezero : |
---|
105 | if entry.OverCharge > 0 : |
---|
106 | datelimit = "DENY" |
---|
107 | reached = "+B" |
---|
108 | else : |
---|
109 | # overcharging by a negative or nul factor means user is always allowed to print |
---|
110 | # TODO : do something when printer prices are negative as well ! |
---|
111 | datelimit = "" |
---|
112 | reached = "-B" |
---|
113 | elif balance < balancezero : |
---|
114 | datelimit = "DENY" |
---|
115 | reached = "+B" |
---|
116 | elif balance <= self.tool.config.getPoorMan() : |
---|
117 | datelimit = "WARNING" |
---|
118 | reached = "?B" |
---|
119 | else : |
---|
120 | datelimit = "" |
---|
121 | reached = "-B" |
---|
122 | elif entry.LimitBy.lower() == "noquota" : |
---|
123 | reached = "NQ" |
---|
124 | datelimit = "" |
---|
125 | elif entry.LimitBy.lower() == "nochange" : |
---|
126 | reached = "NC" |
---|
127 | datelimit = "" |
---|
128 | else : |
---|
129 | # noprint |
---|
130 | reached = "NP" |
---|
131 | datelimit = "DENY" |
---|
132 | |
---|
133 | strbalance = ("%5.2f" % balance)[:10] |
---|
134 | strlifetimepaid = ("%6.2f" % lifetimepaid)[:10] |
---|
135 | strovercharge = ("%5s" % overcharge)[:5] |
---|
136 | strwarncount = ("%4s" % warncount)[:4] |
---|
137 | return (lifepagecounter, lifetimepaid, entry.Name, reached, \ |
---|
138 | pagecounter, str(quota.SoftLimit), str(quota.HardLimit), \ |
---|
139 | strbalance, str(datelimit)[:10], lifepagecounter, \ |
---|
140 | strlifetimepaid, strovercharge, strwarncount) |
---|
141 | |
---|
142 | def openReporter(tool, reporttype, printers, ugnames, isgroup) : |
---|
143 | """Returns a reporter instance of the proper reporter.""" |
---|
144 | try : |
---|
145 | reporterbackend = imp.load_source("reporterbackend", |
---|
146 | os.path.join(os.path.dirname(__file__), |
---|
147 | "reporters", |
---|
148 | "%s.py" % reporttype.lower())) |
---|
149 | except ImportError : |
---|
150 | raise PyKotaReporterError, _("Unsupported reporter backend %s") % reporttype |
---|
151 | else : |
---|
152 | return reporterbackend.Reporter(tool, printers, ugnames, isgroup) |
---|