[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$ |
---|
[1271] | 24 | # Revision 1.12 2004/01/11 23:22:42 jalet |
---|
| 25 | # Major code refactoring, it's way cleaner, and now allows automated addition |
---|
| 26 | # of printers on first print. |
---|
| 27 | # |
---|
[1257] | 28 | # Revision 1.11 2004/01/08 14:10:32 jalet |
---|
| 29 | # Copyright year changed. |
---|
| 30 | # |
---|
[1240] | 31 | # Revision 1.10 2003/12/27 16:49:25 uid67467 |
---|
| 32 | # Should be ok now. |
---|
[1239] | 33 | # |
---|
[1200] | 34 | # Revision 1.8 2003/11/21 14:28:46 jalet |
---|
| 35 | # More complete job history. |
---|
| 36 | # |
---|
[1180] | 37 | # Revision 1.7 2003/11/12 23:29:24 jalet |
---|
| 38 | # More work on new backend. This commit may be unstable. |
---|
| 39 | # |
---|
[1144] | 40 | # Revision 1.6 2003/10/07 09:07:29 jalet |
---|
| 41 | # Character encoding added to please latest version of Python |
---|
| 42 | # |
---|
[1068] | 43 | # Revision 1.5 2003/07/07 11:49:24 jalet |
---|
| 44 | # Lots of small fixes with the help of PyChecker |
---|
| 45 | # |
---|
[1041] | 46 | # Revision 1.4 2003/06/25 14:10:01 jalet |
---|
| 47 | # Hey, it may work (edpykota --reset excepted) ! |
---|
| 48 | # |
---|
[988] | 49 | # Revision 1.3 2003/05/06 14:55:47 jalet |
---|
| 50 | # Missing import ! |
---|
| 51 | # |
---|
[976] | 52 | # Revision 1.2 2003/04/30 13:36:40 jalet |
---|
| 53 | # Stupid accounting method was added. |
---|
| 54 | # |
---|
[973] | 55 | # Revision 1.1 2003/04/29 18:37:54 jalet |
---|
| 56 | # Pluggable accounting methods (actually doesn't support external scripts) |
---|
| 57 | # |
---|
| 58 | # |
---|
| 59 | # |
---|
| 60 | |
---|
[976] | 61 | import sys |
---|
[1180] | 62 | import os |
---|
[988] | 63 | import time |
---|
[973] | 64 | from pykota.accounter import AccounterBase, PyKotaAccounterError |
---|
| 65 | from pykota.requester import openRequester, PyKotaRequesterError |
---|
| 66 | |
---|
[1041] | 67 | MAXTRIES = 12 # maximum number of tries to get the printer's internal page counter |
---|
[973] | 68 | TIMETOSLEEP = 10 # number of seconds to sleep between two tries to get the printer's internal page counter |
---|
| 69 | |
---|
| 70 | class Accounter(AccounterBase) : |
---|
[1180] | 71 | def __init__(self, kotabackend, arguments) : |
---|
| 72 | """Initializes querying accounter.""" |
---|
| 73 | AccounterBase.__init__(self, kotabackend, arguments) |
---|
| 74 | self.requester = openRequester(kotabackend.config, kotabackend.printername) |
---|
[1271] | 75 | self.isDelayed = 1 # With the pykota filter, accounting is delayed by one job |
---|
[1180] | 76 | |
---|
| 77 | def getPrinterInternalPageCounter(self) : |
---|
| 78 | """Returns the printer's internal page counter.""" |
---|
[973] | 79 | global MAXTRIES, TIMETOSLEEP |
---|
[1240] | 80 | self.filter.logdebug("Reading printer's internal page counter...") |
---|
| 81 | for dummy in range(MAXTRIES) : |
---|
[973] | 82 | try : |
---|
[1180] | 83 | counter = self.requester.getPrinterPageCounter(self.filter.printerhostname) |
---|
[973] | 84 | except PyKotaRequesterError, msg : |
---|
| 85 | # can't get actual page counter, assume printer is off or warming up |
---|
| 86 | # log the message anyway. |
---|
| 87 | self.filter.logger.log_message("%s" % msg, "warn") |
---|
[1180] | 88 | counter = None |
---|
[973] | 89 | else : |
---|
| 90 | # printer answered, it is on so we can exit the loop |
---|
| 91 | break |
---|
| 92 | time.sleep(TIMETOSLEEP) |
---|
[1240] | 93 | self.filter.logdebug("Printer's internal page counter value is : %s" % str(counter)) |
---|
[1180] | 94 | return counter |
---|
[973] | 95 | |
---|
[1271] | 96 | def beginJob(self, userpquota) : |
---|
[1180] | 97 | """Saves printer internal page counter at start of job.""" |
---|
| 98 | # save page counter before job |
---|
| 99 | self.LastPageCounter = self.counterbefore = self.getPrinterInternalPageCounter() |
---|
| 100 | |
---|
[1271] | 101 | def endJob(self, userpquota) : |
---|
[1180] | 102 | """Saves printer internal page counter at end of job.""" |
---|
| 103 | # save page counter after job |
---|
| 104 | self.LastPageCounter = self.counterafter = self.getPrinterInternalPageCounter() |
---|
| 105 | |
---|
| 106 | def getJobSize(self) : |
---|
| 107 | """Returns the actual job size.""" |
---|
| 108 | try : |
---|
| 109 | jobsize = (self.counterafter - self.counterbefore) |
---|
| 110 | if jobsize < 0 : |
---|
| 111 | # Try to take care of HP printers |
---|
| 112 | # Their internal page counter is saved to NVRAM |
---|
| 113 | # only every 10 pages. If the printer was switched |
---|
| 114 | # off then back on during the job, and that the |
---|
| 115 | # counters difference is negative, we know |
---|
| 116 | # the formula (we can't know if more than eleven |
---|
| 117 | # pages were printed though) : |
---|
| 118 | if jobsize > -10 : |
---|
| 119 | jobsize += 10 |
---|
| 120 | else : |
---|
| 121 | # here we may have got a printer being replaced |
---|
| 122 | # DURING the job. This is HIGHLY improbable ! |
---|
| 123 | jobsize = 0 |
---|
| 124 | except : |
---|
| 125 | # takes care of the case where one counter (or both) was never set. |
---|
| 126 | jobsize = 0 |
---|
| 127 | return jobsize |
---|
| 128 | |
---|
[1271] | 129 | def doAccounting(self, userpquota) : |
---|
[1180] | 130 | """Does print accounting and returns if the job status is ALLOW or DENY.""" |
---|
| 131 | # Get the page counter directly from the printer itself |
---|
[1271] | 132 | counterbeforejob = self.getPrinterInternalPageCounter() or 0 |
---|
[1180] | 133 | |
---|
[973] | 134 | # Is the current user allowed to print at all ? |
---|
[1271] | 135 | action = self.filter.warnUserPQuota(userpquota) |
---|
[973] | 136 | |
---|
| 137 | # adds the current job to history |
---|
[1271] | 138 | userpquota.Printer.addJobToHistory(self.filter.jobid, userpquota.User, counterbeforejob, action, filename=self.filter.preserveinputfile, title=self.filter.title, copies=self.filter.copies, options=self.filter.options) |
---|
[973] | 139 | return action |
---|
[976] | 140 | |
---|