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

Revision 1180, 7.9 kB (checked in by jalet, 21 years ago)

More work on new backend. This commit may be unstable.

  • 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/12 23:29:24  jalet
25# More work on new backend. This commit may be unstable.
26#
27# Revision 1.6  2003/10/07 09:07:29  jalet
28# Character encoding added to please latest version of Python
29#
30# Revision 1.5  2003/07/07 11:49:24  jalet
31# Lots of small fixes with the help of PyChecker
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/06 14:55:47  jalet
37# Missing import !
38#
39# Revision 1.2  2003/04/30 13:36:40  jalet
40# Stupid accounting method was added.
41#
42# Revision 1.1  2003/04/29 18:37:54  jalet
43# Pluggable accounting methods (actually doesn't support external scripts)
44#
45#
46#
47
48import sys
49import os
50import time
51from pykota.accounter import AccounterBase, PyKotaAccounterError
52from pykota.requester import openRequester, PyKotaRequesterError
53
54MAXTRIES = 12    # maximum number of tries to get the printer's internal page counter
55TIMETOSLEEP = 10 # number of seconds to sleep between two tries to get the printer's internal page counter
56
57class Accounter(AccounterBase) :
58    def __init__(self, kotabackend, arguments) :
59        """Initializes querying accounter."""
60        AccounterBase.__init__(self, kotabackend, arguments)
61        self.requester = openRequester(kotabackend.config, kotabackend.printername)
62       
63    def getPrinterInternalPageCounter(self) :   
64        """Returns the printer's internal page counter."""
65        global MAXTRIES, TIMETOSLEEP
66        for i in range(MAXTRIES) :
67            try :
68                counter = self.requester.getPrinterPageCounter(self.filter.printerhostname)
69            except PyKotaRequesterError, msg :
70                # can't get actual page counter, assume printer is off or warming up
71                # log the message anyway.
72                self.filter.logger.log_message("%s" % msg, "warn")
73                counter = None
74            else :   
75                # printer answered, it is on so we can exit the loop
76                break
77            time.sleep(TIMETOSLEEP)   
78        return counter   
79       
80    def beginJob(self, printer, user) :   
81        """Saves printer internal page counter at start of job."""
82        # save page counter before job
83        self.LastPageCounter = self.counterbefore = self.getPrinterInternalPageCounter()
84       
85    def endJob(self, printer, user) :   
86        """Saves printer internal page counter at end of job."""
87        # save page counter after job
88        self.LastPageCounter = self.counterafter = self.getPrinterInternalPageCounter()
89       
90    def getJobSize(self) :   
91        """Returns the actual job size."""
92        try :
93            jobsize = (self.counterafter - self.counterbefore)   
94            if jobsize < 0 :
95                # Try to take care of HP printers
96                # Their internal page counter is saved to NVRAM
97                # only every 10 pages. If the printer was switched
98                # off then back on during the job, and that the
99                # counters difference is negative, we know
100                # the formula (we can't know if more than eleven
101                # pages were printed though) :
102                if jobsize > -10 :
103                    jobsize += 10
104                else :   
105                    # here we may have got a printer being replaced
106                    # DURING the job. This is HIGHLY improbable !
107                    jobsize = 0
108        except :   
109            # takes care of the case where one counter (or both) was never set.
110            jobsize = 0
111        return jobsize
112       
113    def doAccounting(self, printer, user) :
114        """Does print accounting and returns if the job status is ALLOW or DENY."""
115        # Get the page counter directly from the printer itself
116        # Tries MAXTRIES times, sleeping two seconds each time, in case the printer is sleeping.
117        # This was seen with my Apple LaserWriter 16/600 PS which doesn't answer before having warmed up.
118        counterbeforejob = self.getPrinterInternalPageCounter()
119       
120        # get last job information for this printer
121        if not printer.LastJob.Exists :
122            # The printer hasn't been used yet, from PyKota's point of view
123            lastuser = user
124            lastpagecounter = counterbeforejob
125        else :   
126            # get last values from Quota Storage
127            lastuser = printer.LastJob.User
128            lastpagecounter = printer.LastJob.PrinterPageCounter
129           
130        # if printer is off then we assume the correct counter value is the last one
131        if counterbeforejob is None :
132            counterbeforejob = lastpagecounter
133           
134        # if the internal lifetime page counter for this printer is 0   
135        # then this may be a printer with a volatile counter (never
136        # saved to NVRAM) which has just been switched off and then on
137        # so we use the last page counter from the Quota Storage instead
138        # explanation at : http://web.mit.edu/source/third/lprng/doc/LPRng-HOWTO-15.html
139        if counterbeforejob == 0 :
140            counterbeforejob = lastpagecounter
141           
142        # Computes the last job size as the difference between internal page
143        # counter in the printer and last page counter taken from the Quota
144        # Storage database for this particular printer
145        try :
146            jobsize = (counterbeforejob - lastpagecounter)   
147        except TypeError :   
148            # never used, and internal page counter not accessible
149            jobsize = 0
150           
151        if jobsize < 0 :
152            # Probably an HP printer which was switched off and back on,
153            # its primary counter is only saved in a 10 increment, so
154            # it may be lower than the last page counter saved in the
155            # Quota Storage.
156            # We unconditionnally set the last job's size to
157            # abs(int((10 - abs(lastcounter(snmp) - lastcounter(storage)) / 2))
158            # For more accurate accounting, don't switch off your HP printers !
159            # explanation at : http://web.mit.edu/source/third/lprng/doc/LPRng-HOWTO-15.html
160            self.filter.logger.log_message(_("Error in page count value %i for user %s on printer %s") % (jobsize, lastuser.Name, self.filter.printername), "error")
161            jobsize = abs(int((10 - abs(jobsize)) / 2))     # Workaround for HP printers' feature !
162           
163        # update the quota for the previous user on this printer
164        lastuserquota = self.filter.storage.getUserPQuota(lastuser, printer)
165        if lastuserquota.Exists :
166            lastuserquota.increasePagesUsage(jobsize)
167       
168        # update the last job size in the history
169        if printer.LastJob.Exists :
170            printer.LastJob.setSize(jobsize)
171       
172        # warns the last user if he is over quota
173        if lastuserquota.Exists :
174            self.filter.warnUserPQuota(lastuserquota)
175           
176        # Is the current user allowed to print at all ?
177        action = self.filter.warnUserPQuota(self.filter.storage.getUserPQuota(user, printer))
178       
179        # adds the current job to history   
180        printer.addJobToHistory(self.filter.jobid, user, counterbeforejob, action)
181           
182        return action
183           
Note: See TracBrowser for help on using the browser.