1 | # PyKota |
---|
2 | # |
---|
3 | # PyKota - Print Quotas for CUPS and LPRng |
---|
4 | # |
---|
5 | # (c) 2003 Jerome Alet <alet@librelogiciel.com> |
---|
6 | # This program is free software; you can redistribute it and/or modify |
---|
7 | # it under the terms of the GNU General Public License as published by |
---|
8 | # the Free Software Foundation; either version 2 of the License, or |
---|
9 | # (at your option) any later version. |
---|
10 | # |
---|
11 | # This program is distributed in the hope that it will be useful, |
---|
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | # GNU General Public License for more details. |
---|
15 | # |
---|
16 | # You should have received a copy of the GNU General Public License |
---|
17 | # along with this program; if not, write to the Free Software |
---|
18 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
19 | # |
---|
20 | # $Id$ |
---|
21 | # |
---|
22 | # $Log$ |
---|
23 | # Revision 1.3 2003/05/06 14:55:47 jalet |
---|
24 | # Missing import ! |
---|
25 | # |
---|
26 | # Revision 1.2 2003/04/30 13:36:40 jalet |
---|
27 | # Stupid accounting method was added. |
---|
28 | # |
---|
29 | # Revision 1.1 2003/04/29 18:37:54 jalet |
---|
30 | # Pluggable accounting methods (actually doesn't support external scripts) |
---|
31 | # |
---|
32 | # |
---|
33 | # |
---|
34 | |
---|
35 | import sys |
---|
36 | import time |
---|
37 | from pykota.accounter import AccounterBase, PyKotaAccounterError |
---|
38 | from pykota.requester import openRequester, PyKotaRequesterError |
---|
39 | |
---|
40 | MAXTRIES = 6 # maximum number of tries to get the printer's internal page counter |
---|
41 | TIMETOSLEEP = 10 # number of seconds to sleep between two tries to get the printer's internal page counter |
---|
42 | |
---|
43 | class Accounter(AccounterBase) : |
---|
44 | def doAccounting(self, printerid, userid) : |
---|
45 | """Does print accounting and returns if the job status is ALLOW or DENY.""" |
---|
46 | # Get the page counter directly from the printer itself |
---|
47 | # Tries MAXTRIES times, sleeping two seconds each time, in case the printer is sleeping. |
---|
48 | # This was seen with my Apple LaserWriter 16/600 PS which doesn't answer before having warmed up. |
---|
49 | global MAXTRIES, TIMETOSLEEP |
---|
50 | requester = openRequester(self.filter.config, self.filter.printername) |
---|
51 | for i in range(MAXTRIES) : |
---|
52 | try : |
---|
53 | counterbeforejob = requester.getPrinterPageCounter(self.filter.printerhostname) |
---|
54 | except PyKotaRequesterError, msg : |
---|
55 | # can't get actual page counter, assume printer is off or warming up |
---|
56 | # log the message anyway. |
---|
57 | self.filter.logger.log_message("%s" % msg, "warn") |
---|
58 | counterbeforejob = None |
---|
59 | printerIsOff = 1 |
---|
60 | else : |
---|
61 | # printer answered, it is on so we can exit the loop |
---|
62 | printerIsOff = 0 |
---|
63 | break |
---|
64 | time.sleep(TIMETOSLEEP) |
---|
65 | |
---|
66 | # get last job information for this printer |
---|
67 | pgc = self.filter.storage.getPrinterPageCounter(printerid) |
---|
68 | if pgc is None : |
---|
69 | # The printer hasn't been used yet, from PyKota's point of view |
---|
70 | lasthistoryid = None |
---|
71 | lastjobid = self.filter.jobid |
---|
72 | lastuserid = userid |
---|
73 | lastusername = self.filter.username |
---|
74 | lastpagecounter = counterbeforejob |
---|
75 | else : |
---|
76 | # get last values from Quota Storage |
---|
77 | (lasthistoryid, lastjobid, lastuserid, lastusername, lastpagecounter) = (pgc["id"], pgc["jobid"], pgc["userid"], pgc["username"], pgc["pagecounter"]) |
---|
78 | |
---|
79 | # if printer is off then we assume the correct counter value is the last one |
---|
80 | if printerIsOff : |
---|
81 | counterbeforejob = lastpagecounter |
---|
82 | |
---|
83 | # if the internal lifetime page counter for this printer is 0 |
---|
84 | # then this may be a printer with a volatile counter (never |
---|
85 | # saved to NVRAM) which has just been switched off and then on |
---|
86 | # so we use the last page counter from the Quota Storage instead |
---|
87 | # explanation at : http://web.mit.edu/source/third/lprng/doc/LPRng-HOWTO-15.html |
---|
88 | if counterbeforejob == 0 : |
---|
89 | counterbeforejob = lastpagecounter |
---|
90 | |
---|
91 | # Computes the last job size as the difference between internal page |
---|
92 | # counter in the printer and last page counter taken from the Quota |
---|
93 | # Storage database for this particular printer |
---|
94 | try : |
---|
95 | jobsize = (counterbeforejob - lastpagecounter) |
---|
96 | except TypeError : |
---|
97 | # never used, and internal page counter not accessible |
---|
98 | jobsize = 0 |
---|
99 | |
---|
100 | if jobsize < 0 : |
---|
101 | # Probably an HP printer which was switched off and back on, |
---|
102 | # its primary counter is only saved in a 10 increment, so |
---|
103 | # it may be lower than the last page counter saved in the |
---|
104 | # Quota Storage. |
---|
105 | # We unconditionnally set the last job's size to |
---|
106 | # abs(int((10 - abs(lastcounter(snmp) - lastcounter(storage)) / 2)) |
---|
107 | # For more accurate accounting, don't switch off your HP printers ! |
---|
108 | # explanation at : http://web.mit.edu/source/third/lprng/doc/LPRng-HOWTO-15.html |
---|
109 | self.filter.logger.log_message(_("Error in page count value %i for user %s on printer %s") % (jobsize, lastusername, self.filter.printername), "error") |
---|
110 | jobsize = abs(int((10 - abs(jobsize)) / 2)) # Workaround for HP printers' feature ! |
---|
111 | |
---|
112 | # update the quota for the previous user on this printer |
---|
113 | self.filter.storage.updateUserPQuota(lastuserid, printerid, jobsize) |
---|
114 | |
---|
115 | # update the last job size in the history |
---|
116 | self.filter.storage.updateJobSizeInHistory(lasthistoryid, jobsize) |
---|
117 | |
---|
118 | # warns the last user if he is over quota |
---|
119 | self.filter.warnUserPQuota(lastusername, self.filter.printername) |
---|
120 | |
---|
121 | # Is the current user allowed to print at all ? |
---|
122 | action = self.filter.warnUserPQuota(self.filter.username, self.filter.printername) |
---|
123 | |
---|
124 | # adds the current job to history |
---|
125 | self.filter.storage.addJobToHistory(self.filter.jobid, self.filter.storage.getUserId(self.filter.username), printerid, counterbeforejob, action) |
---|
126 | |
---|
127 | return action |
---|
128 | |
---|