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