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