1 | # PyKota |
---|
2 | # -*- coding: ISO-8859-15 -*- |
---|
3 | |
---|
4 | # PyKota - Print Quotas for CUPS and LPRng |
---|
5 | # |
---|
6 | # (c) 2003 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
20 | # |
---|
21 | # $Id$ |
---|
22 | # |
---|
23 | # $Log$ |
---|
24 | # Revision 1.5 2003/10/07 09:07:29 jalet |
---|
25 | # Character encoding added to please latest version of Python |
---|
26 | # |
---|
27 | # Revision 1.4 2003/07/07 11:49:24 jalet |
---|
28 | # Lots of small fixes with the help of PyChecker |
---|
29 | # |
---|
30 | # Revision 1.3 2003/07/05 07:46:50 jalet |
---|
31 | # The previous bug fix was incomplete. |
---|
32 | # |
---|
33 | # Revision 1.2 2003/07/02 09:29:12 jalet |
---|
34 | # Bug fixed when wanting a report and an user/group was limited by account balance |
---|
35 | # |
---|
36 | # Revision 1.1 2003/06/30 12:46:15 jalet |
---|
37 | # Extracted reporting code. |
---|
38 | # |
---|
39 | # |
---|
40 | # |
---|
41 | |
---|
42 | from mx import DateTime |
---|
43 | |
---|
44 | from pykota.reporter import BaseReporter, PyKotaReporterError |
---|
45 | |
---|
46 | class Reporter(BaseReporter) : |
---|
47 | """Base class for all PyKota command line tools.""" |
---|
48 | def generateReport(self) : |
---|
49 | """Produces a simple text report.""" |
---|
50 | self.report = [] |
---|
51 | for printer in self.printers : |
---|
52 | self.report.append(_("*** Report for %s quota on printer %s") % ((self.isgroup and "group") or "user", printer.Name)) |
---|
53 | self.report.append(_("Pages grace time: %i days") % self.tool.config.getGraceDelay(printer.Name)) |
---|
54 | if printer.PricePerJob is not None : |
---|
55 | self.report.append(_("Price per job: %.3f") % printer.PricePerJob) |
---|
56 | if printer.PricePerPage is not None : |
---|
57 | self.report.append(_("Price per page: %.3f") % printer.PricePerPage) |
---|
58 | total = 0 |
---|
59 | totalmoney = 0.0 |
---|
60 | if self.isgroup : |
---|
61 | self.report.append(_("Group used soft hard balance grace total paid")) |
---|
62 | self.report.append("------------------------------------------------------------------------------") |
---|
63 | for (group, grouppquota) in self.tool.storage.getPrinterGroupsAndQuotas(printer, self.ugnames) : |
---|
64 | (pages, money) = self.printQuota(group, grouppquota) |
---|
65 | total += pages |
---|
66 | totalmoney += money |
---|
67 | else : |
---|
68 | # default is user quota report |
---|
69 | self.report.append(_("User used soft hard balance grace total paid")) |
---|
70 | self.report.append("------------------------------------------------------------------------------") |
---|
71 | for (user, userpquota) in self.tool.storage.getPrinterUsersAndQuotas(printer, self.ugnames) : |
---|
72 | (pages, money) = self.printQuota(user, userpquota) |
---|
73 | total += pages |
---|
74 | totalmoney += money |
---|
75 | if total or totalmoney : |
---|
76 | self.report.append((" " * 50) + (_("Total : %9i") % total) + ("%11s" % ("%7.2f" % totalmoney)[:11])) |
---|
77 | try : |
---|
78 | msg = "%9i" % printer.LastJob.PrinterPageCounter |
---|
79 | except TypeError : |
---|
80 | msg = _("unknown") |
---|
81 | self.report.append((" " * 51) + (_("Real : %s") % msg)) |
---|
82 | self.report.append("") |
---|
83 | if self.isgroup : |
---|
84 | self.report.append(_("Totals may be inaccurate if some users are members of several groups.")) |
---|
85 | return "\n".join(self.report) |
---|
86 | |
---|
87 | def printQuota(self, entry, quota) : |
---|
88 | """Prints the quota information.""" |
---|
89 | lifepagecounter = int(quota.LifePageCounter or 0) |
---|
90 | pagecounter = int(quota.PageCounter or 0) |
---|
91 | balance = float(entry.AccountBalance or 0.0) |
---|
92 | lifetimepaid = float(entry.LifeTimePaid or 0.0) |
---|
93 | |
---|
94 | if entry.LimitBy and (entry.LimitBy.lower() == "balance") : |
---|
95 | if balance <= 0 : |
---|
96 | datelimit = "DENY" |
---|
97 | reached = "+B" |
---|
98 | else : |
---|
99 | datelimit = "" |
---|
100 | reached = "-B" |
---|
101 | else : |
---|
102 | if quota.DateLimit is not None : |
---|
103 | now = DateTime.now() |
---|
104 | datelimit = DateTime.ISO.ParseDateTime(quota.DateLimit) |
---|
105 | if now >= datelimit : |
---|
106 | datelimit = "DENY" |
---|
107 | elif (quota.HardLimit is not None) and (pagecounter >= quota.HardLimit) : |
---|
108 | datelimit = "DENY" |
---|
109 | elif (quota.HardLimit is None) and (quota.SoftLimit is not None) and (pagecounter >= quota.SoftLimit) : |
---|
110 | datelimit = "DENY" |
---|
111 | else : |
---|
112 | datelimit = "" |
---|
113 | reached = (((quota.SoftLimit is not None) and (pagecounter >= quota.SoftLimit) and "+") or "-") + "Q" |
---|
114 | |
---|
115 | strbalance = ("%5.2f" % balance)[:10] |
---|
116 | strlifetimepaid = ("%6.2f" % lifetimepaid)[:10] |
---|
117 | 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)) |
---|
118 | return (lifepagecounter, lifetimepaid) |
---|
119 | |
---|