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