root / pykota / trunk / pykota / reporters / text.py @ 1068

Revision 1068, 5.3 kB (checked in by jalet, 21 years ago)

Lots of small fixes with the help of PyChecker?

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