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

Revision 977, 3.9 kB (checked in by jalet, 21 years ago)

Small fix

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1# PyKota
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.2  2003/04/30 13:40:47  jalet
24# Small fix
25#
26# Revision 1.1  2003/04/30 13:36:40  jalet
27# Stupid accounting method was added.
28#
29#
30#
31
32import sys
33import tempfile
34from pykota.accounter import AccounterBase, PyKotaAccounterError
35
36class Accounter(AccounterBase) :
37    def doAccounting(self, printerid, userid) :
38        """Does print accounting by stupidly counting the 'showpage' postscript instructions in the document.
39       
40           This method is essentially unreliable, but shows how to create a simple accounter.
41        """
42        # first we log a message because using this accounting method is not recommended.
43        self.filter.logger.log_message(_("Using the 'stupid' accounting method is unreliable."), "warn")
44       
45        # get the job size   
46        jobsize = self.getJobSize()
47           
48        # get last job information for this printer
49        pgc = self.filter.storage.getPrinterPageCounter(printerid)   
50        if pgc is None :
51            # The printer hasn't been used yet, from PyKota's point of view
52            counterbeforejob = 0
53        else :   
54            # get last job size and page counter from Quota Storage
55            # Last lifetime page counter before actual job is
56            # last page counter + last job size
57            counterbeforejob = (pgc["pagecounter"] or 0) + (pgc["jobsize"] or 0)
58           
59        # Is the current user allowed to print at all ?
60        action = self.filter.warnUserPQuota(self.filter.username, self.filter.printername)
61       
62        # update the quota for the current user on this printer, if allowed to print
63        if action == "DENY" :
64            jobsize = 0
65        else :   
66            self.filter.storage.updateUserPQuota(userid, printerid, jobsize)
67       
68        # adds the current job to history   
69        self.filter.storage.addJobToHistory(self.filter.jobid, self.filter.storage.getUserId(self.filter.username), printerid, counterbeforejob, action, jobsize)
70           
71        return action
72       
73    def getJobSize(self) :   
74        """Computes the job size and return its value.
75       
76           THIS METHOD IS COMPLETELY UNRELIABLE BUT SERVES AS AN EXAMPLE.
77        """
78        temporary = None   
79        if self.filter.inputfile is None :   
80            infile = sys.stdin
81            # we will have to duplicate our standard input
82            temporary = tempfile.TemporaryFile()
83        else :   
84            infile = open(self.filter.inputfile, "rb")
85           
86        pagecount = 0
87        for line in infile.xreadlines() :
88            if line.startswith("showpage") :
89                pagecount += 1
90            if temporary is not None :   
91                temporary.write(line)   
92               
93        if temporary is not None :   
94            # this is a copy of our previous standard input
95            # flush, then rewind
96            temporary.flush()
97            temporary.seek(0, 0)
98            # our temporary file will be used later if the
99            # job is allowed.
100            self.filter.inputfile = temporary
101        else :
102            infile.close()
103           
104        return pagecount   
105           
Note: See TracBrowser for help on using the browser.