[973] | 1 | # PyKota |
---|
[1144] | 2 | # -*- coding: ISO-8859-15 -*- |
---|
[973] | 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$ |
---|
[1144] | 24 | # Revision 1.6 2003/10/07 09:07:29 jalet |
---|
| 25 | # Character encoding added to please latest version of Python |
---|
| 26 | # |
---|
[1068] | 27 | # Revision 1.5 2003/07/07 11:49:24 jalet |
---|
| 28 | # Lots of small fixes with the help of PyChecker |
---|
| 29 | # |
---|
[1041] | 30 | # Revision 1.4 2003/06/25 14:10:01 jalet |
---|
| 31 | # Hey, it may work (edpykota --reset excepted) ! |
---|
| 32 | # |
---|
[988] | 33 | # Revision 1.3 2003/05/06 14:55:47 jalet |
---|
| 34 | # Missing import ! |
---|
| 35 | # |
---|
[976] | 36 | # Revision 1.2 2003/04/30 13:36:40 jalet |
---|
| 37 | # Stupid accounting method was added. |
---|
| 38 | # |
---|
[973] | 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 | |
---|
[976] | 45 | import sys |
---|
[988] | 46 | import time |
---|
[973] | 47 | from pykota.accounter import AccounterBase, PyKotaAccounterError |
---|
| 48 | from pykota.requester import openRequester, PyKotaRequesterError |
---|
| 49 | |
---|
[1041] | 50 | MAXTRIES = 12 # maximum number of tries to get the printer's internal page counter |
---|
[973] | 51 | TIMETOSLEEP = 10 # number of seconds to sleep between two tries to get the printer's internal page counter |
---|
| 52 | |
---|
| 53 | class Accounter(AccounterBase) : |
---|
[1041] | 54 | def doAccounting(self, printer, user) : |
---|
[973] | 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 |
---|
[1041] | 77 | if not printer.LastJob.Exists : |
---|
[973] | 78 | # The printer hasn't been used yet, from PyKota's point of view |
---|
[1041] | 79 | lastuser = user |
---|
[973] | 80 | lastpagecounter = counterbeforejob |
---|
| 81 | else : |
---|
| 82 | # get last values from Quota Storage |
---|
[1041] | 83 | lastuser = printer.LastJob.User |
---|
| 84 | lastpagecounter = printer.LastJob.PrinterPageCounter |
---|
[973] | 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 |
---|
[1041] | 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") |
---|
[973] | 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 |
---|
[1041] | 120 | lastuserquota = self.filter.storage.getUserPQuota(lastuser, printer) |
---|
| 121 | if lastuserquota.Exists : |
---|
| 122 | lastuserquota.increasePagesUsage(jobsize) |
---|
[973] | 123 | |
---|
| 124 | # update the last job size in the history |
---|
[1041] | 125 | if printer.LastJob.Exists : |
---|
| 126 | printer.LastJob.setSize(jobsize) |
---|
[973] | 127 | |
---|
| 128 | # warns the last user if he is over quota |
---|
[1041] | 129 | if lastuserquota.Exists : |
---|
| 130 | self.filter.warnUserPQuota(lastuserquota) |
---|
[973] | 131 | |
---|
| 132 | # Is the current user allowed to print at all ? |
---|
[1041] | 133 | action = self.filter.warnUserPQuota(self.filter.storage.getUserPQuota(user, printer)) |
---|
[973] | 134 | |
---|
| 135 | # adds the current job to history |
---|
[1041] | 136 | printer.addJobToHistory(self.filter.jobid, user, counterbeforejob, action) |
---|
[973] | 137 | |
---|
| 138 | return action |
---|
[976] | 139 | |
---|