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

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

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