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

Revision 1144, 6.0 kB (checked in by jalet, 21 years ago)

Character encoding added to please latest version of Python

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