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

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