[695] | 1 | # PyKota |
---|
[1144] | 2 | # -*- coding: ISO-8859-15 -*- |
---|
[695] | 3 | # |
---|
[952] | 4 | # PyKota : Print Quotas for CUPS and LPRng |
---|
[695] | 5 | # |
---|
[1257] | 6 | # (c) 2003-2004 Jerome Alet <alet@librelogiciel.com> |
---|
[873] | 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. |
---|
[695] | 11 | # |
---|
[873] | 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. |
---|
[695] | 20 | # |
---|
| 21 | # $Id$ |
---|
| 22 | # |
---|
| 23 | # $Log$ |
---|
[1259] | 24 | # Revision 1.35 2004/01/08 16:33:27 jalet |
---|
| 25 | # Additionnal check to not create a circular printers group. |
---|
| 26 | # |
---|
[1258] | 27 | # Revision 1.34 2004/01/08 16:24:49 jalet |
---|
| 28 | # edpykota now supports adding printers to printer groups. |
---|
| 29 | # |
---|
[1257] | 30 | # Revision 1.33 2004/01/08 14:10:32 jalet |
---|
| 31 | # Copyright year changed. |
---|
| 32 | # |
---|
[1252] | 33 | # Revision 1.32 2004/01/06 16:02:57 jalet |
---|
| 34 | # This time printer groups caching works. |
---|
| 35 | # |
---|
[1250] | 36 | # Revision 1.31 2004/01/06 15:51:24 jalet |
---|
| 37 | # Fixed caching of printer groups |
---|
| 38 | # |
---|
[1249] | 39 | # Revision 1.30 2004/01/06 14:24:59 jalet |
---|
| 40 | # Printer groups should be cached now, if caching is enabled. |
---|
| 41 | # |
---|
[1240] | 42 | # Revision 1.29 2003/12/27 16:49:25 uid67467 |
---|
| 43 | # Should be ok now. |
---|
| 44 | # |
---|
[1219] | 45 | # Revision 1.28 2003/11/25 23:46:40 jalet |
---|
| 46 | # Don't try to verify if module name is valid, Python does this better than us. |
---|
| 47 | # |
---|
[1203] | 48 | # Revision 1.27 2003/11/23 19:01:36 jalet |
---|
| 49 | # Job price added to history |
---|
| 50 | # |
---|
[1200] | 51 | # Revision 1.26 2003/11/21 14:28:45 jalet |
---|
| 52 | # More complete job history. |
---|
| 53 | # |
---|
[1151] | 54 | # Revision 1.25 2003/10/08 21:12:27 jalet |
---|
| 55 | # Do not cache anymore entries which don't exist. |
---|
| 56 | # |
---|
[1148] | 57 | # Revision 1.24 2003/10/07 22:06:05 jalet |
---|
| 58 | # Preliminary code to disable job history |
---|
| 59 | # |
---|
[1144] | 60 | # Revision 1.23 2003/10/07 09:07:28 jalet |
---|
| 61 | # Character encoding added to please latest version of Python |
---|
| 62 | # |
---|
[1137] | 63 | # Revision 1.22 2003/10/06 13:12:27 jalet |
---|
| 64 | # More work on caching |
---|
| 65 | # |
---|
[1132] | 66 | # Revision 1.21 2003/10/03 09:02:20 jalet |
---|
| 67 | # Logs cache store actions too |
---|
| 68 | # |
---|
[1130] | 69 | # Revision 1.20 2003/10/02 20:23:18 jalet |
---|
| 70 | # Storage caching mechanism added. |
---|
| 71 | # |
---|
[1087] | 72 | # Revision 1.19 2003/07/16 21:53:07 jalet |
---|
| 73 | # Really big modifications wrt new configuration file's location and content. |
---|
| 74 | # |
---|
[1067] | 75 | # Revision 1.18 2003/07/07 08:33:18 jalet |
---|
| 76 | # Bug fix due to a typo in LDAP code |
---|
| 77 | # |
---|
[1062] | 78 | # Revision 1.17 2003/07/05 07:46:50 jalet |
---|
| 79 | # The previous bug fix was incomplete. |
---|
| 80 | # |
---|
[1043] | 81 | # Revision 1.16 2003/06/25 19:52:31 jalet |
---|
| 82 | # Should be ready for testing :-) |
---|
| 83 | # |
---|
[1042] | 84 | # Revision 1.15 2003/06/25 14:10:58 jalet |
---|
| 85 | # Exception raising for now. |
---|
| 86 | # |
---|
[1041] | 87 | # Revision 1.14 2003/06/25 14:10:01 jalet |
---|
| 88 | # Hey, it may work (edpykota --reset excepted) ! |
---|
| 89 | # |
---|
[1021] | 90 | # Revision 1.13 2003/06/10 16:37:54 jalet |
---|
| 91 | # Deletion of the second user which is not needed anymore. |
---|
| 92 | # Added a debug configuration field in /etc/pykota.conf |
---|
| 93 | # All queries can now be sent to the logger in debug mode, this will |
---|
| 94 | # greatly help improve performance when time for this will come. |
---|
| 95 | # |
---|
[952] | 96 | # Revision 1.12 2003/04/23 22:13:57 jalet |
---|
| 97 | # Preliminary support for LPRng added BUT STILL UNTESTED. |
---|
| 98 | # |
---|
[900] | 99 | # Revision 1.11 2003/04/10 21:47:20 jalet |
---|
| 100 | # Job history added. Upgrade script neutralized for now ! |
---|
| 101 | # |
---|
[873] | 102 | # Revision 1.10 2003/03/29 13:45:27 jalet |
---|
| 103 | # GPL paragraphs were incorrectly (from memory) copied into the sources. |
---|
| 104 | # Two README files were added. |
---|
| 105 | # Upgrade script for PostgreSQL pre 1.01 schema was added. |
---|
| 106 | # |
---|
[802] | 107 | # Revision 1.9 2003/02/17 22:55:01 jalet |
---|
| 108 | # More options can now be set per printer or globally : |
---|
| 109 | # |
---|
[873] | 110 | # admin |
---|
| 111 | # adminmail |
---|
| 112 | # gracedelay |
---|
| 113 | # requester |
---|
[802] | 114 | # |
---|
| 115 | # the printer option has priority when both are defined. |
---|
| 116 | # |
---|
[800] | 117 | # Revision 1.8 2003/02/17 22:05:50 jalet |
---|
| 118 | # Storage backend now supports admin and user passwords (untested) |
---|
| 119 | # |
---|
[791] | 120 | # Revision 1.7 2003/02/10 12:07:31 jalet |
---|
| 121 | # Now repykota should output the recorded total page number for each printer too. |
---|
| 122 | # |
---|
[773] | 123 | # Revision 1.6 2003/02/09 13:05:43 jalet |
---|
| 124 | # Internationalization continues... |
---|
| 125 | # |
---|
[768] | 126 | # Revision 1.5 2003/02/08 22:39:46 jalet |
---|
| 127 | # --reset command line option added |
---|
| 128 | # |
---|
[759] | 129 | # Revision 1.4 2003/02/08 09:59:59 jalet |
---|
| 130 | # Added preliminary base class for all storages |
---|
| 131 | # |
---|
[698] | 132 | # Revision 1.3 2003/02/05 22:10:29 jalet |
---|
| 133 | # Typos |
---|
| 134 | # |
---|
[697] | 135 | # Revision 1.2 2003/02/05 22:02:22 jalet |
---|
| 136 | # __import__ statement didn't work as expected |
---|
| 137 | # |
---|
[695] | 138 | # Revision 1.1 2003/02/05 21:28:17 jalet |
---|
| 139 | # Initial import into CVS |
---|
| 140 | # |
---|
| 141 | # |
---|
| 142 | # |
---|
| 143 | |
---|
| 144 | class PyKotaStorageError(Exception): |
---|
| 145 | """An exception for Quota Storage related stuff.""" |
---|
| 146 | def __init__(self, message = ""): |
---|
| 147 | self.message = message |
---|
| 148 | Exception.__init__(self, message) |
---|
| 149 | def __repr__(self): |
---|
| 150 | return self.message |
---|
| 151 | __str__ = __repr__ |
---|
[759] | 152 | |
---|
[1041] | 153 | class StorageObject : |
---|
| 154 | """Object present in the Quota Storage.""" |
---|
| 155 | def __init__(self, parent) : |
---|
| 156 | "Initialize minimal data.""" |
---|
| 157 | self.parent = parent |
---|
| 158 | self.ident = None |
---|
| 159 | self.Exists = 0 |
---|
| 160 | |
---|
| 161 | class StorageUser(StorageObject) : |
---|
| 162 | """User class.""" |
---|
| 163 | def __init__(self, parent, name) : |
---|
| 164 | StorageObject.__init__(self, parent) |
---|
| 165 | self.Name = name |
---|
| 166 | self.LimitBy = None |
---|
| 167 | self.AccountBalance = None |
---|
| 168 | self.LifeTimePaid = None |
---|
[1067] | 169 | self.Email = None |
---|
[1041] | 170 | |
---|
| 171 | def consumeAccountBalance(self, amount) : |
---|
| 172 | """Consumes an amount of money from the user's account balance.""" |
---|
| 173 | newbalance = float(self.AccountBalance or 0.0) - amount |
---|
| 174 | self.parent.writeUserAccountBalance(self, newbalance) |
---|
| 175 | self.AccountBalance = newbalance |
---|
| 176 | |
---|
| 177 | def setAccountBalance(self, balance, lifetimepaid) : |
---|
| 178 | """Sets the user's account balance in case he pays more money.""" |
---|
[1043] | 179 | self.parent.writeUserAccountBalance(self, balance, lifetimepaid) |
---|
| 180 | self.AccountBalance = balance |
---|
| 181 | self.LifeTimePaid = lifetimepaid |
---|
[1041] | 182 | |
---|
| 183 | def setLimitBy(self, limitby) : |
---|
| 184 | """Sets the user's limiting factor.""" |
---|
[1062] | 185 | try : |
---|
| 186 | limitby = limitby.lower() |
---|
| 187 | except AttributeError : |
---|
| 188 | limitby = "quota" |
---|
[1041] | 189 | if limitby in ["quota", "balance"] : |
---|
| 190 | self.parent.writeUserLimitBy(self, limitby) |
---|
| 191 | self.LimitBy = limitby |
---|
| 192 | |
---|
| 193 | def delete(self) : |
---|
| 194 | """Deletes an user from the Quota Storage.""" |
---|
| 195 | self.parent.beginTransaction() |
---|
| 196 | try : |
---|
| 197 | self.parent.deleteUser(self) |
---|
| 198 | except PyKotaStorageError, msg : |
---|
| 199 | self.parent.rollbackTransaction() |
---|
| 200 | raise PyKotaStorageError, msg |
---|
| 201 | else : |
---|
| 202 | self.parent.commitTransaction() |
---|
| 203 | |
---|
| 204 | class StorageGroup(StorageObject) : |
---|
| 205 | """User class.""" |
---|
| 206 | def __init__(self, parent, name) : |
---|
| 207 | StorageObject.__init__(self, parent) |
---|
| 208 | self.Name = name |
---|
| 209 | self.LimitBy = None |
---|
| 210 | self.AccountBalance = None |
---|
| 211 | self.LifeTimePaid = None |
---|
| 212 | |
---|
| 213 | def setLimitBy(self, limitby) : |
---|
| 214 | """Sets the user's limiting factor.""" |
---|
[1062] | 215 | try : |
---|
| 216 | limitby = limitby.lower() |
---|
| 217 | except AttributeError : |
---|
| 218 | limitby = "quota" |
---|
[1041] | 219 | if limitby in ["quota", "balance"] : |
---|
| 220 | self.parent.writeGroupLimitBy(self, limitby) |
---|
| 221 | self.LimitBy = limitby |
---|
| 222 | |
---|
| 223 | def delete(self) : |
---|
| 224 | """Deletes a group from the Quota Storage.""" |
---|
| 225 | self.parent.beginTransaction() |
---|
| 226 | try : |
---|
| 227 | self.parent.deleteGroup(self) |
---|
| 228 | except PyKotaStorageError, msg : |
---|
| 229 | self.parent.rollbackTransaction() |
---|
| 230 | raise PyKotaStorageError, msg |
---|
| 231 | else : |
---|
| 232 | self.parent.commitTransaction() |
---|
| 233 | |
---|
| 234 | class StoragePrinter(StorageObject) : |
---|
| 235 | """Printer class.""" |
---|
| 236 | def __init__(self, parent, name) : |
---|
| 237 | StorageObject.__init__(self, parent) |
---|
| 238 | self.Name = name |
---|
| 239 | self.PricePerPage = None |
---|
| 240 | self.PricePerJob = None |
---|
| 241 | self.LastJob = None |
---|
| 242 | |
---|
[1203] | 243 | def addJobToHistory(self, jobid, user, pagecounter, action, jobsize=None, jobprice=None, filename=None, title=None, copies=None, options=None) : |
---|
[1041] | 244 | """Adds a job to the printer's history.""" |
---|
[1203] | 245 | self.parent.writeJobNew(self, user, jobid, pagecounter, action, jobsize, jobprice, filename, title, copies, options) |
---|
[1041] | 246 | # TODO : update LastJob object ? Probably not needed. |
---|
| 247 | |
---|
[1258] | 248 | def addPrinterToGroup(self, printer) : |
---|
| 249 | """Adds a printer to a printer group.""" |
---|
[1259] | 250 | if (printer not in self.parent.getParentPrinters(self)) and (printer.ident != self.ident) : |
---|
[1258] | 251 | self.parent.writePrinterToGroup(self, printer) |
---|
| 252 | |
---|
[1041] | 253 | def setPrices(self, priceperpage = None, priceperjob = None) : |
---|
| 254 | """Sets the printer's prices.""" |
---|
| 255 | if priceperpage is None : |
---|
| 256 | priceperpage = self.PricePerPage |
---|
| 257 | else : |
---|
| 258 | self.PricePerPage = float(priceperpage) |
---|
| 259 | if priceperjob is None : |
---|
| 260 | priceperjob = self.PricePerJob |
---|
| 261 | else : |
---|
| 262 | self.PricePerJob = float(priceperjob) |
---|
| 263 | self.parent.writePrinterPrices(self) |
---|
| 264 | |
---|
| 265 | class StorageUserPQuota(StorageObject) : |
---|
| 266 | """User Print Quota class.""" |
---|
| 267 | def __init__(self, parent, user, printer) : |
---|
| 268 | StorageObject.__init__(self, parent) |
---|
| 269 | self.User = user |
---|
| 270 | self.Printer = printer |
---|
| 271 | self.PageCounter = None |
---|
| 272 | self.LifePageCounter = None |
---|
| 273 | self.SoftLimit = None |
---|
| 274 | self.HardLimit = None |
---|
| 275 | self.DateLimit = None |
---|
[1240] | 276 | self.ParentPrintersUserPQuota = (user.Exists and printer.Exists and parent.getParentPrintersUserPQuota(self)) or [] |
---|
[1041] | 277 | |
---|
| 278 | def setDateLimit(self, datelimit) : |
---|
| 279 | """Sets the date limit for this quota.""" |
---|
| 280 | date = "%04i-%02i-%02i %02i:%02i:%02i" % (datelimit.year, datelimit.month, datelimit.day, datelimit.hour, datelimit.minute, datelimit.second) |
---|
| 281 | self.parent.writeUserPQuotaDateLimit(self, date) |
---|
| 282 | self.DateLimit = date |
---|
| 283 | |
---|
| 284 | def setLimits(self, softlimit, hardlimit) : |
---|
| 285 | """Sets the soft and hard limit for this quota.""" |
---|
| 286 | self.parent.writeUserPQuotaLimits(self, softlimit, hardlimit) |
---|
| 287 | self.SoftLimit = softlimit |
---|
| 288 | self.HardLimit = hardlimit |
---|
| 289 | |
---|
| 290 | def reset(self) : |
---|
| 291 | """Resets page counter to 0.""" |
---|
| 292 | self.parent.writeUserPQuotaPagesCounters(self, 0, int(self.LifePageCounter or 0)) |
---|
| 293 | self.PageCounter = 0 |
---|
| 294 | |
---|
| 295 | def increasePagesUsage(self, nbpages) : |
---|
| 296 | """Increase the value of used pages and money.""" |
---|
[1240] | 297 | jobprice = (float(self.Printer.PricePerPage or 0.0) * nbpages) + float(self.Printer.PricePerJob or 0.0) |
---|
| 298 | self.parent.beginTransaction() |
---|
| 299 | try : |
---|
| 300 | if nbpages : |
---|
| 301 | self.User.consumeAccountBalance(jobprice) |
---|
| 302 | for upq in [ self ] + self.ParentPrintersUserPQuota : |
---|
| 303 | newpagecounter = int(upq.PageCounter or 0) + nbpages |
---|
| 304 | newlifepagecounter = int(upq.LifePageCounter or 0) + nbpages |
---|
| 305 | self.parent.writeUserPQuotaPagesCounters(upq, newpagecounter, newlifepagecounter) |
---|
| 306 | upq.PageCounter = newpagecounter |
---|
| 307 | upq.LifePageCounter = newlifepagecounter |
---|
| 308 | except PyKotaStorageError, msg : |
---|
| 309 | self.parent.rollbackTransaction() |
---|
| 310 | raise PyKotaStorageError, msg |
---|
| 311 | else : |
---|
| 312 | self.parent.commitTransaction() |
---|
[1041] | 313 | |
---|
| 314 | class StorageGroupPQuota(StorageObject) : |
---|
| 315 | """Group Print Quota class.""" |
---|
| 316 | def __init__(self, parent, group, printer) : |
---|
| 317 | StorageObject.__init__(self, parent) |
---|
| 318 | self.Group = group |
---|
| 319 | self.Printer = printer |
---|
| 320 | self.PageCounter = None |
---|
| 321 | self.LifePageCounter = None |
---|
| 322 | self.SoftLimit = None |
---|
| 323 | self.HardLimit = None |
---|
| 324 | self.DateLimit = None |
---|
| 325 | |
---|
| 326 | def setDateLimit(self, datelimit) : |
---|
| 327 | """Sets the date limit for this quota.""" |
---|
| 328 | date = "%04i-%02i-%02i %02i:%02i:%02i" % (datelimit.year, datelimit.month, datelimit.day, datelimit.hour, datelimit.minute, datelimit.second) |
---|
| 329 | self.parent.writeGroupPQuotaDateLimit(self, date) |
---|
| 330 | self.DateLimit = date |
---|
| 331 | |
---|
| 332 | def setLimits(self, softlimit, hardlimit) : |
---|
| 333 | """Sets the soft and hard limit for this quota.""" |
---|
| 334 | self.parent.writeGroupPQuotaLimits(self, softlimit, hardlimit) |
---|
| 335 | self.SoftLimit = softlimit |
---|
| 336 | self.HardLimit = hardlimit |
---|
| 337 | |
---|
| 338 | class StorageLastJob(StorageObject) : |
---|
| 339 | """Printer's Last Job class.""" |
---|
| 340 | def __init__(self, parent, printer) : |
---|
| 341 | StorageObject.__init__(self, parent) |
---|
| 342 | self.Printer = printer |
---|
| 343 | self.JobId = None |
---|
| 344 | self.User = None |
---|
| 345 | self.PrinterPageCounter = None |
---|
| 346 | self.JobSize = None |
---|
| 347 | self.JobAction = None |
---|
| 348 | self.JobDate = None |
---|
[1203] | 349 | self.JobPrice = None |
---|
| 350 | self.JobFileName = None |
---|
| 351 | self.JobTitle = None |
---|
| 352 | self.JobCopies = None |
---|
| 353 | self.JobOptions = None |
---|
[1041] | 354 | |
---|
| 355 | def setSize(self, jobsize) : |
---|
| 356 | """Sets the last job's size.""" |
---|
[1203] | 357 | jobprice = (float(self.Printer.PricePerPage or 0.0) * jobsize) + float(self.Printer.PricePerJob or 0.0) |
---|
| 358 | self.parent.writeLastJobSize(self, jobsize, jobprice) |
---|
[1041] | 359 | self.JobSize = jobsize |
---|
[1203] | 360 | self.JobPrice = jobprice |
---|
[1041] | 361 | |
---|
[1130] | 362 | class BaseStorage : |
---|
| 363 | def __init__(self, pykotatool) : |
---|
| 364 | """Opens the LDAP connection.""" |
---|
| 365 | # raise PyKotaStorageError, "Sorry, the LDAP backend for PyKota is not yet implemented !" |
---|
| 366 | self.closed = 1 |
---|
| 367 | self.tool = pykotatool |
---|
| 368 | self.usecache = pykotatool.config.getCaching() |
---|
[1148] | 369 | self.disablehistory = pykotatool.config.getDisableHistory() |
---|
[1130] | 370 | if self.usecache : |
---|
| 371 | self.tool.logdebug("Caching enabled.") |
---|
[1252] | 372 | self.caches = { "USERS" : {}, "GROUPS" : {}, "PRINTERS" : {}, "USERPQUOTAS" : {}, "GROUPPQUOTAS" : {}, "JOBS" : {}, "LASTJOBS" : {} } |
---|
[1130] | 373 | |
---|
[1240] | 374 | def close(self) : |
---|
| 375 | """Must be overriden in children classes.""" |
---|
| 376 | raise RuntimeError, "BaseStorage.close() must be overriden !" |
---|
| 377 | |
---|
[1130] | 378 | def __del__(self) : |
---|
| 379 | """Ensures that the database connection is closed.""" |
---|
| 380 | self.close() |
---|
| 381 | |
---|
| 382 | def getFromCache(self, cachetype, key) : |
---|
| 383 | """Tries to extract something from the cache.""" |
---|
| 384 | if self.usecache : |
---|
| 385 | entry = self.caches[cachetype].get(key) |
---|
| 386 | if entry is not None : |
---|
| 387 | self.tool.logdebug("Cache hit (%s->%s)" % (cachetype, key)) |
---|
| 388 | else : |
---|
| 389 | self.tool.logdebug("Cache miss (%s->%s)" % (cachetype, key)) |
---|
| 390 | return entry |
---|
| 391 | |
---|
| 392 | def cacheEntry(self, cachetype, key, value) : |
---|
| 393 | """Puts an entry in the cache.""" |
---|
[1151] | 394 | if self.usecache and getattr(value, "Exists", 0) : |
---|
[1130] | 395 | self.caches[cachetype][key] = value |
---|
[1132] | 396 | self.tool.logdebug("Cache store (%s->%s)" % (cachetype, key)) |
---|
[1130] | 397 | |
---|
| 398 | def getUser(self, username) : |
---|
| 399 | """Returns the user from cache.""" |
---|
| 400 | user = self.getFromCache("USERS", username) |
---|
| 401 | if user is None : |
---|
| 402 | user = self.getUserFromBackend(username) |
---|
| 403 | self.cacheEntry("USERS", username, user) |
---|
| 404 | return user |
---|
| 405 | |
---|
| 406 | def getGroup(self, groupname) : |
---|
| 407 | """Returns the group from cache.""" |
---|
| 408 | group = self.getFromCache("GROUPS", groupname) |
---|
| 409 | if group is None : |
---|
| 410 | group = self.getGroupFromBackend(groupname) |
---|
| 411 | self.cacheEntry("GROUPS", groupname, group) |
---|
| 412 | return group |
---|
| 413 | |
---|
| 414 | def getPrinter(self, printername) : |
---|
| 415 | """Returns the printer from cache.""" |
---|
| 416 | printer = self.getFromCache("PRINTERS", printername) |
---|
| 417 | if printer is None : |
---|
| 418 | printer = self.getPrinterFromBackend(printername) |
---|
| 419 | self.cacheEntry("PRINTERS", printername, printer) |
---|
| 420 | return printer |
---|
| 421 | |
---|
| 422 | def getUserPQuota(self, user, printer) : |
---|
| 423 | """Returns the user quota information from cache.""" |
---|
| 424 | useratprinter = "%s@%s" % (user.Name, printer.Name) |
---|
| 425 | upquota = self.getFromCache("USERPQUOTAS", useratprinter) |
---|
| 426 | if upquota is None : |
---|
| 427 | upquota = self.getUserPQuotaFromBackend(user, printer) |
---|
| 428 | self.cacheEntry("USERPQUOTAS", useratprinter, upquota) |
---|
| 429 | return upquota |
---|
| 430 | |
---|
| 431 | def getGroupPQuota(self, group, printer) : |
---|
| 432 | """Returns the group quota information from cache.""" |
---|
| 433 | groupatprinter = "%s@%s" % (group.Name, printer.Name) |
---|
| 434 | gpquota = self.getFromCache("GROUPPQUOTAS", groupatprinter) |
---|
| 435 | if gpquota is None : |
---|
| 436 | gpquota = self.getGroupPQuotaFromBackend(group, printer) |
---|
| 437 | self.cacheEntry("GROUPPQUOTAS", groupatprinter, gpquota) |
---|
| 438 | return gpquota |
---|
| 439 | |
---|
| 440 | def getPrinterLastJob(self, printer) : |
---|
| 441 | """Extracts last job information for a given printer from cache.""" |
---|
| 442 | lastjob = self.getFromCache("LASTJOBS", printer.Name) |
---|
| 443 | if lastjob is None : |
---|
| 444 | lastjob = self.getPrinterLastJobFromBackend(printer) |
---|
| 445 | self.cacheEntry("LASTJOBS", printer.Name, lastjob) |
---|
| 446 | return lastjob |
---|
| 447 | |
---|
[1249] | 448 | def getParentPrinters(self, printer) : |
---|
| 449 | """Extracts parent printers information for a given printer from cache.""" |
---|
[1252] | 450 | if self.usecache : |
---|
| 451 | if not hasattr(printer, "Parents") : |
---|
| 452 | self.tool.logdebug("Cache miss (%s->Parents)" % printer.Name) |
---|
| 453 | printer.Parents = self.getParentPrintersFromBackend(printer) |
---|
| 454 | self.tool.logdebug("Cache store (%s->Parents)" % printer.Name) |
---|
| 455 | else : |
---|
| 456 | self.tool.logdebug("Cache hit (%s->Parents)" % printer.Name) |
---|
| 457 | else : |
---|
| 458 | printer.Parents = self.getParentPrintersFromBackend(printer) |
---|
| 459 | return printer.Parents |
---|
[1249] | 460 | |
---|
[1137] | 461 | def getGroupMembers(self, group) : |
---|
| 462 | """Returns the group's members list from in-group cache.""" |
---|
| 463 | if self.usecache : |
---|
| 464 | if not hasattr(group, "Members") : |
---|
| 465 | self.tool.logdebug("Cache miss (%s->Members)" % group.Name) |
---|
| 466 | group.Members = self.getGroupMembersFromBackend(group) |
---|
| 467 | self.tool.logdebug("Cache store (%s->Members)" % group.Name) |
---|
| 468 | else : |
---|
| 469 | self.tool.logdebug("Cache hit (%s->Members)" % group.Name) |
---|
| 470 | else : |
---|
| 471 | group.Members = self.getGroupMembersFromBackend(group) |
---|
| 472 | return group.Members |
---|
| 473 | |
---|
| 474 | def getUserGroups(self, user) : |
---|
| 475 | """Returns the user's groups list from in-user cache.""" |
---|
| 476 | if self.usecache : |
---|
| 477 | if not hasattr(user, "Groups") : |
---|
| 478 | self.tool.logdebug("Cache miss (%s->Groups)" % user.Name) |
---|
| 479 | user.Groups = self.getUserGroupsFromBackend(user) |
---|
| 480 | self.tool.logdebug("Cache store (%s->Groups)" % user.Name) |
---|
| 481 | else : |
---|
| 482 | self.tool.logdebug("Cache hit (%s->Groups)" % user.Name) |
---|
| 483 | else : |
---|
| 484 | user.Groups = self.getUserGroupsFromBackend(user) |
---|
| 485 | return user.Groups |
---|
| 486 | |
---|
[1240] | 487 | def getParentPrintersUserPQuota(self, userpquota) : |
---|
| 488 | """Returns all user print quota on the printer and its parents.""" |
---|
| 489 | upquotas = [ ] |
---|
| 490 | for printer in self.getParentPrinters(userpquota.Printer) : |
---|
| 491 | upquotas.append(self.getUserPQuota(userpquota.User, printer)) |
---|
| 492 | return upquotas |
---|
| 493 | |
---|
[1021] | 494 | def openConnection(pykotatool) : |
---|
[695] | 495 | """Returns a connection handle to the appropriate Quota Storage Database.""" |
---|
[1021] | 496 | backendinfo = pykotatool.config.getStorageBackend() |
---|
[800] | 497 | backend = backendinfo["storagebackend"] |
---|
[695] | 498 | try : |
---|
[1219] | 499 | exec "from pykota.storages import %s as storagebackend" % backend.lower() |
---|
[695] | 500 | except ImportError : |
---|
[773] | 501 | raise PyKotaStorageError, _("Unsupported quota storage backend %s") % backend |
---|
[695] | 502 | else : |
---|
[800] | 503 | host = backendinfo["storageserver"] |
---|
| 504 | database = backendinfo["storagename"] |
---|
[1087] | 505 | admin = backendinfo["storageadmin"] or backendinfo["storageuser"] |
---|
| 506 | adminpw = backendinfo["storageadminpw"] or backendinfo["storageuserpw"] |
---|
[1240] | 507 | return storagebackend.Storage(pykotatool, host, database, admin, adminpw) |
---|