1 | # PyKota |
---|
2 | # -*- coding: ISO-8859-15 -*- |
---|
3 | # |
---|
4 | # PyKota : Print Quotas for CUPS and LPRng |
---|
5 | # |
---|
6 | # (c) 2003-2004 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.51 2004/03/24 15:15:24 jalet |
---|
25 | # Began integration of Henrik Janhagen's work on quota-then-balance |
---|
26 | # and balance-then-quota |
---|
27 | # |
---|
28 | # Revision 1.50 2004/03/09 08:05:27 jalet |
---|
29 | # Small fix : only keeps existing quota entries when searching parents |
---|
30 | # |
---|
31 | # Revision 1.49 2004/03/01 15:06:51 jalet |
---|
32 | # Pre and Post hooks should now work in the pykota filter too. |
---|
33 | # The pykota filter doesn't check the last user's quota anymore |
---|
34 | # when delayed hardware accounting is used : this will be checked |
---|
35 | # anyway the next time the last user will print |
---|
36 | # |
---|
37 | # Revision 1.48 2004/02/27 13:50:12 jalet |
---|
38 | # Hopefully the final fix for groups (users and printers) |
---|
39 | # |
---|
40 | # Revision 1.47 2004/02/27 09:30:33 jalet |
---|
41 | # datelimit wasn't reset when modifying soft and hard limits with the LDAP backend |
---|
42 | # |
---|
43 | # Revision 1.46 2004/02/26 14:18:07 jalet |
---|
44 | # Should fix the remaining bugs wrt printers groups and users groups. |
---|
45 | # |
---|
46 | # Revision 1.45 2004/02/26 10:40:40 jalet |
---|
47 | # Fixed nested printer groups accounting. |
---|
48 | # |
---|
49 | # Revision 1.44 2004/02/25 19:09:24 jalet |
---|
50 | # Fix for LDAP problem when job price was 0. |
---|
51 | # |
---|
52 | # Revision 1.43 2004/02/25 12:36:34 jalet |
---|
53 | # Avoids a database query even if caching was disabled. |
---|
54 | # |
---|
55 | # Revision 1.42 2004/02/23 22:53:21 jalet |
---|
56 | # Don't retrieve data when it's not needed, to avoid database queries |
---|
57 | # |
---|
58 | # Revision 1.41 2004/02/04 17:12:33 jalet |
---|
59 | # Removing a printer from a printers group should work now. |
---|
60 | # |
---|
61 | # Revision 1.40 2004/02/04 13:24:41 jalet |
---|
62 | # pkprinters can now remove printers from printers groups. |
---|
63 | # |
---|
64 | # Revision 1.39 2004/02/04 11:16:59 jalet |
---|
65 | # pkprinters command line tool added. |
---|
66 | # |
---|
67 | # Revision 1.38 2004/01/12 22:43:40 jalet |
---|
68 | # New formula to compute a job's price |
---|
69 | # |
---|
70 | # Revision 1.37 2004/01/12 14:35:01 jalet |
---|
71 | # Printing history added to CGI script. |
---|
72 | # |
---|
73 | # Revision 1.36 2004/01/10 09:44:02 jalet |
---|
74 | # Fixed potential accuracy problem if a user printed on several printers at |
---|
75 | # the very same time. |
---|
76 | # |
---|
77 | # Revision 1.35 2004/01/08 16:33:27 jalet |
---|
78 | # Additionnal check to not create a circular printers group. |
---|
79 | # |
---|
80 | # Revision 1.34 2004/01/08 16:24:49 jalet |
---|
81 | # edpykota now supports adding printers to printer groups. |
---|
82 | # |
---|
83 | # Revision 1.33 2004/01/08 14:10:32 jalet |
---|
84 | # Copyright year changed. |
---|
85 | # |
---|
86 | # Revision 1.32 2004/01/06 16:02:57 jalet |
---|
87 | # This time printer groups caching works. |
---|
88 | # |
---|
89 | # Revision 1.31 2004/01/06 15:51:24 jalet |
---|
90 | # Fixed caching of printer groups |
---|
91 | # |
---|
92 | # Revision 1.30 2004/01/06 14:24:59 jalet |
---|
93 | # Printer groups should be cached now, if caching is enabled. |
---|
94 | # |
---|
95 | # Revision 1.29 2003/12/27 16:49:25 uid67467 |
---|
96 | # Should be ok now. |
---|
97 | # |
---|
98 | # Revision 1.28 2003/11/25 23:46:40 jalet |
---|
99 | # Don't try to verify if module name is valid, Python does this better than us. |
---|
100 | # |
---|
101 | # Revision 1.27 2003/11/23 19:01:36 jalet |
---|
102 | # Job price added to history |
---|
103 | # |
---|
104 | # Revision 1.26 2003/11/21 14:28:45 jalet |
---|
105 | # More complete job history. |
---|
106 | # |
---|
107 | # Revision 1.25 2003/10/08 21:12:27 jalet |
---|
108 | # Do not cache anymore entries which don't exist. |
---|
109 | # |
---|
110 | # Revision 1.24 2003/10/07 22:06:05 jalet |
---|
111 | # Preliminary code to disable job history |
---|
112 | # |
---|
113 | # Revision 1.23 2003/10/07 09:07:28 jalet |
---|
114 | # Character encoding added to please latest version of Python |
---|
115 | # |
---|
116 | # Revision 1.22 2003/10/06 13:12:27 jalet |
---|
117 | # More work on caching |
---|
118 | # |
---|
119 | # Revision 1.21 2003/10/03 09:02:20 jalet |
---|
120 | # Logs cache store actions too |
---|
121 | # |
---|
122 | # Revision 1.20 2003/10/02 20:23:18 jalet |
---|
123 | # Storage caching mechanism added. |
---|
124 | # |
---|
125 | # Revision 1.19 2003/07/16 21:53:07 jalet |
---|
126 | # Really big modifications wrt new configuration file's location and content. |
---|
127 | # |
---|
128 | # Revision 1.18 2003/07/07 08:33:18 jalet |
---|
129 | # Bug fix due to a typo in LDAP code |
---|
130 | # |
---|
131 | # Revision 1.17 2003/07/05 07:46:50 jalet |
---|
132 | # The previous bug fix was incomplete. |
---|
133 | # |
---|
134 | # Revision 1.16 2003/06/25 19:52:31 jalet |
---|
135 | # Should be ready for testing :-) |
---|
136 | # |
---|
137 | # Revision 1.15 2003/06/25 14:10:58 jalet |
---|
138 | # Exception raising for now. |
---|
139 | # |
---|
140 | # Revision 1.14 2003/06/25 14:10:01 jalet |
---|
141 | # Hey, it may work (edpykota --reset excepted) ! |
---|
142 | # |
---|
143 | # Revision 1.13 2003/06/10 16:37:54 jalet |
---|
144 | # Deletion of the second user which is not needed anymore. |
---|
145 | # Added a debug configuration field in /etc/pykota.conf |
---|
146 | # All queries can now be sent to the logger in debug mode, this will |
---|
147 | # greatly help improve performance when time for this will come. |
---|
148 | # |
---|
149 | # Revision 1.12 2003/04/23 22:13:57 jalet |
---|
150 | # Preliminary support for LPRng added BUT STILL UNTESTED. |
---|
151 | # |
---|
152 | # Revision 1.11 2003/04/10 21:47:20 jalet |
---|
153 | # Job history added. Upgrade script neutralized for now ! |
---|
154 | # |
---|
155 | # Revision 1.10 2003/03/29 13:45:27 jalet |
---|
156 | # GPL paragraphs were incorrectly (from memory) copied into the sources. |
---|
157 | # Two README files were added. |
---|
158 | # Upgrade script for PostgreSQL pre 1.01 schema was added. |
---|
159 | # |
---|
160 | # Revision 1.9 2003/02/17 22:55:01 jalet |
---|
161 | # More options can now be set per printer or globally : |
---|
162 | # |
---|
163 | # admin |
---|
164 | # adminmail |
---|
165 | # gracedelay |
---|
166 | # requester |
---|
167 | # |
---|
168 | # the printer option has priority when both are defined. |
---|
169 | # |
---|
170 | # Revision 1.8 2003/02/17 22:05:50 jalet |
---|
171 | # Storage backend now supports admin and user passwords (untested) |
---|
172 | # |
---|
173 | # Revision 1.7 2003/02/10 12:07:31 jalet |
---|
174 | # Now repykota should output the recorded total page number for each printer too. |
---|
175 | # |
---|
176 | # Revision 1.6 2003/02/09 13:05:43 jalet |
---|
177 | # Internationalization continues... |
---|
178 | # |
---|
179 | # Revision 1.5 2003/02/08 22:39:46 jalet |
---|
180 | # --reset command line option added |
---|
181 | # |
---|
182 | # Revision 1.4 2003/02/08 09:59:59 jalet |
---|
183 | # Added preliminary base class for all storages |
---|
184 | # |
---|
185 | # Revision 1.3 2003/02/05 22:10:29 jalet |
---|
186 | # Typos |
---|
187 | # |
---|
188 | # Revision 1.2 2003/02/05 22:02:22 jalet |
---|
189 | # __import__ statement didn't work as expected |
---|
190 | # |
---|
191 | # Revision 1.1 2003/02/05 21:28:17 jalet |
---|
192 | # Initial import into CVS |
---|
193 | # |
---|
194 | # |
---|
195 | # |
---|
196 | |
---|
197 | class PyKotaStorageError(Exception): |
---|
198 | """An exception for Quota Storage related stuff.""" |
---|
199 | def __init__(self, message = ""): |
---|
200 | self.message = message |
---|
201 | Exception.__init__(self, message) |
---|
202 | def __repr__(self): |
---|
203 | return self.message |
---|
204 | __str__ = __repr__ |
---|
205 | |
---|
206 | class StorageObject : |
---|
207 | """Object present in the Quota Storage.""" |
---|
208 | def __init__(self, parent) : |
---|
209 | "Initialize minimal data.""" |
---|
210 | self.parent = parent |
---|
211 | self.ident = None |
---|
212 | self.Exists = 0 |
---|
213 | |
---|
214 | class StorageUser(StorageObject) : |
---|
215 | """User class.""" |
---|
216 | def __init__(self, parent, name) : |
---|
217 | StorageObject.__init__(self, parent) |
---|
218 | self.Name = name |
---|
219 | self.LimitBy = None |
---|
220 | self.AccountBalance = None |
---|
221 | self.LifeTimePaid = None |
---|
222 | self.Email = None |
---|
223 | |
---|
224 | def consumeAccountBalance(self, amount) : |
---|
225 | """Consumes an amount of money from the user's account balance.""" |
---|
226 | self.parent.decreaseUserAccountBalance(self, amount) |
---|
227 | self.AccountBalance = float(self.AccountBalance or 0.0) - amount |
---|
228 | |
---|
229 | def setAccountBalance(self, balance, lifetimepaid) : |
---|
230 | """Sets the user's account balance in case he pays more money.""" |
---|
231 | self.parent.writeUserAccountBalance(self, balance, lifetimepaid) |
---|
232 | self.AccountBalance = balance |
---|
233 | self.LifeTimePaid = lifetimepaid |
---|
234 | |
---|
235 | def setLimitBy(self, limitby) : |
---|
236 | """Sets the user's limiting factor.""" |
---|
237 | try : |
---|
238 | limitby = limitby.lower() |
---|
239 | except AttributeError : |
---|
240 | limitby = "quota" |
---|
241 | if limitby in ["quota", "balance", "quota-then-balance", "balance-then-quota"] : |
---|
242 | self.parent.writeUserLimitBy(self, limitby) |
---|
243 | self.LimitBy = limitby |
---|
244 | |
---|
245 | def delete(self) : |
---|
246 | """Deletes an user from the Quota Storage.""" |
---|
247 | self.parent.beginTransaction() |
---|
248 | try : |
---|
249 | self.parent.deleteUser(self) |
---|
250 | except PyKotaStorageError, msg : |
---|
251 | self.parent.rollbackTransaction() |
---|
252 | raise PyKotaStorageError, msg |
---|
253 | else : |
---|
254 | self.parent.commitTransaction() |
---|
255 | |
---|
256 | class StorageGroup(StorageObject) : |
---|
257 | """User class.""" |
---|
258 | def __init__(self, parent, name) : |
---|
259 | StorageObject.__init__(self, parent) |
---|
260 | self.Name = name |
---|
261 | self.LimitBy = None |
---|
262 | self.AccountBalance = None |
---|
263 | self.LifeTimePaid = None |
---|
264 | |
---|
265 | def setLimitBy(self, limitby) : |
---|
266 | """Sets the user's limiting factor.""" |
---|
267 | try : |
---|
268 | limitby = limitby.lower() |
---|
269 | except AttributeError : |
---|
270 | limitby = "quota" |
---|
271 | if limitby in ["quota", "balance"] : |
---|
272 | self.parent.writeGroupLimitBy(self, limitby) |
---|
273 | self.LimitBy = limitby |
---|
274 | |
---|
275 | def delete(self) : |
---|
276 | """Deletes a group from the Quota Storage.""" |
---|
277 | self.parent.beginTransaction() |
---|
278 | try : |
---|
279 | self.parent.deleteGroup(self) |
---|
280 | except PyKotaStorageError, msg : |
---|
281 | self.parent.rollbackTransaction() |
---|
282 | raise PyKotaStorageError, msg |
---|
283 | else : |
---|
284 | self.parent.commitTransaction() |
---|
285 | |
---|
286 | class StoragePrinter(StorageObject) : |
---|
287 | """Printer class.""" |
---|
288 | def __init__(self, parent, name) : |
---|
289 | StorageObject.__init__(self, parent) |
---|
290 | self.Name = name |
---|
291 | self.PricePerPage = None |
---|
292 | self.PricePerJob = None |
---|
293 | |
---|
294 | def __getattr__(self, name) : |
---|
295 | """Delays data retrieval until it's really needed.""" |
---|
296 | if name == "LastJob" : |
---|
297 | self.LastJob = self.parent.getPrinterLastJob(self) |
---|
298 | return self.LastJob |
---|
299 | else : |
---|
300 | raise AttributeError, name |
---|
301 | |
---|
302 | def addJobToHistory(self, jobid, user, pagecounter, action, jobsize=None, jobprice=None, filename=None, title=None, copies=None, options=None) : |
---|
303 | """Adds a job to the printer's history.""" |
---|
304 | self.parent.writeJobNew(self, user, jobid, pagecounter, action, jobsize, jobprice, filename, title, copies, options) |
---|
305 | # TODO : update LastJob object ? Probably not needed. |
---|
306 | |
---|
307 | def addPrinterToGroup(self, printer) : |
---|
308 | """Adds a printer to a printer group.""" |
---|
309 | if (printer not in self.parent.getParentPrinters(self)) and (printer.ident != self.ident) : |
---|
310 | self.parent.writePrinterToGroup(self, printer) |
---|
311 | # TODO : reset cached value for printer parents, or add new parent to cached value |
---|
312 | |
---|
313 | def delPrinterFromGroup(self, printer) : |
---|
314 | """Deletes a printer from a printer group.""" |
---|
315 | self.parent.removePrinterFromGroup(self, printer) |
---|
316 | # TODO : reset cached value for printer parents, or add new parent to cached value |
---|
317 | |
---|
318 | def setPrices(self, priceperpage = None, priceperjob = None) : |
---|
319 | """Sets the printer's prices.""" |
---|
320 | if priceperpage is None : |
---|
321 | priceperpage = self.PricePerPage |
---|
322 | else : |
---|
323 | self.PricePerPage = float(priceperpage) |
---|
324 | if priceperjob is None : |
---|
325 | priceperjob = self.PricePerJob |
---|
326 | else : |
---|
327 | self.PricePerJob = float(priceperjob) |
---|
328 | self.parent.writePrinterPrices(self) |
---|
329 | |
---|
330 | def delete(self) : |
---|
331 | """Deletes a printer from the Quota Storage.""" |
---|
332 | self.parent.beginTransaction() |
---|
333 | try : |
---|
334 | self.parent.deletePrinter(self) |
---|
335 | except PyKotaStorageError, msg : |
---|
336 | self.parent.rollbackTransaction() |
---|
337 | raise PyKotaStorageError, msg |
---|
338 | else : |
---|
339 | self.parent.commitTransaction() |
---|
340 | |
---|
341 | class StorageUserPQuota(StorageObject) : |
---|
342 | """User Print Quota class.""" |
---|
343 | def __init__(self, parent, user, printer) : |
---|
344 | StorageObject.__init__(self, parent) |
---|
345 | self.User = user |
---|
346 | self.Printer = printer |
---|
347 | self.PageCounter = None |
---|
348 | self.LifePageCounter = None |
---|
349 | self.SoftLimit = None |
---|
350 | self.HardLimit = None |
---|
351 | self.DateLimit = None |
---|
352 | |
---|
353 | def __getattr__(self, name) : |
---|
354 | """Delays data retrieval until it's really needed.""" |
---|
355 | if name == "ParentPrintersUserPQuota" : |
---|
356 | self.ParentPrintersUserPQuota = (self.User.Exists and self.Printer.Exists and self.parent.getParentPrintersUserPQuota(self)) or [] |
---|
357 | return self.ParentPrintersUserPQuota |
---|
358 | else : |
---|
359 | raise AttributeError, name |
---|
360 | |
---|
361 | def setDateLimit(self, datelimit) : |
---|
362 | """Sets the date limit for this quota.""" |
---|
363 | date = "%04i-%02i-%02i %02i:%02i:%02i" % (datelimit.year, datelimit.month, datelimit.day, datelimit.hour, datelimit.minute, datelimit.second) |
---|
364 | self.parent.writeUserPQuotaDateLimit(self, date) |
---|
365 | self.DateLimit = date |
---|
366 | |
---|
367 | def setLimits(self, softlimit, hardlimit) : |
---|
368 | """Sets the soft and hard limit for this quota.""" |
---|
369 | self.parent.writeUserPQuotaLimits(self, softlimit, hardlimit) |
---|
370 | self.SoftLimit = softlimit |
---|
371 | self.HardLimit = hardlimit |
---|
372 | self.DateLimit = None |
---|
373 | |
---|
374 | def reset(self) : |
---|
375 | """Resets page counter to 0.""" |
---|
376 | self.parent.writeUserPQuotaPagesCounters(self, 0, int(self.LifePageCounter or 0)) |
---|
377 | self.PageCounter = 0 |
---|
378 | |
---|
379 | def computeJobPrice(self, jobsize) : |
---|
380 | """Computes the job price as the sum of all parent printers' prices + current printer's ones.""" |
---|
381 | totalprice = 0.0 |
---|
382 | if jobsize : |
---|
383 | for upq in [ self ] + self.ParentPrintersUserPQuota : |
---|
384 | price = (float(upq.Printer.PricePerPage or 0.0) * jobsize) + float(upq.Printer.PricePerJob or 0.0) |
---|
385 | totalprice += price |
---|
386 | return totalprice |
---|
387 | |
---|
388 | def increasePagesUsage(self, jobsize) : |
---|
389 | """Increase the value of used pages and money.""" |
---|
390 | jobprice = self.computeJobPrice(jobsize) |
---|
391 | if jobsize : |
---|
392 | self.parent.beginTransaction() |
---|
393 | try : |
---|
394 | if jobprice : |
---|
395 | self.User.consumeAccountBalance(jobprice) |
---|
396 | for upq in [ self ] + self.ParentPrintersUserPQuota : |
---|
397 | self.parent.increaseUserPQuotaPagesCounters(upq, jobsize) |
---|
398 | upq.PageCounter = int(upq.PageCounter or 0) + jobsize |
---|
399 | upq.LifePageCounter = int(upq.LifePageCounter or 0) + jobsize |
---|
400 | except PyKotaStorageError, msg : |
---|
401 | self.parent.rollbackTransaction() |
---|
402 | raise PyKotaStorageError, msg |
---|
403 | else : |
---|
404 | self.parent.commitTransaction() |
---|
405 | return jobprice |
---|
406 | |
---|
407 | class StorageGroupPQuota(StorageObject) : |
---|
408 | """Group Print Quota class.""" |
---|
409 | def __init__(self, parent, group, printer) : |
---|
410 | StorageObject.__init__(self, parent) |
---|
411 | self.Group = group |
---|
412 | self.Printer = printer |
---|
413 | self.PageCounter = None |
---|
414 | self.LifePageCounter = None |
---|
415 | self.SoftLimit = None |
---|
416 | self.HardLimit = None |
---|
417 | self.DateLimit = None |
---|
418 | |
---|
419 | def __getattr__(self, name) : |
---|
420 | """Delays data retrieval until it's really needed.""" |
---|
421 | if name == "ParentPrintersGroupPQuota" : |
---|
422 | self.ParentPrintersGroupPQuota = (self.Group.Exists and self.Printer.Exists and self.parent.getParentPrintersGroupPQuota(self)) or [] |
---|
423 | return self.ParentPrintersGroupPQuota |
---|
424 | else : |
---|
425 | raise AttributeError, name |
---|
426 | |
---|
427 | def setDateLimit(self, datelimit) : |
---|
428 | """Sets the date limit for this quota.""" |
---|
429 | date = "%04i-%02i-%02i %02i:%02i:%02i" % (datelimit.year, datelimit.month, datelimit.day, datelimit.hour, datelimit.minute, datelimit.second) |
---|
430 | self.parent.writeGroupPQuotaDateLimit(self, date) |
---|
431 | self.DateLimit = date |
---|
432 | |
---|
433 | def setLimits(self, softlimit, hardlimit) : |
---|
434 | """Sets the soft and hard limit for this quota.""" |
---|
435 | self.parent.writeGroupPQuotaLimits(self, softlimit, hardlimit) |
---|
436 | self.SoftLimit = softlimit |
---|
437 | self.HardLimit = hardlimit |
---|
438 | self.DateLimit = None |
---|
439 | |
---|
440 | class StorageJob(StorageObject) : |
---|
441 | """Printer's Last Job class.""" |
---|
442 | def __init__(self, parent) : |
---|
443 | StorageObject.__init__(self, parent) |
---|
444 | self.UserName = None |
---|
445 | self.PrinterName = None |
---|
446 | self.JobId = None |
---|
447 | self.PrinterPageCounter = None |
---|
448 | self.JobSize = None |
---|
449 | self.JobAction = None |
---|
450 | self.JobDate = None |
---|
451 | self.JobPrice = None |
---|
452 | self.JobFileName = None |
---|
453 | self.JobTitle = None |
---|
454 | self.JobCopies = None |
---|
455 | self.JobOptions = None |
---|
456 | |
---|
457 | def __getattr__(self, name) : |
---|
458 | """Delays data retrieval until it's really needed.""" |
---|
459 | if name == "User" : |
---|
460 | self.User = self.parent.getUser(self.UserName) |
---|
461 | return self.User |
---|
462 | elif name == "Printer" : |
---|
463 | self.Printer = self.parent.getPrinter(self.PrinterName) |
---|
464 | return self.Printer |
---|
465 | else : |
---|
466 | raise AttributeError, name |
---|
467 | |
---|
468 | class StorageLastJob(StorageJob) : |
---|
469 | """Printer's Last Job class.""" |
---|
470 | def __init__(self, parent, printer) : |
---|
471 | StorageJob.__init__(self, parent) |
---|
472 | self.PrinterName = printer.Name # not needed |
---|
473 | self.Printer = printer |
---|
474 | |
---|
475 | def __getattr__(self, name) : |
---|
476 | """Delays data retrieval until it's really needed.""" |
---|
477 | if name == "User" : |
---|
478 | self.User = self.parent.getUser(self.UserName) |
---|
479 | return self.User |
---|
480 | else : |
---|
481 | raise AttributeError, name |
---|
482 | |
---|
483 | def setSize(self, userpquota, jobsize) : |
---|
484 | """Sets the last job's size.""" |
---|
485 | jobprice = userpquota.computeJobPrice(jobsize) |
---|
486 | self.parent.writeLastJobSize(self, jobsize, jobprice) |
---|
487 | self.JobSize = jobsize |
---|
488 | self.JobPrice = jobprice |
---|
489 | return jobprice |
---|
490 | |
---|
491 | class BaseStorage : |
---|
492 | def __init__(self, pykotatool) : |
---|
493 | """Opens the LDAP connection.""" |
---|
494 | # raise PyKotaStorageError, "Sorry, the LDAP backend for PyKota is not yet implemented !" |
---|
495 | self.closed = 1 |
---|
496 | self.tool = pykotatool |
---|
497 | self.usecache = pykotatool.config.getCaching() |
---|
498 | self.disablehistory = pykotatool.config.getDisableHistory() |
---|
499 | if self.usecache : |
---|
500 | self.tool.logdebug("Caching enabled.") |
---|
501 | self.caches = { "USERS" : {}, "GROUPS" : {}, "PRINTERS" : {}, "USERPQUOTAS" : {}, "GROUPPQUOTAS" : {}, "JOBS" : {}, "LASTJOBS" : {} } |
---|
502 | |
---|
503 | def close(self) : |
---|
504 | """Must be overriden in children classes.""" |
---|
505 | raise RuntimeError, "BaseStorage.close() must be overriden !" |
---|
506 | |
---|
507 | def __del__(self) : |
---|
508 | """Ensures that the database connection is closed.""" |
---|
509 | self.close() |
---|
510 | |
---|
511 | def getFromCache(self, cachetype, key) : |
---|
512 | """Tries to extract something from the cache.""" |
---|
513 | if self.usecache : |
---|
514 | entry = self.caches[cachetype].get(key) |
---|
515 | if entry is not None : |
---|
516 | self.tool.logdebug("Cache hit (%s->%s)" % (cachetype, key)) |
---|
517 | else : |
---|
518 | self.tool.logdebug("Cache miss (%s->%s)" % (cachetype, key)) |
---|
519 | return entry |
---|
520 | |
---|
521 | def cacheEntry(self, cachetype, key, value) : |
---|
522 | """Puts an entry in the cache.""" |
---|
523 | if self.usecache and getattr(value, "Exists", 0) : |
---|
524 | self.caches[cachetype][key] = value |
---|
525 | self.tool.logdebug("Cache store (%s->%s)" % (cachetype, key)) |
---|
526 | |
---|
527 | def getUser(self, username) : |
---|
528 | """Returns the user from cache.""" |
---|
529 | user = self.getFromCache("USERS", username) |
---|
530 | if user is None : |
---|
531 | user = self.getUserFromBackend(username) |
---|
532 | self.cacheEntry("USERS", username, user) |
---|
533 | return user |
---|
534 | |
---|
535 | def getGroup(self, groupname) : |
---|
536 | """Returns the group from cache.""" |
---|
537 | group = self.getFromCache("GROUPS", groupname) |
---|
538 | if group is None : |
---|
539 | group = self.getGroupFromBackend(groupname) |
---|
540 | self.cacheEntry("GROUPS", groupname, group) |
---|
541 | return group |
---|
542 | |
---|
543 | def getPrinter(self, printername) : |
---|
544 | """Returns the printer from cache.""" |
---|
545 | printer = self.getFromCache("PRINTERS", printername) |
---|
546 | if printer is None : |
---|
547 | printer = self.getPrinterFromBackend(printername) |
---|
548 | self.cacheEntry("PRINTERS", printername, printer) |
---|
549 | return printer |
---|
550 | |
---|
551 | def getUserPQuota(self, user, printer) : |
---|
552 | """Returns the user quota information from cache.""" |
---|
553 | useratprinter = "%s@%s" % (user.Name, printer.Name) |
---|
554 | upquota = self.getFromCache("USERPQUOTAS", useratprinter) |
---|
555 | if upquota is None : |
---|
556 | upquota = self.getUserPQuotaFromBackend(user, printer) |
---|
557 | self.cacheEntry("USERPQUOTAS", useratprinter, upquota) |
---|
558 | return upquota |
---|
559 | |
---|
560 | def getGroupPQuota(self, group, printer) : |
---|
561 | """Returns the group quota information from cache.""" |
---|
562 | groupatprinter = "%s@%s" % (group.Name, printer.Name) |
---|
563 | gpquota = self.getFromCache("GROUPPQUOTAS", groupatprinter) |
---|
564 | if gpquota is None : |
---|
565 | gpquota = self.getGroupPQuotaFromBackend(group, printer) |
---|
566 | self.cacheEntry("GROUPPQUOTAS", groupatprinter, gpquota) |
---|
567 | return gpquota |
---|
568 | |
---|
569 | def getPrinterLastJob(self, printer) : |
---|
570 | """Extracts last job information for a given printer from cache.""" |
---|
571 | lastjob = self.getFromCache("LASTJOBS", printer.Name) |
---|
572 | if lastjob is None : |
---|
573 | lastjob = self.getPrinterLastJobFromBackend(printer) |
---|
574 | self.cacheEntry("LASTJOBS", printer.Name, lastjob) |
---|
575 | return lastjob |
---|
576 | |
---|
577 | def getParentPrinters(self, printer) : |
---|
578 | """Extracts parent printers information for a given printer from cache.""" |
---|
579 | if self.usecache : |
---|
580 | if not hasattr(printer, "Parents") : |
---|
581 | self.tool.logdebug("Cache miss (%s->Parents)" % printer.Name) |
---|
582 | printer.Parents = self.getParentPrintersFromBackend(printer) |
---|
583 | self.tool.logdebug("Cache store (%s->Parents)" % printer.Name) |
---|
584 | else : |
---|
585 | self.tool.logdebug("Cache hit (%s->Parents)" % printer.Name) |
---|
586 | else : |
---|
587 | printer.Parents = self.getParentPrintersFromBackend(printer) |
---|
588 | for parent in printer.Parents[:] : |
---|
589 | printer.Parents.extend(self.getParentPrinters(parent)) |
---|
590 | uniquedic = {} |
---|
591 | for parent in printer.Parents : |
---|
592 | uniquedic[parent.Name] = parent |
---|
593 | printer.Parents = uniquedic.values() |
---|
594 | return printer.Parents |
---|
595 | |
---|
596 | def getGroupMembers(self, group) : |
---|
597 | """Returns the group's members list from in-group cache.""" |
---|
598 | if self.usecache : |
---|
599 | if not hasattr(group, "Members") : |
---|
600 | self.tool.logdebug("Cache miss (%s->Members)" % group.Name) |
---|
601 | group.Members = self.getGroupMembersFromBackend(group) |
---|
602 | self.tool.logdebug("Cache store (%s->Members)" % group.Name) |
---|
603 | else : |
---|
604 | self.tool.logdebug("Cache hit (%s->Members)" % group.Name) |
---|
605 | else : |
---|
606 | group.Members = self.getGroupMembersFromBackend(group) |
---|
607 | return group.Members |
---|
608 | |
---|
609 | def getUserGroups(self, user) : |
---|
610 | """Returns the user's groups list from in-user cache.""" |
---|
611 | if self.usecache : |
---|
612 | if not hasattr(user, "Groups") : |
---|
613 | self.tool.logdebug("Cache miss (%s->Groups)" % user.Name) |
---|
614 | user.Groups = self.getUserGroupsFromBackend(user) |
---|
615 | self.tool.logdebug("Cache store (%s->Groups)" % user.Name) |
---|
616 | else : |
---|
617 | self.tool.logdebug("Cache hit (%s->Groups)" % user.Name) |
---|
618 | else : |
---|
619 | user.Groups = self.getUserGroupsFromBackend(user) |
---|
620 | return user.Groups |
---|
621 | |
---|
622 | def getParentPrintersUserPQuota(self, userpquota) : |
---|
623 | """Returns all user print quota on the printer and all its parents recursively.""" |
---|
624 | upquotas = [ ] |
---|
625 | for printer in self.getParentPrinters(userpquota.Printer) : |
---|
626 | upq = self.getUserPQuota(userpquota.User, printer) |
---|
627 | if upq.Exists : |
---|
628 | upquotas.append(upq) |
---|
629 | return upquotas |
---|
630 | |
---|
631 | def getParentPrintersGroupPQuota(self, grouppquota) : |
---|
632 | """Returns all group print quota on the printer and all its parents recursively.""" |
---|
633 | gpquotas = [ ] |
---|
634 | for printer in self.getParentPrinters(grouppquota.Printer) : |
---|
635 | gpq = self.getGroupPQuota(grouppquota.Group, printer) |
---|
636 | if gpq.Exists : |
---|
637 | gpquotas.append(gpq) |
---|
638 | return gpquotas |
---|
639 | |
---|
640 | def openConnection(pykotatool) : |
---|
641 | """Returns a connection handle to the appropriate Quota Storage Database.""" |
---|
642 | backendinfo = pykotatool.config.getStorageBackend() |
---|
643 | backend = backendinfo["storagebackend"] |
---|
644 | try : |
---|
645 | exec "from pykota.storages import %s as storagebackend" % backend.lower() |
---|
646 | except ImportError : |
---|
647 | raise PyKotaStorageError, _("Unsupported quota storage backend %s") % backend |
---|
648 | else : |
---|
649 | host = backendinfo["storageserver"] |
---|
650 | database = backendinfo["storagename"] |
---|
651 | admin = backendinfo["storageadmin"] or backendinfo["storageuser"] |
---|
652 | adminpw = backendinfo["storageadminpw"] or backendinfo["storageuserpw"] |
---|
653 | return storagebackend.Storage(pykotatool, host, database, admin, adminpw) |
---|