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.5 2003/07/07 11:49:24 jalet |
---|
24 | # Lots of small fixes with the help of PyChecker |
---|
25 | # |
---|
26 | # Revision 1.4 2003/06/25 14:10:01 jalet |
---|
27 | # Hey, it may work (edpykota --reset excepted) ! |
---|
28 | # |
---|
29 | # Revision 1.3 2003/05/06 14:55:47 jalet |
---|
30 | # Missing import ! |
---|
31 | # |
---|
32 | # Revision 1.2 2003/04/30 13:36:40 jalet |
---|
33 | # Stupid accounting method was added. |
---|
34 | # |
---|
35 | # Revision 1.1 2003/04/29 18:37:54 jalet |
---|
36 | # Pluggable accounting methods (actually doesn't support external scripts) |
---|
37 | # |
---|
38 | # |
---|
39 | # |
---|
40 | |
---|
41 | import sys |
---|
42 | import time |
---|
43 | from pykota.accounter import AccounterBase, PyKotaAccounterError |
---|
44 | from pykota.requester import openRequester, PyKotaRequesterError |
---|
45 | |
---|
46 | MAXTRIES = 12 # maximum number of tries to get the printer's internal page counter |
---|
47 | TIMETOSLEEP = 10 # number of seconds to sleep between two tries to get the printer's internal page counter |
---|
48 | |
---|
49 | class Accounter(AccounterBase) : |
---|
50 | def doAccounting(self, printer, user) : |
---|
51 | """Does print accounting and returns if the job status is ALLOW or DENY.""" |
---|
52 | # Get the page counter directly from the printer itself |
---|
53 | # Tries MAXTRIES times, sleeping two seconds each time, in case the printer is sleeping. |
---|
54 | # This was seen with my Apple LaserWriter 16/600 PS which doesn't answer before having warmed up. |
---|
55 | global MAXTRIES, TIMETOSLEEP |
---|
56 | requester = openRequester(self.filter.config, self.filter.printername) |
---|
57 | for i in range(MAXTRIES) : |
---|
58 | try : |
---|
59 | counterbeforejob = requester.getPrinterPageCounter(self.filter.printerhostname) |
---|
60 | except PyKotaRequesterError, msg : |
---|
61 | # can't get actual page counter, assume printer is off or warming up |
---|
62 | # log the message anyway. |
---|
63 | self.filter.logger.log_message("%s" % msg, "warn") |
---|
64 | counterbeforejob = None |
---|
65 | printerIsOff = 1 |
---|
66 | else : |
---|
67 | # printer answered, it is on so we can exit the loop |
---|
68 | printerIsOff = 0 |
---|
69 | break |
---|
70 | time.sleep(TIMETOSLEEP) |
---|
71 | |
---|
72 | # get last job information for this printer |
---|
73 | if not printer.LastJob.Exists : |
---|
74 | # The printer hasn't been used yet, from PyKota's point of view |
---|
75 | lastuser = user |
---|
76 | lastpagecounter = counterbeforejob |
---|
77 | else : |
---|
78 | # get last values from Quota Storage |
---|
79 | lastuser = printer.LastJob.User |
---|
80 | lastpagecounter = printer.LastJob.PrinterPageCounter |
---|
81 | |
---|
82 | # if printer is off then we assume the correct counter value is the last one |
---|
83 | if printerIsOff : |
---|
84 | counterbeforejob = lastpagecounter |
---|
85 | |
---|
86 | # if the internal lifetime page counter for this printer is 0 |
---|
87 | # then this may be a printer with a volatile counter (never |
---|
88 | # saved to NVRAM) which has just been switched off and then on |
---|
89 | # so we use the last page counter from the Quota Storage instead |
---|
90 | # explanation at : http://web.mit.edu/source/third/lprng/doc/LPRng-HOWTO-15.html |
---|
91 | if counterbeforejob == 0 : |
---|
92 | counterbeforejob = lastpagecounter |
---|
93 | |
---|
94 | # Computes the last job size as the difference between internal page |
---|
95 | # counter in the printer and last page counter taken from the Quota |
---|
96 | # Storage database for this particular printer |
---|
97 | try : |
---|
98 | jobsize = (counterbeforejob - lastpagecounter) |
---|
99 | except TypeError : |
---|
100 | # never used, and internal page counter not accessible |
---|
101 | jobsize = 0 |
---|
102 | |
---|
103 | if jobsize < 0 : |
---|
104 | # Probably an HP printer which was switched off and back on, |
---|
105 | # its primary counter is only saved in a 10 increment, so |
---|
106 | # it may be lower than the last page counter saved in the |
---|
107 | # Quota Storage. |
---|
108 | # We unconditionnally set the last job's size to |
---|
109 | # abs(int((10 - abs(lastcounter(snmp) - lastcounter(storage)) / 2)) |
---|
110 | # For more accurate accounting, don't switch off your HP printers ! |
---|
111 | # explanation at : http://web.mit.edu/source/third/lprng/doc/LPRng-HOWTO-15.html |
---|
112 | self.filter.logger.log_message(_("Error in page count value %i for user %s on printer %s") % (jobsize, lastuser.Name, self.filter.printername), "error") |
---|
113 | jobsize = abs(int((10 - abs(jobsize)) / 2)) # Workaround for HP printers' feature ! |
---|
114 | |
---|
115 | # update the quota for the previous user on this printer |
---|
116 | lastuserquota = self.filter.storage.getUserPQuota(lastuser, printer) |
---|
117 | if lastuserquota.Exists : |
---|
118 | lastuserquota.increasePagesUsage(jobsize) |
---|
119 | |
---|
120 | # update the last job size in the history |
---|
121 | if printer.LastJob.Exists : |
---|
122 | printer.LastJob.setSize(jobsize) |
---|
123 | |
---|
124 | # warns the last user if he is over quota |
---|
125 | if lastuserquota.Exists : |
---|
126 | self.filter.warnUserPQuota(lastuserquota) |
---|
127 | |
---|
128 | # Is the current user allowed to print at all ? |
---|
129 | action = self.filter.warnUserPQuota(self.filter.storage.getUserPQuota(user, printer)) |
---|
130 | |
---|
131 | # adds the current job to history |
---|
132 | printer.addJobToHistory(self.filter.jobid, user, counterbeforejob, action) |
---|
133 | |
---|
134 | return action |
---|
135 | |
---|