root / pykota / trunk / pykota / accounters / stupid.py @ 1203

Revision 1203, 5.5 kB (checked in by jalet, 20 years ago)

Job price added to history

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
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.8  2003/11/23 19:01:37  jalet
25# Job price added to history
26#
27# Revision 1.7  2003/11/21 14:28:46  jalet
28# More complete job history.
29#
30# Revision 1.6  2003/11/12 23:29:24  jalet
31# More work on new backend. This commit may be unstable.
32#
33# Revision 1.5  2003/10/07 09:07:29  jalet
34# Character encoding added to please latest version of Python
35#
36# Revision 1.4  2003/06/25 14:10:01  jalet
37# Hey, it may work (edpykota --reset excepted) !
38#
39# Revision 1.3  2003/05/27 23:00:21  jalet
40# Big rewrite of external accounting methods.
41# Should work well now.
42#
43# Revision 1.2  2003/04/30 13:40:47  jalet
44# Small fix
45#
46# Revision 1.1  2003/04/30 13:36:40  jalet
47# Stupid accounting method was added.
48#
49#
50#
51
52import sys
53import tempfile
54from pykota.accounter import AccounterBase, PyKotaAccounterError
55
56class Accounter(AccounterBase) :
57    def beginJob(self) :   
58        """Saves the computed job size."""
59        self.JobSize = self.computeJobSize()
60       
61        # get last job information for this printer
62        if not printer.LastJob.Exists :
63            # The printer hasn't been used yet, from PyKota's point of view
64            self.LastPageCounter = 0
65        else :   
66            # get last job size and page counter from Quota Storage
67            # Last lifetime page counter before actual job is
68            # last page counter + last job size
69            self.LastPageCounter = int(printer.LastJob.PrinterPageCounter or 0) + int(printer.LastJob.JobSize or 0)
70       
71    def endJob(self) :   
72        """Do nothing."""
73        pass
74       
75    def getJobSize(self) :   
76        """Returns the actual job size."""
77        try :
78            return self.JobSize
79        except AttributeError :   
80            return 0
81       
82    def doAccounting(self, printer, user) :
83        """Does print accounting by stupidly counting the 'showpage' postscript instructions in the document.
84       
85           This method is essentially unreliable, but shows how to create a simple accounter.
86        """
87        # first we log a message because using this accounting method is not recommended.
88        self.filter.logger.log_message(_("Using the 'stupid' accounting method is unreliable."), "warn")
89       
90        # get the job size   
91        jobsize = self.computeJobSize() * self.filter.copies
92           
93        # get last job information for this printer
94        if not printer.LastJob.Exists :
95            # The printer hasn't been used yet, from PyKota's point of view
96            counterbeforejob = 0
97        else :   
98            # get last job size and page counter from Quota Storage
99            # Last lifetime page counter before actual job is
100            # last page counter + last job size
101            counterbeforejob = int(printer.LastJob.PrinterPageCounter or 0) + int(printer.LastJob.JobSize or 0)
102           
103        # Is the current user allowed to print at all ?
104        userpquota = self.filter.storage.getUserPQuota(user, printer)
105        action = self.filter.warnUserPQuota(userpquota)
106       
107        # update the quota for the current user on this printer, if allowed to print
108        if action == "DENY" :
109            jobsize = 0
110        else :   
111            userpquota.increasePagesUsage(jobsize)
112       
113        # adds the current job to history   
114        jobprice = (float(printer.PricePerPage or 0.0) * jobsize) + float(printer.PricePerJob or 0.0)
115        printer.addJobToHistory(self.filter.jobid, user, counterbeforejob, action, jobsize, jobprice, self.filter.preserveinputfile, self.filter.title, self.filter.copies, self.filter.options)
116           
117        return action
118       
119    def computeJobSize(self) :   
120        """Computes the job size and return its value.
121       
122           THIS METHOD IS COMPLETELY UNRELIABLE BUT SERVES AS AN EXAMPLE.
123        """
124        temporary = None   
125        if self.filter.inputfile is None :   
126            infile = sys.stdin
127            # we will have to duplicate our standard input
128            temporary = tempfile.TemporaryFile()
129        else :   
130            infile = open(self.filter.inputfile, "rb")
131           
132        pagecount = 0
133        for line in infile.xreadlines() :
134            pagecount += line.count("showpage")
135            if temporary is not None :   
136                temporary.write(line)   
137               
138        if temporary is not None :   
139            # this is a copy of our previous standard input
140            # flush, then rewind
141            temporary.flush()
142            temporary.seek(0, 0)
143            # our temporary file will be used later if the
144            # job is allowed.
145            self.filter.inputfile = temporary
146        else :
147            infile.close()
148           
149        return pagecount   
150           
Note: See TracBrowser for help on using the browser.