1 | # PyKota |
---|
2 | # -*- coding: ISO-8859-15 -*- |
---|
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$ |
---|
24 | # Revision 1.10 2003/12/27 16:49:25 uid67467 |
---|
25 | # Should be ok now. |
---|
26 | # |
---|
27 | # Revision 1.8 2003/11/21 14:28:46 jalet |
---|
28 | # More complete job history. |
---|
29 | # |
---|
30 | # Revision 1.7 2003/11/12 23:29:24 jalet |
---|
31 | # More work on new backend. This commit may be unstable. |
---|
32 | # |
---|
33 | # Revision 1.6 2003/10/07 09:07:29 jalet |
---|
34 | # Character encoding added to please latest version of Python |
---|
35 | # |
---|
36 | # Revision 1.5 2003/07/07 11:49:24 jalet |
---|
37 | # Lots of small fixes with the help of PyChecker |
---|
38 | # |
---|
39 | # Revision 1.4 2003/06/25 14:10:01 jalet |
---|
40 | # Hey, it may work (edpykota --reset excepted) ! |
---|
41 | # |
---|
42 | # Revision 1.3 2003/05/06 14:55:47 jalet |
---|
43 | # Missing import ! |
---|
44 | # |
---|
45 | # Revision 1.2 2003/04/30 13:36:40 jalet |
---|
46 | # Stupid accounting method was added. |
---|
47 | # |
---|
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 | |
---|
54 | import sys |
---|
55 | import os |
---|
56 | import time |
---|
57 | from pykota.accounter import AccounterBase, PyKotaAccounterError |
---|
58 | from pykota.requester import openRequester, PyKotaRequesterError |
---|
59 | |
---|
60 | MAXTRIES = 12 # maximum number of tries to get the printer's internal page counter |
---|
61 | TIMETOSLEEP = 10 # number of seconds to sleep between two tries to get the printer's internal page counter |
---|
62 | |
---|
63 | class Accounter(AccounterBase) : |
---|
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.""" |
---|
71 | global MAXTRIES, TIMETOSLEEP |
---|
72 | self.filter.logdebug("Reading printer's internal page counter...") |
---|
73 | for dummy in range(MAXTRIES) : |
---|
74 | try : |
---|
75 | counter = self.requester.getPrinterPageCounter(self.filter.printerhostname) |
---|
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") |
---|
80 | counter = None |
---|
81 | else : |
---|
82 | # printer answered, it is on so we can exit the loop |
---|
83 | break |
---|
84 | time.sleep(TIMETOSLEEP) |
---|
85 | self.filter.logdebug("Printer's internal page counter value is : %s" % str(counter)) |
---|
86 | return counter |
---|
87 | |
---|
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 | |
---|
128 | # get last job information for this printer |
---|
129 | if not printer.LastJob.Exists : |
---|
130 | # The printer hasn't been used yet, from PyKota's point of view |
---|
131 | lastuser = user |
---|
132 | lastpagecounter = counterbeforejob |
---|
133 | else : |
---|
134 | # get last values from Quota Storage |
---|
135 | lastuser = printer.LastJob.User |
---|
136 | lastpagecounter = printer.LastJob.PrinterPageCounter |
---|
137 | |
---|
138 | # if printer is off then we assume the correct counter value is the last one |
---|
139 | if counterbeforejob is None : |
---|
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 |
---|
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") |
---|
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 |
---|
172 | lastuserquota = self.filter.storage.getUserPQuota(lastuser, printer) |
---|
173 | if lastuserquota.Exists : |
---|
174 | lastuserquota.increasePagesUsage(jobsize) |
---|
175 | |
---|
176 | # update the last job size in the history |
---|
177 | if printer.LastJob.Exists : |
---|
178 | printer.LastJob.setSize(jobsize) |
---|
179 | |
---|
180 | # warns the last user if he is over quota |
---|
181 | if lastuserquota.Exists : |
---|
182 | self.filter.warnUserPQuota(lastuserquota) |
---|
183 | |
---|
184 | # Is the current user allowed to print at all ? |
---|
185 | action = self.filter.warnUserPQuota(self.filter.storage.getUserPQuota(user, printer)) |
---|
186 | |
---|
187 | # adds the current job to history |
---|
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) |
---|
189 | return action |
---|
190 | |
---|