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