root / pykota / trunk / pykota / accounters / querying.py @ 976

Revision 976, 5.9 kB (checked in by jalet, 21 years ago)

Stupid accounting method was added.

  • 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:36:40  jalet
24# Stupid accounting method was added.
25#
26# Revision 1.1  2003/04/29 18:37:54  jalet
27# Pluggable accounting methods (actually doesn't support external scripts)
28#
29#
30#
31
32import sys
33from pykota.accounter import AccounterBase, PyKotaAccounterError
34from pykota.requester import openRequester, PyKotaRequesterError
35
36MAXTRIES = 6     # maximum number of tries to get the printer's internal page counter
37TIMETOSLEEP = 10 # number of seconds to sleep between two tries to get the printer's internal page counter
38
39class Accounter(AccounterBase) :
40    def doAccounting(self, printerid, userid) :
41        """Does print accounting and returns if the job status is ALLOW or DENY."""
42        # Get the page counter directly from the printer itself
43        # Tries MAXTRIES times, sleeping two seconds each time, in case the printer is sleeping.
44        # This was seen with my Apple LaserWriter 16/600 PS which doesn't answer before having warmed up.
45        global MAXTRIES, TIMETOSLEEP
46        requester = openRequester(self.filter.config, self.filter.printername)
47        for i in range(MAXTRIES) :
48            try :
49                counterbeforejob = requester.getPrinterPageCounter(self.filter.printerhostname)
50            except PyKotaRequesterError, msg :
51                # can't get actual page counter, assume printer is off or warming up
52                # log the message anyway.
53                self.filter.logger.log_message("%s" % msg, "warn")
54                counterbeforejob = None
55                printerIsOff = 1
56            else :   
57                # printer answered, it is on so we can exit the loop
58                printerIsOff = 0
59                break
60            time.sleep(TIMETOSLEEP)   
61       
62        # get last job information for this printer
63        pgc = self.filter.storage.getPrinterPageCounter(printerid)   
64        if pgc is None :
65            # The printer hasn't been used yet, from PyKota's point of view
66            lasthistoryid = None
67            lastjobid = self.filter.jobid
68            lastuserid = userid
69            lastusername = self.filter.username
70            lastpagecounter = counterbeforejob
71        else :   
72            # get last values from Quota Storage
73            (lasthistoryid, lastjobid, lastuserid, lastusername, lastpagecounter) = (pgc["id"], pgc["jobid"], pgc["userid"], pgc["username"], pgc["pagecounter"])
74           
75        # if printer is off then we assume the correct counter value is the last one
76        if printerIsOff :
77            counterbeforejob = lastpagecounter
78           
79        # if the internal lifetime page counter for this printer is 0   
80        # then this may be a printer with a volatile counter (never
81        # saved to NVRAM) which has just been switched off and then on
82        # so we use the last page counter from the Quota Storage instead
83        # explanation at : http://web.mit.edu/source/third/lprng/doc/LPRng-HOWTO-15.html
84        if counterbeforejob == 0 :
85            counterbeforejob = lastpagecounter
86           
87        # Computes the last job size as the difference between internal page
88        # counter in the printer and last page counter taken from the Quota
89        # Storage database for this particular printer
90        try :
91            jobsize = (counterbeforejob - lastpagecounter)   
92        except TypeError :   
93            # never used, and internal page counter not accessible
94            jobsize = 0
95           
96        if jobsize < 0 :
97            # Probably an HP printer which was switched off and back on,
98            # its primary counter is only saved in a 10 increment, so
99            # it may be lower than the last page counter saved in the
100            # Quota Storage.
101            # We unconditionnally set the last job's size to
102            # abs(int((10 - abs(lastcounter(snmp) - lastcounter(storage)) / 2))
103            # For more accurate accounting, don't switch off your HP printers !
104            # explanation at : http://web.mit.edu/source/third/lprng/doc/LPRng-HOWTO-15.html
105            self.filter.logger.log_message(_("Error in page count value %i for user %s on printer %s") % (jobsize, lastusername, self.filter.printername), "error")
106            jobsize = abs(int((10 - abs(jobsize)) / 2))     # Workaround for HP printers' feature !
107           
108        # update the quota for the previous user on this printer
109        self.filter.storage.updateUserPQuota(lastuserid, printerid, jobsize)
110       
111        # update the last job size in the history
112        self.filter.storage.updateJobSizeInHistory(lasthistoryid, jobsize)
113       
114        # warns the last user if he is over quota
115        self.filter.warnUserPQuota(lastusername, self.filter.printername)
116           
117        # Is the current user allowed to print at all ?
118        action = self.filter.warnUserPQuota(self.filter.username, self.filter.printername)
119       
120        # adds the current job to history   
121        self.filter.storage.addJobToHistory(self.filter.jobid, self.filter.storage.getUserId(self.filter.username), printerid, counterbeforejob, action)
122           
123        return action
124           
Note: See TracBrowser for help on using the browser.