Changeset 3413 for pykota/trunk/pykota/config.py
- Timestamp:
- 09/27/08 22:02:37 (16 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
pykota/trunk/pykota/config.py
r3411 r3413 8 8 # the Free Software Foundation, either version 3 of the License, or 9 9 # (at your option) any later version. 10 # 10 # 11 11 # This program is distributed in the hope that it will be useful, 12 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 14 # GNU General Public License for more details. 15 # 15 # 16 16 # You should have received a copy of the GNU General Public License 17 17 # along with this program. If not, see <http://www.gnu.org/licenses/>. … … 29 29 30 30 from pykota.utils import unicodeToDatabase 31 from pykota.errors import PyKotaConfigError 31 from pykota.errors import PyKotaConfigError 32 32 33 33 class PyKotaConfig : … … 43 43 if not os.path.isfile(self.adminfilename) : 44 44 raise PyKotaConfigError, _("Configuration file %s not found.") % self.adminfilename 45 if os.access(self.adminfilename, os.R_OK) : 45 if os.access(self.adminfilename, os.R_OK) : 46 46 self.isAdmin = 1 47 47 self.config = ConfigParser.ConfigParser() … … 53 53 except (ConfigParser.NoSectionError, ConfigParser.NoOptionError) : 54 54 self.config_charset = "UTF-8" 55 56 def isTrue(self, option) : 55 56 def isTrue(self, option) : 57 57 """Returns True if option is set to true, else False.""" 58 58 if (option is not None) and (option.strip().upper() in ['Y', 'YES', '1', 'ON', 'T', 'TRUE']) : 59 59 return True 60 else : 60 else : 61 61 return False 62 63 def isFalse(self, option) : 62 63 def isFalse(self, option) : 64 64 """Returns True if option is set to false, else False.""" 65 65 if (option is not None) and (option.strip().upper() in ['N', 'NO', '0', 'OFF', 'F', 'FALSE']) : 66 66 return True 67 else : 67 else : 68 68 return False 69 70 def getPrinterNames(self) : 69 70 def getPrinterNames(self) : 71 71 """Returns the list of configured printers, i.e. all sections names minus 'global'.""" 72 72 return [pname for pname in self.config.sections() if pname != "global"] 73 74 def getGlobalOption(self, option, ignore=False) : 73 74 def getGlobalOption(self, option, ignore=False) : 75 75 """Returns an option from the global section, or raises a PyKotaConfigError if ignore is not set, else returns None.""" 76 76 try : 77 77 return self.config.get("global", option, raw=1).decode(self.config_charset) 78 except (ConfigParser.NoSectionError, ConfigParser.NoOptionError) : 78 except (ConfigParser.NoSectionError, ConfigParser.NoOptionError) : 79 79 if ignore : 80 80 return None 81 81 else : 82 82 raise PyKotaConfigError, _("Option %s not found in section global of %s") % (option, self.filename) 83 84 def getPrinterOption(self, printername, option) : 83 84 def getPrinterOption(self, printername, option) : 85 85 """Returns an option from the printer section, or the global section, or raises a PyKotaConfigError.""" 86 86 globaloption = self.getGlobalOption(option, ignore=True) 87 87 try : 88 88 return self.config.get(printername, option, raw=1).decode(self.config_charset) 89 except (ConfigParser.NoSectionError, ConfigParser.NoOptionError) : 89 except (ConfigParser.NoSectionError, ConfigParser.NoOptionError) : 90 90 if globaloption is not None : 91 91 return globaloption 92 92 else : 93 93 raise PyKotaConfigError, _("Option %s not found in section %s of %s") % (option, printername, self.filename) 94 95 def getStorageBackend(self) : 96 """Returns the storage backend information as a Python mapping.""" 94 95 def getStorageBackend(self) : 96 """Returns the storage backend information as a Python mapping.""" 97 97 backendinfo = {} 98 98 backend = self.getGlobalOption("storagebackend").lower() … … 102 102 backendinfo["storagename"] = self.getGlobalOption("storagename") 103 103 for option in ["storageserver", "storageuser", "storageuserpw"] : 104 backendinfo[option] = None 104 backendinfo[option] = None 105 105 else : 106 106 issqlite = False … … 108 108 backendinfo[option] = self.getGlobalOption(option) 109 109 backendinfo["storageuserpw"] = self.getGlobalOption("storageuserpw", ignore=True) # password is optional 110 110 111 111 backendinfo["storageadmin"] = None 112 112 backendinfo["storageadminpw"] = None … … 117 117 try : 118 118 backendinfo["storageadmin"] = adminconf.get("global", "storageadmin", raw=1).decode(self.config_charset) 119 except (ConfigParser.NoSectionError, ConfigParser.NoOptionError) : 119 except (ConfigParser.NoSectionError, ConfigParser.NoOptionError) : 120 120 if not issqlite : 121 121 raise PyKotaConfigError, _("Option %s not found in section global of %s") % ("storageadmin", self.adminfilename) 122 122 try : 123 123 backendinfo["storageadminpw"] = adminconf.get("global", "storageadminpw", raw=1).decode(self.config_charset) 124 except (ConfigParser.NoSectionError, ConfigParser.NoOptionError) : 124 except (ConfigParser.NoSectionError, ConfigParser.NoOptionError) : 125 125 pass # Password is optional 126 # Now try to overwrite the storagebackend, storageserver 126 # Now try to overwrite the storagebackend, storageserver 127 127 # and storagename. This allows admins to use the master LDAP 128 128 # server directly and users to use the replicas transparently. … … 140 140 pass 141 141 return backendinfo 142 143 def getLDAPInfo(self) : 144 """Returns some hints for the LDAP backend.""" 142 143 def getLDAPInfo(self) : 144 """Returns some hints for the LDAP backend.""" 145 145 ldapinfo = {} 146 146 for option in [ "userbase", "userrdn", \ … … 157 157 if ldapinfo[field].lower().startswith('attach(') : 158 158 ldapinfo[field] = ldapinfo[field][7:-1] 159 160 # should we use TLS, by default (if unset) value is NO 159 160 # should we use TLS, by default (if unset) value is NO 161 161 ldapinfo["ldaptls"] = self.isTrue(self.getGlobalOption("ldaptls", ignore=True)) 162 162 ldapinfo["cacert"] = self.getGlobalOption("cacert", ignore=True) 163 163 if ldapinfo["cacert"] : 164 164 ldapinfo["cacert"] = ldapinfo["cacert"].strip().encode(sys.getfilesystemencoding(), "replace") 165 if ldapinfo["ldaptls"] : 165 if ldapinfo["ldaptls"] : 166 166 if not os.access(ldapinfo["cacert"] or "", os.R_OK) : 167 167 raise PyKotaConfigError, _("Option ldaptls is set, but certificate %s is not readable.") % repr(ldapinfo["cacert"]) 168 168 return ldapinfo 169 170 def getLoggingBackend(self) : 169 170 def getLoggingBackend(self) : 171 171 """Returns the logging backend information.""" 172 validloggers = [ "stderr", "system" ] 172 validloggers = [ "stderr", "system" ] 173 173 try : 174 174 logger = self.getGlobalOption("logger").lower() 175 except PyKotaConfigError : 175 except PyKotaConfigError : 176 176 logger = "system" 177 if logger not in validloggers : 177 if logger not in validloggers : 178 178 raise PyKotaConfigError, _("Option logger only supports values in %s") % str(validloggers) 179 return logger 180 179 return logger 180 181 181 def getLogoURL(self) : 182 182 """Returns the URL to use for the logo in the CGI scripts.""" 183 183 url = self.getGlobalOption("logourl", ignore=True) or \ 184 184 "http://www.pykota.com/pykota.png" 185 return url.strip() 186 185 return url.strip() 186 187 187 def getLogoLink(self) : 188 188 """Returns the URL to go to when the user clicks on the logo in the CGI scripts.""" 189 189 url = self.getGlobalOption("logolink", ignore=True) or \ 190 190 "http://www.pykota.com/" 191 return url.strip() 192 193 def getPreAccounterBackend(self, printername) : 191 return url.strip() 192 193 def getPreAccounterBackend(self, printername) : 194 194 """Returns the preaccounter backend to use for a given printer.""" 195 validaccounters = [ "software", "ink" ] 195 validaccounters = [ "software", "ink" ] 196 196 try : 197 197 fullaccounter = self.getPrinterOption(printername, "preaccounter").strip() 198 except PyKotaConfigError : 198 except PyKotaConfigError : 199 199 return ("software", "") 200 else : 200 else : 201 201 flower = fullaccounter.lower() 202 202 for vac in validaccounters : 203 if flower.startswith(vac) : 203 if flower.startswith(vac) : 204 204 try : 205 205 (accounter, args) = [x.strip() for x in fullaccounter.split('(', 1)] 206 except ValueError : 206 except ValueError : 207 207 raise PyKotaConfigError, _("Invalid preaccounter %s for printer %s") % (fullaccounter, printername) 208 208 if args.endswith(')') : 209 209 args = args[:-1].strip() 210 if (vac == "ink") and not args : 210 if (vac == "ink") and not args : 211 211 raise PyKotaConfigError, _("Invalid preaccounter %s for printer %s") % (fullaccounter, printername) 212 212 return (vac, args) 213 213 raise PyKotaConfigError, _("Option preaccounter in section %s only supports values in %s") % (printername, str(validaccounters)) 214 215 def getAccounterBackend(self, printername) : 214 215 def getAccounterBackend(self, printername) : 216 216 """Returns the accounter backend to use for a given printer.""" 217 validaccounters = [ "hardware", "software", "ink" ] 217 validaccounters = [ "hardware", "software", "ink" ] 218 218 try : 219 219 fullaccounter = self.getPrinterOption(printername, "accounter").strip() 220 except PyKotaConfigError : 220 except PyKotaConfigError : 221 221 return ("software", "") 222 else : 222 else : 223 223 flower = fullaccounter.lower() 224 224 for vac in validaccounters : 225 if flower.startswith(vac) : 225 if flower.startswith(vac) : 226 226 try : 227 227 (accounter, args) = [x.strip() for x in fullaccounter.split('(', 1)] 228 except ValueError : 228 except ValueError : 229 229 raise PyKotaConfigError, _("Invalid accounter %s for printer %s") % (fullaccounter, printername) 230 230 if args.endswith(')') : … … 234 234 return (vac, args) 235 235 raise PyKotaConfigError, _("Option accounter in section %s only supports values in %s") % (printername, str(validaccounters)) 236 237 def getPreHook(self, printername) : 236 237 def getPreHook(self, printername) : 238 238 """Returns the prehook command line to launch, or None if unset.""" 239 239 try : 240 240 return self.getPrinterOption(printername, "prehook").strip() 241 except PyKotaConfigError : 241 except PyKotaConfigError : 242 242 return # No command to launch in the pre-hook 243 244 def getPostHook(self, printername) : 243 244 def getPostHook(self, printername) : 245 245 """Returns the posthook command line to launch, or None if unset.""" 246 246 try : 247 247 return self.getPrinterOption(printername, "posthook").strip() 248 except PyKotaConfigError : 248 except PyKotaConfigError : 249 249 return # No command to launch in the post-hook 250 251 def getStripTitle(self, printername) : 250 251 def getStripTitle(self, printername) : 252 252 """Returns the striptitle directive's content, or None if unset.""" 253 253 try : 254 254 return self.getPrinterOption(printername, "striptitle").strip() 255 except PyKotaConfigError : 255 except PyKotaConfigError : 256 256 return # No prefix to strip off 257 258 def getAskConfirmation(self, printername) : 257 258 def getAskConfirmation(self, printername) : 259 259 """Returns the askconfirmation directive's content, or None if unset.""" 260 260 try : 261 261 return self.getPrinterOption(printername, "askconfirmation").strip() 262 except PyKotaConfigError : 262 except PyKotaConfigError : 263 263 return # No overwriting will be done 264 265 def getOverwriteJobTicket(self, printername) : 264 265 def getOverwriteJobTicket(self, printername) : 266 266 """Returns the overwrite_jobticket directive's content, or None if unset.""" 267 267 try : 268 268 return self.getPrinterOption(printername, "overwrite_jobticket").strip() 269 except PyKotaConfigError : 269 except PyKotaConfigError : 270 270 return # No overwriting will be done 271 272 def getUnknownBillingCode(self, printername) : 271 272 def getUnknownBillingCode(self, printername) : 273 273 """Returns the unknown_billingcode directive's content, or the default value if unset.""" 274 274 validvalues = [ "CREATE", "DENY" ] 275 275 try : 276 276 fullvalue = self.getPrinterOption(printername, "unknown_billingcode") 277 except PyKotaConfigError : 277 except PyKotaConfigError : 278 278 return ("CREATE", None) 279 else : 279 else : 280 280 try : 281 281 value = [x.strip() for x in fullvalue.split('(', 1)] 282 except ValueError : 282 except ValueError : 283 283 raise PyKotaConfigError, _("Invalid unknown_billingcode directive %s for printer %s") % (fullvalue, printername) 284 if len(value) == 1 : 284 if len(value) == 1 : 285 285 value.append("") 286 (value, args) = value 286 (value, args) = value 287 287 if args.endswith(')') : 288 288 args = args[:-1] 289 value = value.upper() 289 value = value.upper() 290 290 if (value == "DENY") and not args : 291 291 return ("DENY", None) … … 293 293 raise PyKotaConfigError, _("Directive unknown_billingcode in section %s only supports values in %s") % (printername, str(validvalues)) 294 294 return (value, args) 295 296 def getPrinterEnforcement(self, printername) : 295 296 def getPrinterEnforcement(self, printername) : 297 297 """Returns if quota enforcement should be strict or laxist for the current printer.""" 298 validenforcements = [ "STRICT", "LAXIST" ] 298 validenforcements = [ "STRICT", "LAXIST" ] 299 299 try : 300 300 enforcement = self.getPrinterOption(printername, "enforcement") 301 except PyKotaConfigError : 301 except PyKotaConfigError : 302 302 return "LAXIST" 303 else : 303 else : 304 304 enforcement = enforcement.upper() 305 305 if enforcement not in validenforcements : 306 306 raise PyKotaConfigError, _("Option enforcement in section %s only supports values in %s") % (printername, str(validenforcements)) 307 return enforcement 308 309 def getPrinterOnBackendError(self, printername) : 307 return enforcement 308 309 def getPrinterOnBackendError(self, printername) : 310 310 """Returns what must be done whenever the real CUPS backend fails.""" 311 validactions = [ "CHARGE", "NOCHARGE" ] 311 validactions = [ "CHARGE", "NOCHARGE" ] 312 312 try : 313 313 action = self.getPrinterOption(printername, "onbackenderror") 314 except PyKotaConfigError : 314 except PyKotaConfigError : 315 315 return ["NOCHARGE"] 316 else : 316 else : 317 317 action = action.upper().split(",") 318 318 error = False … … 322 322 try : 323 323 (num, delay) = [int(p) for p in act[6:].split(":", 2)] 324 except ValueError : 324 except ValueError : 325 325 error = True 326 else : 326 else : 327 327 error = True 328 328 if error : 329 329 raise PyKotaConfigError, _("Option onbackenderror in section %s only supports values 'charge', 'nocharge', and 'retry:num:delay'") % printername 330 return action 331 332 def getPrinterOnAccounterError(self, printername) : 330 return action 331 332 def getPrinterOnAccounterError(self, printername) : 333 333 """Returns what must be done whenever the accounter fails.""" 334 validactions = [ "CONTINUE", "STOP" ] 334 validactions = [ "CONTINUE", "STOP" ] 335 335 try : 336 336 action = self.getPrinterOption(printername, "onaccountererror") 337 except PyKotaConfigError : 337 except PyKotaConfigError : 338 338 return "STOP" 339 else : 339 else : 340 340 action = action.upper() 341 341 if action not in validactions : 342 342 raise PyKotaConfigError, _("Option onaccountererror in section %s only supports values in %s") % (printername, str(validactions)) 343 return action 344 345 def getPrinterPolicy(self, printername) : 343 return action 344 345 def getPrinterPolicy(self, printername) : 346 346 """Returns the default policy for the current printer.""" 347 validpolicies = [ "ALLOW", "DENY", "EXTERNAL" ] 347 validpolicies = [ "ALLOW", "DENY", "EXTERNAL" ] 348 348 try : 349 349 fullpolicy = self.getPrinterOption(printername, "policy") 350 except PyKotaConfigError : 350 except PyKotaConfigError : 351 351 return ("DENY", None) 352 else : 352 else : 353 353 try : 354 354 policy = [x.strip() for x in fullpolicy.split('(', 1)] 355 except ValueError : 355 except ValueError : 356 356 raise PyKotaConfigError, _("Invalid policy %s for printer %s") % (fullpolicy, printername) 357 if len(policy) == 1 : 357 if len(policy) == 1 : 358 358 policy.append("") 359 (policy, args) = policy 359 (policy, args) = policy 360 360 if args.endswith(')') : 361 361 args = args[:-1] 362 policy = policy.upper() 362 policy = policy.upper() 363 363 if (policy == "EXTERNAL") and not args : 364 364 raise PyKotaConfigError, _("Invalid policy %s for printer %s") % (fullpolicy, printername) … … 366 366 raise PyKotaConfigError, _("Option policy in section %s only supports values in %s") % (printername, str(validpolicies)) 367 367 return (policy, args) 368 369 def getCrashRecipient(self) : 368 369 def getCrashRecipient(self) : 370 370 """Returns the email address of the software crash messages recipient.""" 371 371 try : 372 372 return self.getGlobalOption("crashrecipient") 373 except : 373 except : 374 374 return 375 376 def getSMTPServer(self) : 375 376 def getSMTPServer(self) : 377 377 """Returns the SMTP server to use to send messages to users.""" 378 378 try : 379 379 return self.getGlobalOption("smtpserver") 380 except PyKotaConfigError : 380 except PyKotaConfigError : 381 381 return "localhost" 382 383 def getMailDomain(self) : 382 383 def getMailDomain(self) : 384 384 """Returns the mail domain to use to send messages to users.""" 385 385 try : 386 386 return self.getGlobalOption("maildomain") 387 except PyKotaConfigError : 388 return 389 390 def getAdminMail(self, printername) : 387 except PyKotaConfigError : 388 return 389 390 def getAdminMail(self, printername) : 391 391 """Returns the Email address of the Print Quota Administrator.""" 392 392 try : 393 393 return self.getPrinterOption(printername, "adminmail") 394 except PyKotaConfigError : 394 except PyKotaConfigError : 395 395 return "root@localhost" 396 397 def getAdmin(self, printername) : 396 397 def getAdmin(self, printername) : 398 398 """Returns the full name of the Print Quota Administrator.""" 399 399 try : 400 400 return self.getPrinterOption(printername, "admin") 401 except PyKotaConfigError : 401 except PyKotaConfigError : 402 402 return "root" 403 404 def getMailTo(self, printername) : 403 404 def getMailTo(self, printername) : 405 405 """Returns the recipient of email messages.""" 406 406 validmailtos = [ "EXTERNAL", "NOBODY", "NONE", "NOONE", "BITBUCKET", "DEVNULL", "BOTH", "USER", "ADMIN" ] 407 407 try : 408 408 fullmailto = self.getPrinterOption(printername, "mailto") 409 except PyKotaConfigError : 409 except PyKotaConfigError : 410 410 return ("BOTH", None) 411 else : 411 else : 412 412 try : 413 413 mailto = [x.strip() for x in fullmailto.split('(', 1)] 414 except ValueError : 414 except ValueError : 415 415 raise PyKotaConfigError, _("Invalid option mailto %s for printer %s") % (fullmailto, printername) 416 if len(mailto) == 1 : 416 if len(mailto) == 1 : 417 417 mailto.append("") 418 (mailto, args) = mailto 418 (mailto, args) = mailto 419 419 if args.endswith(')') : 420 420 args = args[:-1] 421 mailto = mailto.upper() 421 mailto = mailto.upper() 422 422 if (mailto == "EXTERNAL") and not args : 423 423 raise PyKotaConfigError, _("Invalid option mailto %s for printer %s") % (fullmailto, printername) … … 425 425 raise PyKotaConfigError, _("Option mailto in section %s only supports values in %s") % (printername, str(validmailtos)) 426 426 return (mailto, args) 427 428 def getMaxDenyBanners(self, printername) : 427 428 def getMaxDenyBanners(self, printername) : 429 429 """Returns the maximum number of deny banners to be printed for a particular user on a particular printer.""" 430 430 try : 431 431 maxdb = self.getPrinterOption(printername, "maxdenybanners") 432 except PyKotaConfigError : 432 except PyKotaConfigError : 433 433 return 0 # default value is to forbid printing a deny banner. 434 434 try : … … 436 436 if value < 0 : 437 437 raise ValueError 438 except (TypeError, ValueError) : 438 except (TypeError, ValueError) : 439 439 raise PyKotaConfigError, _("Invalid maximal deny banners counter %s") % maxdb 440 else : 440 else : 441 441 return value 442 442 … … 447 447 except PyKotaConfigError : 448 448 return True 449 450 def getGraceDelay(self, printername) : 449 450 def getGraceDelay(self, printername) : 451 451 """Returns the grace delay in days.""" 452 452 try : 453 453 gd = self.getPrinterOption(printername, "gracedelay") 454 except PyKotaConfigError : 454 except PyKotaConfigError : 455 455 gd = 7 # default value of 7 days 456 456 try : 457 457 return int(gd) 458 except (TypeError, ValueError) : 458 except (TypeError, ValueError) : 459 459 raise PyKotaConfigError, _("Invalid grace delay %s") % gd 460 461 def getPoorMan(self) : 460 461 def getPoorMan(self) : 462 462 """Returns the poor man's threshold.""" 463 463 try : 464 464 pm = self.getGlobalOption("poorman") 465 except PyKotaConfigError : 465 except PyKotaConfigError : 466 466 pm = 1.0 # default value of 1 unit 467 467 try : 468 468 return float(pm) 469 except (TypeError, ValueError) : 469 except (TypeError, ValueError) : 470 470 raise PyKotaConfigError, _("Invalid poor man's threshold %s") % pm 471 472 def getBalanceZero(self) : 471 472 def getBalanceZero(self) : 473 473 """Returns the value of the zero for balance limitation.""" 474 474 try : 475 475 bz = self.getGlobalOption("balancezero") 476 except PyKotaConfigError : 476 except PyKotaConfigError : 477 477 bz = 0.0 # default value, zero is 0.0 478 478 try : 479 479 return float(bz) 480 except (TypeError, ValueError) : 480 except (TypeError, ValueError) : 481 481 raise PyKotaConfigError, _("Invalid balancezero value %s") % bz 482 483 def getPoorWarn(self) : 482 483 def getPoorWarn(self) : 484 484 """Returns the poor man's warning message.""" 485 485 try : 486 486 return self.getGlobalOption("poorwarn") 487 except PyKotaConfigError : 487 except PyKotaConfigError : 488 488 return _("Your Print Quota account balance is Low.\nSoon you'll not be allowed to print anymore.\nPlease contact the Print Quota Administrator to solve the problem.") 489 490 def getHardWarn(self, printername) : 489 490 def getHardWarn(self, printername) : 491 491 """Returns the hard limit error message.""" 492 492 try : 493 493 return self.getPrinterOption(printername, "hardwarn") 494 except PyKotaConfigError : 494 except PyKotaConfigError : 495 495 return _("You are not allowed to print anymore because\nyour Print Quota is exceeded on printer %s.") % printername 496 497 def getSoftWarn(self, printername) : 496 497 def getSoftWarn(self, printername) : 498 498 """Returns the soft limit error message.""" 499 499 try : 500 500 return self.getPrinterOption(printername, "softwarn") 501 except PyKotaConfigError : 501 except PyKotaConfigError : 502 502 return _("You will soon be forbidden to print anymore because\nyour Print Quota is almost reached on printer %s.") % printername 503 504 def getPrivacy(self) : 503 504 def getPrivacy(self) : 505 505 """Returns True if privacy is activated, else False.""" 506 506 return self.isTrue(self.getGlobalOption("privacy", ignore=True)) 507 508 def getDebug(self) : 507 508 def getDebug(self) : 509 509 """Returns True if debugging is activated, else False.""" 510 510 return self.isTrue(self.getGlobalOption("debug", ignore=True)) 511 512 def getCaching(self) : 511 512 def getCaching(self) : 513 513 """Returns True if database caching is enabled, else False.""" 514 514 return self.isTrue(self.getGlobalOption("storagecaching", ignore=True)) 515 516 def getLDAPCache(self) : 515 516 def getLDAPCache(self) : 517 517 """Returns True if low-level LDAP caching is enabled, else False.""" 518 518 return self.isTrue(self.getGlobalOption("ldapcache", ignore=True)) 519 520 def getDisableHistory(self) : 519 520 def getDisableHistory(self) : 521 521 """Returns True if we want to disable history, else False.""" 522 522 return self.isTrue(self.getGlobalOption("disablehistory", ignore=True)) 523 524 def getUserNameToLower(self) : 523 524 def getUserNameToLower(self) : 525 525 """Deprecated.""" 526 526 return self.getGlobalOption("utolower", ignore=True) 527 527 528 528 def getUserNameCase(self) : 529 529 """Returns value for user name case: upper, lower or native""" … … 531 531 try : 532 532 value = self.getGlobalOption("usernamecase", ignore=True).strip().lower() 533 except AttributeError : 533 except AttributeError : 534 534 value = "native" 535 535 if value not in validvalues : 536 536 raise PyKotaConfigError, _("Option usernamecase only supports values in %s") % str(validvalues) 537 537 return value 538 539 def getRejectUnknown(self) : 538 539 def getRejectUnknown(self) : 540 540 """Returns True if we want to reject the creation of unknown users or groups, else False.""" 541 541 return self.isTrue(self.getGlobalOption("reject_unknown", ignore=True)) 542 543 def getPrinterKeepFiles(self, printername) : 542 543 def getPrinterKeepFiles(self, printername) : 544 544 """Returns True if files must be kept on disk, else False.""" 545 try : 545 try : 546 546 return self.isTrue(self.getPrinterOption(printername, "keepfiles")) 547 except PyKotaConfigError : 547 except PyKotaConfigError : 548 548 return False 549 550 def getPrinterDirectory(self, printername) : 549 550 def getPrinterDirectory(self, printername) : 551 551 """Returns the path to our working directory, else a directory suitable for temporary files.""" 552 try : 552 try : 553 553 return self.getPrinterOption(printername, "directory").strip() 554 except PyKotaConfigError : 554 except PyKotaConfigError : 555 555 return tempfile.gettempdir() 556 557 def getDenyDuplicates(self, printername) : 556 557 def getDenyDuplicates(self, printername) : 558 558 """Returns True or a command if we want to deny duplicate jobs, else False.""" 559 try : 559 try : 560 560 denyduplicates = self.getPrinterOption(printername, "denyduplicates") 561 except PyKotaConfigError : 561 except PyKotaConfigError : 562 562 return False 563 else : 563 else : 564 564 if self.isTrue(denyduplicates) : 565 565 return True 566 566 elif self.isFalse(denyduplicates) : 567 567 return False 568 else : 568 else : 569 569 # it's a command to run. 570 570 return denyduplicates 571 572 def getDuplicatesDelay(self, printername) : 571 572 def getDuplicatesDelay(self, printername) : 573 573 """Returns the number of seconds after which two identical jobs are not considered a duplicate anymore.""" 574 try : 574 try : 575 575 duplicatesdelay = self.getPrinterOption(printername, "duplicatesdelay") 576 except PyKotaConfigError : 576 except PyKotaConfigError : 577 577 return 0 578 else : 578 else : 579 579 try : 580 580 return int(duplicatesdelay) 581 581 except (TypeError, ValueError) : 582 582 raise PyKotaConfigError, _("Incorrect value %s for the duplicatesdelay directive in section %s") % (str(duplicatesdelay), printername) 583 584 def getNoPrintingMaxDelay(self, printername) : 583 584 def getNoPrintingMaxDelay(self, printername) : 585 585 """Returns the max number of seconds to wait for the printer to be in 'printing' mode.""" 586 try : 586 try : 587 587 maxdelay = self.getPrinterOption(printername, "noprintingmaxdelay") 588 except PyKotaConfigError : 588 except PyKotaConfigError : 589 589 return None # tells to use hardcoded value 590 else : 590 else : 591 591 try : 592 592 maxdelay = int(maxdelay) … … 595 595 except (TypeError, ValueError) : 596 596 raise PyKotaConfigError, _("Incorrect value %s for the noprintingmaxdelay directive in section %s") % (str(maxdelay), printername) 597 else : 597 else : 598 598 return maxdelay 599 600 def getStatusStabilizationLoops(self, printername) : 599 600 def getStatusStabilizationLoops(self, printername) : 601 601 """Returns the number of times the printer must return the 'idle' status to consider it stable.""" 602 try : 602 try : 603 603 stab = self.getPrinterOption(printername, "statusstabilizationloops") 604 except PyKotaConfigError : 604 except PyKotaConfigError : 605 605 return None # tells to use hardcoded value 606 else : 606 else : 607 607 try : 608 608 stab = int(stab) … … 611 611 except (TypeError, ValueError) : 612 612 raise PyKotaConfigError, _("Incorrect value %s for the statusstabilizationloops directive in section %s") % (str(stab), printername) 613 else : 613 else : 614 614 return stab 615 616 def getStatusStabilizationDelay(self, printername) : 615 616 def getStatusStabilizationDelay(self, printername) : 617 617 """Returns the number of seconds to wait between two checks of the printer's status.""" 618 try : 618 try : 619 619 stab = self.getPrinterOption(printername, "statusstabilizationdelay") 620 except PyKotaConfigError : 620 except PyKotaConfigError : 621 621 return None # tells to use hardcoded value 622 else : 622 else : 623 623 try : 624 624 stab = float(stab) … … 627 627 except (TypeError, ValueError) : 628 628 raise PyKotaConfigError, _("Incorrect value %s for the statusstabilizationdelay directive in section %s") % (str(stab), printername) 629 else : 629 else : 630 630 return stab 631 632 def getPrinterSNMPErrorMask(self, printername) : 631 632 def getPrinterSNMPErrorMask(self, printername) : 633 633 """Returns the SNMP error mask for a particular printer, or None if not defined.""" 634 try : 634 try : 635 635 errmask = self.getPrinterOption(printername, "snmperrormask").lower() 636 except PyKotaConfigError : 636 except PyKotaConfigError : 637 637 return None # tells to use hardcoded value 638 else : 638 else : 639 639 try : 640 640 if errmask.startswith("0x") : 641 641 value = int(errmask, 16) 642 elif errmask.startswith("0") : 642 elif errmask.startswith("0") : 643 643 value = int(errmask, 8) 644 else : 644 else : 645 645 value = int(errmask) 646 646 if 0 <= value < 65536 : 647 647 return value 648 else : 648 else : 649 649 raise ValueError 650 except ValueError : 650 except ValueError : 651 651 raise PyKotaConfigError, _("Incorrect value %s for the snmperrormask directive in section %s") % (errmask, printername) 652 653 def getWinbindSeparator(self) : 652 653 def getWinbindSeparator(self) : 654 654 """Returns the winbind separator's value if it is set, else None.""" 655 655 return self.getGlobalOption("winbind_separator", ignore=True) … … 657 657 def getAccountBanner(self, printername) : 658 658 """Returns which banner(s) to account for: NONE, BOTH, STARTING, ENDING.""" 659 validvalues = [ "NONE", "BOTH", "STARTING", "ENDING" ] 659 validvalues = [ "NONE", "BOTH", "STARTING", "ENDING" ] 660 660 try : 661 661 value = self.getPrinterOption(printername, "accountbanner") 662 except PyKotaConfigError : 662 except PyKotaConfigError : 663 663 return "BOTH" # Default value of BOTH 664 else : 664 else : 665 665 value = value.strip().upper() 666 666 if value not in validvalues : 667 667 raise PyKotaConfigError, _("Option accountbanner in section %s only supports values in %s") % (printername, str(validvalues)) 668 return value 668 return value 669 669 670 670 def getAvoidDuplicateBanners(self, printername) : … … 699 699 except PyKotaConfigError : 700 700 return None 701 701 702 702 def getTrustJobSize(self, printername) : 703 703 """Returns the normalized value of the trustjobsize's directive.""" … … 706 706 except PyKotaConfigError : 707 707 return (None, "YES") 708 else : 708 else : 709 709 if value == "YES" : 710 710 return (None, "YES") 711 try : 711 try : 712 712 (limit, replacement) = [p.strip() for p in value.split(">")[1].split(":")] 713 713 limit = int(limit) 714 714 try : 715 replacement = int(replacement) 716 except ValueError : 715 replacement = int(replacement) 716 except ValueError : 717 717 if replacement != "PRECOMPUTED" : 718 718 raise … … 723 723 except (IndexError, ValueError, TypeError) : 724 724 raise PyKotaConfigError, _("Option trustjobsize for printer %s is incorrect") % printername 725 return (limit, replacement) 726 725 return (limit, replacement) 726 727 727 def getPrinterCoefficients(self, printername) : 728 728 """Returns a mapping of coefficients for a particular printer.""" … … 743 743 try : 744 744 branches[k] = float(value) 745 except ValueError : 745 except ValueError : 746 746 raise PyKotaConfigError, "Invalid coefficient %s (%s) for printer %s" % (k, value, printername) 747 747 748 748 for (k, v) in sectionbranches : 749 749 k = k.split('_', 1)[1] … … 752 752 try : 753 753 branches[k] = float(value) # overwrite any global option or set a new value 754 except ValueError : 754 except ValueError : 755 755 raise PyKotaConfigError, "Invalid coefficient %s (%s) for printer %s" % (k, value, printername) 756 756 else : 757 757 del branches[k] # empty value disables a global option 758 758 return branches 759 759 760 760 def getPrinterSkipInitialWait(self, printername) : 761 761 """Returns True if we want to skip the initial waiting loop, else False."""