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

Revision 1041, 4.0 kB (checked in by jalet, 21 years ago)

Hey, it may work (edpykota --reset excepted) !

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