[3260] | 1 | # -*- coding: UTF-8 -*- |
---|
[695] | 2 | # |
---|
[3260] | 3 | # PyKota : Print Quotas for CUPS |
---|
[695] | 4 | # |
---|
[3275] | 5 | # (c) 2003, 2004, 2005, 2006, 2007, 2008 Jerome Alet <alet@librelogiciel.com> |
---|
[3260] | 6 | # This program is free software: you can redistribute it and/or modify |
---|
[873] | 7 | # it under the terms of the GNU General Public License as published by |
---|
[3260] | 8 | # the Free Software Foundation, either version 3 of the License, or |
---|
[873] | 9 | # (at your option) any later version. |
---|
[3260] | 10 | # |
---|
[873] | 11 | # This program is distributed in the hope that it will be useful, |
---|
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 14 | # GNU General Public License for more details. |
---|
| 15 | # |
---|
| 16 | # You should have received a copy of the GNU General Public License |
---|
[3260] | 17 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
[695] | 18 | # |
---|
| 19 | # $Id$ |
---|
| 20 | # |
---|
[2074] | 21 | # |
---|
[695] | 22 | |
---|
[3184] | 23 | """This module defines classes used to parse PyKota configuration files.""" |
---|
| 24 | |
---|
[695] | 25 | import os |
---|
[2405] | 26 | import tempfile |
---|
[695] | 27 | import ConfigParser |
---|
| 28 | |
---|
| 29 | class PyKotaConfigError(Exception): |
---|
| 30 | """An exception for PyKota config related stuff.""" |
---|
| 31 | def __init__(self, message = ""): |
---|
| 32 | self.message = message |
---|
| 33 | Exception.__init__(self, message) |
---|
| 34 | def __repr__(self): |
---|
| 35 | return self.message |
---|
| 36 | __str__ = __repr__ |
---|
| 37 | |
---|
| 38 | class PyKotaConfig : |
---|
| 39 | """A class to deal with PyKota's configuration.""" |
---|
| 40 | def __init__(self, directory) : |
---|
| 41 | """Reads and checks the configuration file.""" |
---|
[1785] | 42 | self.isAdmin = 0 |
---|
| 43 | self.directory = directory |
---|
[695] | 44 | self.filename = os.path.join(directory, "pykota.conf") |
---|
[2418] | 45 | self.adminfilename = os.path.join(directory, "pykotadmin.conf") |
---|
[2421] | 46 | if not os.access(self.filename, os.R_OK) : |
---|
| 47 | raise PyKotaConfigError, _("Configuration file %s can't be read. Please check that the file exists and that your permissions are sufficient.") % self.filename |
---|
[2418] | 48 | if not os.path.isfile(self.adminfilename) : |
---|
| 49 | raise PyKotaConfigError, _("Configuration file %s not found.") % self.adminfilename |
---|
| 50 | if os.access(self.adminfilename, os.R_OK) : |
---|
| 51 | self.isAdmin = 1 |
---|
[695] | 52 | self.config = ConfigParser.ConfigParser() |
---|
| 53 | self.config.read([self.filename]) |
---|
[1227] | 54 | |
---|
| 55 | def isTrue(self, option) : |
---|
[3126] | 56 | """Returns True if option is set to true, else False.""" |
---|
[2308] | 57 | if (option is not None) and (option.strip().upper() in ['Y', 'YES', '1', 'ON', 'T', 'TRUE']) : |
---|
[3126] | 58 | return True |
---|
[1227] | 59 | else : |
---|
[3126] | 60 | return False |
---|
[695] | 61 | |
---|
[2308] | 62 | def isFalse(self, option) : |
---|
[3126] | 63 | """Returns True if option is set to false, else False.""" |
---|
[2308] | 64 | if (option is not None) and (option.strip().upper() in ['N', 'NO', '0', 'OFF', 'F', 'FALSE']) : |
---|
[3126] | 65 | return True |
---|
[2308] | 66 | else : |
---|
[3126] | 67 | return False |
---|
[2308] | 68 | |
---|
[695] | 69 | def getPrinterNames(self) : |
---|
| 70 | """Returns the list of configured printers, i.e. all sections names minus 'global'.""" |
---|
| 71 | return [pname for pname in self.config.sections() if pname != "global"] |
---|
| 72 | |
---|
[802] | 73 | def getGlobalOption(self, option, ignore=0) : |
---|
| 74 | """Returns an option from the global section, or raises a PyKotaConfigError if ignore is not set, else returns None.""" |
---|
| 75 | try : |
---|
| 76 | return self.config.get("global", option, raw=1) |
---|
| 77 | except (ConfigParser.NoSectionError, ConfigParser.NoOptionError) : |
---|
| 78 | if ignore : |
---|
[3169] | 79 | return None |
---|
[802] | 80 | else : |
---|
| 81 | raise PyKotaConfigError, _("Option %s not found in section global of %s") % (option, self.filename) |
---|
| 82 | |
---|
[1192] | 83 | def getPrinterOption(self, printername, option) : |
---|
[802] | 84 | """Returns an option from the printer section, or the global section, or raises a PyKotaConfigError.""" |
---|
| 85 | globaloption = self.getGlobalOption(option, ignore=1) |
---|
| 86 | try : |
---|
[1192] | 87 | return self.config.get(printername, option, raw=1) |
---|
[802] | 88 | except (ConfigParser.NoSectionError, ConfigParser.NoOptionError) : |
---|
| 89 | if globaloption is not None : |
---|
| 90 | return globaloption |
---|
| 91 | else : |
---|
[1192] | 92 | raise PyKotaConfigError, _("Option %s not found in section %s of %s") % (option, printername, self.filename) |
---|
[802] | 93 | |
---|
[695] | 94 | def getStorageBackend(self) : |
---|
[800] | 95 | """Returns the storage backend information as a Python mapping.""" |
---|
| 96 | backendinfo = {} |
---|
[2593] | 97 | backend = self.getGlobalOption("storagebackend").lower() |
---|
| 98 | backendinfo["storagebackend"] = backend |
---|
| 99 | if backend == "sqlitestorage" : |
---|
| 100 | issqlite = 1 |
---|
| 101 | backendinfo["storagename"] = self.getGlobalOption("storagename") |
---|
| 102 | for option in ["storageserver", "storageuser", "storageuserpw"] : |
---|
| 103 | backendinfo[option] = None |
---|
| 104 | else : |
---|
| 105 | issqlite = 0 |
---|
| 106 | for option in ["storageserver", "storagename", "storageuser"] : |
---|
| 107 | backendinfo[option] = self.getGlobalOption(option) |
---|
| 108 | backendinfo["storageuserpw"] = self.getGlobalOption("storageuserpw", ignore=1) # password is optional |
---|
| 109 | |
---|
[1087] | 110 | backendinfo["storageadmin"] = None |
---|
| 111 | backendinfo["storageadminpw"] = None |
---|
[2418] | 112 | if self.isAdmin : |
---|
| 113 | adminconf = ConfigParser.ConfigParser() |
---|
| 114 | adminconf.read([self.adminfilename]) |
---|
| 115 | if adminconf.sections() : # were we able to read the file ? |
---|
| 116 | try : |
---|
| 117 | backendinfo["storageadmin"] = adminconf.get("global", "storageadmin", raw=1) |
---|
| 118 | except (ConfigParser.NoSectionError, ConfigParser.NoOptionError) : |
---|
[2593] | 119 | if not issqlite : |
---|
| 120 | raise PyKotaConfigError, _("Option %s not found in section global of %s") % ("storageadmin", self.adminfilename) |
---|
[2418] | 121 | try : |
---|
| 122 | backendinfo["storageadminpw"] = adminconf.get("global", "storageadminpw", raw=1) |
---|
| 123 | except (ConfigParser.NoSectionError, ConfigParser.NoOptionError) : |
---|
| 124 | pass # Password is optional |
---|
| 125 | # Now try to overwrite the storagebackend, storageserver |
---|
| 126 | # and storagename. This allows admins to use the master LDAP |
---|
| 127 | # server directly and users to use the replicas transparently. |
---|
| 128 | try : |
---|
| 129 | backendinfo["storagebackend"] = adminconf.get("global", "storagebackend", raw=1) |
---|
| 130 | except ConfigParser.NoOptionError : |
---|
| 131 | pass |
---|
| 132 | try : |
---|
| 133 | backendinfo["storageserver"] = adminconf.get("global", "storageserver", raw=1) |
---|
| 134 | except ConfigParser.NoOptionError : |
---|
| 135 | pass |
---|
| 136 | try : |
---|
| 137 | backendinfo["storagename"] = adminconf.get("global", "storagename", raw=1) |
---|
| 138 | except ConfigParser.NoOptionError : |
---|
| 139 | pass |
---|
[800] | 140 | return backendinfo |
---|
[695] | 141 | |
---|
[1029] | 142 | def getLDAPInfo(self) : |
---|
| 143 | """Returns some hints for the LDAP backend.""" |
---|
| 144 | ldapinfo = {} |
---|
| 145 | for option in [ "userbase", "userrdn", \ |
---|
[1041] | 146 | "balancebase", "balancerdn", \ |
---|
[1029] | 147 | "groupbase", "grouprdn", "groupmembers", \ |
---|
| 148 | "printerbase", "printerrdn", \ |
---|
| 149 | "userquotabase", "groupquotabase", \ |
---|
[2370] | 150 | "jobbase", "lastjobbase", "billingcodebase", \ |
---|
[1105] | 151 | "newuser", "newgroup", \ |
---|
[1111] | 152 | "usermail", \ |
---|
[1029] | 153 | ] : |
---|
[1105] | 154 | ldapinfo[option] = self.getGlobalOption(option).strip() |
---|
| 155 | for field in ["newuser", "newgroup"] : |
---|
| 156 | if ldapinfo[field].lower().startswith('attach(') : |
---|
| 157 | ldapinfo[field] = ldapinfo[field][7:-1] |
---|
[1968] | 158 | |
---|
| 159 | # should we use TLS, by default (if unset) value is NO |
---|
| 160 | ldapinfo["ldaptls"] = self.isTrue(self.getGlobalOption("ldaptls", ignore=1)) |
---|
| 161 | ldapinfo["cacert"] = self.getGlobalOption("cacert", ignore=1) |
---|
| 162 | if ldapinfo["cacert"] : |
---|
| 163 | ldapinfo["cacert"] = ldapinfo["cacert"].strip() |
---|
| 164 | if ldapinfo["ldaptls"] : |
---|
| 165 | if not os.access(ldapinfo["cacert"] or "", os.R_OK) : |
---|
| 166 | raise PyKotaConfigError, _("Option ldaptls is set, but certificate %s is not readable.") % str(ldapinfo["cacert"]) |
---|
[1029] | 167 | return ldapinfo |
---|
| 168 | |
---|
[695] | 169 | def getLoggingBackend(self) : |
---|
| 170 | """Returns the logging backend information.""" |
---|
[802] | 171 | validloggers = [ "stderr", "system" ] |
---|
[853] | 172 | try : |
---|
| 173 | logger = self.getGlobalOption("logger").lower() |
---|
| 174 | except PyKotaConfigError : |
---|
| 175 | logger = "system" |
---|
[802] | 176 | if logger not in validloggers : |
---|
| 177 | raise PyKotaConfigError, _("Option logger only supports values in %s") % str(validloggers) |
---|
| 178 | return logger |
---|
[695] | 179 | |
---|
[2262] | 180 | def getLogoURL(self) : |
---|
| 181 | """Returns the URL to use for the logo in the CGI scripts.""" |
---|
| 182 | url = self.getGlobalOption("logourl", ignore=1) or \ |
---|
[2909] | 183 | "http://www.pykota.com/pykota.png" |
---|
[2262] | 184 | return url.strip() |
---|
[2265] | 185 | |
---|
| 186 | def getLogoLink(self) : |
---|
| 187 | """Returns the URL to go to when the user clicks on the logo in the CGI scripts.""" |
---|
| 188 | url = self.getGlobalOption("logolink", ignore=1) or \ |
---|
[2909] | 189 | "http://www.pykota.com/" |
---|
[2265] | 190 | return url.strip() |
---|
[2262] | 191 | |
---|
[2635] | 192 | def getPreAccounterBackend(self, printername) : |
---|
| 193 | """Returns the preaccounter backend to use for a given printer.""" |
---|
[3034] | 194 | validaccounters = [ "software", "ink" ] |
---|
[2635] | 195 | try : |
---|
| 196 | fullaccounter = self.getPrinterOption(printername, "preaccounter").strip() |
---|
| 197 | except PyKotaConfigError : |
---|
| 198 | return ("software", "") |
---|
| 199 | else : |
---|
| 200 | flower = fullaccounter.lower() |
---|
[3029] | 201 | for vac in validaccounters : |
---|
| 202 | if flower.startswith(vac) : |
---|
| 203 | try : |
---|
| 204 | (accounter, args) = [x.strip() for x in fullaccounter.split('(', 1)] |
---|
| 205 | except ValueError : |
---|
| 206 | raise PyKotaConfigError, _("Invalid preaccounter %s for printer %s") % (fullaccounter, printername) |
---|
| 207 | if args.endswith(')') : |
---|
| 208 | args = args[:-1].strip() |
---|
[3034] | 209 | if (vac == "ink") and not args : |
---|
| 210 | raise PyKotaConfigError, _("Invalid preaccounter %s for printer %s") % (fullaccounter, printername) |
---|
[3029] | 211 | return (vac, args) |
---|
| 212 | raise PyKotaConfigError, _("Option preaccounter in section %s only supports values in %s") % (printername, str(validaccounters)) |
---|
[2635] | 213 | |
---|
[1192] | 214 | def getAccounterBackend(self, printername) : |
---|
[2635] | 215 | """Returns the accounter backend to use for a given printer.""" |
---|
[3034] | 216 | validaccounters = [ "hardware", "software", "ink" ] |
---|
[3029] | 217 | try : |
---|
| 218 | fullaccounter = self.getPrinterOption(printername, "accounter").strip() |
---|
| 219 | except PyKotaConfigError : |
---|
| 220 | return ("software", "") |
---|
| 221 | else : |
---|
| 222 | flower = fullaccounter.lower() |
---|
| 223 | for vac in validaccounters : |
---|
| 224 | if flower.startswith(vac) : |
---|
| 225 | try : |
---|
| 226 | (accounter, args) = [x.strip() for x in fullaccounter.split('(', 1)] |
---|
| 227 | except ValueError : |
---|
| 228 | raise PyKotaConfigError, _("Invalid accounter %s for printer %s") % (fullaccounter, printername) |
---|
| 229 | if args.endswith(')') : |
---|
| 230 | args = args[:-1].strip() |
---|
[3034] | 231 | if (vac in ("hardware", "ink")) and not args : |
---|
[3029] | 232 | raise PyKotaConfigError, _("Invalid accounter %s for printer %s") % (fullaccounter, printername) |
---|
| 233 | return (vac, args) |
---|
[1192] | 234 | raise PyKotaConfigError, _("Option accounter in section %s only supports values in %s") % (printername, str(validaccounters)) |
---|
[973] | 235 | |
---|
[1371] | 236 | def getPreHook(self, printername) : |
---|
| 237 | """Returns the prehook command line to launch, or None if unset.""" |
---|
| 238 | try : |
---|
| 239 | return self.getPrinterOption(printername, "prehook").strip() |
---|
| 240 | except PyKotaConfigError : |
---|
| 241 | return # No command to launch in the pre-hook |
---|
| 242 | |
---|
| 243 | def getPostHook(self, printername) : |
---|
| 244 | """Returns the posthook command line to launch, or None if unset.""" |
---|
| 245 | try : |
---|
| 246 | return self.getPrinterOption(printername, "posthook").strip() |
---|
| 247 | except PyKotaConfigError : |
---|
| 248 | return # No command to launch in the post-hook |
---|
| 249 | |
---|
[2307] | 250 | def getStripTitle(self, printername) : |
---|
| 251 | """Returns the striptitle directive's content, or None if unset.""" |
---|
| 252 | try : |
---|
| 253 | return self.getPrinterOption(printername, "striptitle").strip() |
---|
| 254 | except PyKotaConfigError : |
---|
| 255 | return # No prefix to strip off |
---|
| 256 | |
---|
[2895] | 257 | def getAskConfirmation(self, printername) : |
---|
| 258 | """Returns the askconfirmation directive's content, or None if unset.""" |
---|
| 259 | try : |
---|
| 260 | return self.getPrinterOption(printername, "askconfirmation").strip() |
---|
| 261 | except PyKotaConfigError : |
---|
| 262 | return # No overwriting will be done |
---|
| 263 | |
---|
[2385] | 264 | def getOverwriteJobTicket(self, printername) : |
---|
| 265 | """Returns the overwrite_jobticket directive's content, or None if unset.""" |
---|
| 266 | try : |
---|
| 267 | return self.getPrinterOption(printername, "overwrite_jobticket").strip() |
---|
| 268 | except PyKotaConfigError : |
---|
| 269 | return # No overwriting will be done |
---|
| 270 | |
---|
| 271 | def getUnknownBillingCode(self, printername) : |
---|
| 272 | """Returns the unknown_billingcode directive's content, or the default value if unset.""" |
---|
| 273 | validvalues = [ "CREATE", "DENY" ] |
---|
| 274 | try : |
---|
| 275 | fullvalue = self.getPrinterOption(printername, "unknown_billingcode") |
---|
| 276 | except PyKotaConfigError : |
---|
| 277 | return ("CREATE", None) |
---|
| 278 | else : |
---|
| 279 | try : |
---|
| 280 | value = [x.strip() for x in fullvalue.split('(', 1)] |
---|
| 281 | except ValueError : |
---|
| 282 | raise PyKotaConfigError, _("Invalid unknown_billingcode directive %s for printer %s") % (fullvalue, printername) |
---|
| 283 | if len(value) == 1 : |
---|
| 284 | value.append("") |
---|
| 285 | (value, args) = value |
---|
| 286 | if args.endswith(')') : |
---|
| 287 | args = args[:-1] |
---|
| 288 | value = value.upper() |
---|
| 289 | if (value == "DENY") and not args : |
---|
| 290 | return ("DENY", None) |
---|
| 291 | if value not in validvalues : |
---|
| 292 | raise PyKotaConfigError, _("Directive unknown_billingcode in section %s only supports values in %s") % (printername, str(validvalues)) |
---|
| 293 | return (value, args) |
---|
| 294 | |
---|
[1495] | 295 | def getPrinterEnforcement(self, printername) : |
---|
| 296 | """Returns if quota enforcement should be strict or laxist for the current printer.""" |
---|
| 297 | validenforcements = [ "STRICT", "LAXIST" ] |
---|
| 298 | try : |
---|
| 299 | enforcement = self.getPrinterOption(printername, "enforcement") |
---|
| 300 | except PyKotaConfigError : |
---|
| 301 | return "LAXIST" |
---|
| 302 | else : |
---|
| 303 | enforcement = enforcement.upper() |
---|
| 304 | if enforcement not in validenforcements : |
---|
| 305 | raise PyKotaConfigError, _("Option enforcement in section %s only supports values in %s") % (printername, str(validenforcements)) |
---|
| 306 | return enforcement |
---|
| 307 | |
---|
[2583] | 308 | def getPrinterOnBackendError(self, printername) : |
---|
| 309 | """Returns what must be done whenever the real CUPS backend fails.""" |
---|
| 310 | validactions = [ "CHARGE", "NOCHARGE" ] |
---|
| 311 | try : |
---|
| 312 | action = self.getPrinterOption(printername, "onbackenderror") |
---|
| 313 | except PyKotaConfigError : |
---|
[2759] | 314 | return ["NOCHARGE"] |
---|
[2583] | 315 | else : |
---|
[2759] | 316 | action = action.upper().split(",") |
---|
| 317 | error = False |
---|
| 318 | for act in action : |
---|
| 319 | if act not in validactions : |
---|
| 320 | if act.startswith("RETRY:") : |
---|
| 321 | try : |
---|
| 322 | (num, delay) = [int(p) for p in act[6:].split(":", 2)] |
---|
| 323 | except ValueError : |
---|
| 324 | error = True |
---|
| 325 | else : |
---|
| 326 | error = True |
---|
| 327 | if error : |
---|
| 328 | raise PyKotaConfigError, _("Option onbackenderror in section %s only supports values 'charge', 'nocharge', and 'retry:num:delay'") % printername |
---|
[2583] | 329 | return action |
---|
| 330 | |
---|
[1687] | 331 | def getPrinterOnAccounterError(self, printername) : |
---|
| 332 | """Returns what must be done whenever the accounter fails.""" |
---|
| 333 | validactions = [ "CONTINUE", "STOP" ] |
---|
| 334 | try : |
---|
| 335 | action = self.getPrinterOption(printername, "onaccountererror") |
---|
| 336 | except PyKotaConfigError : |
---|
| 337 | return "STOP" |
---|
| 338 | else : |
---|
| 339 | action = action.upper() |
---|
| 340 | if action not in validactions : |
---|
| 341 | raise PyKotaConfigError, _("Option onaccountererror in section %s only supports values in %s") % (printername, str(validactions)) |
---|
| 342 | return action |
---|
| 343 | |
---|
[1192] | 344 | def getPrinterPolicy(self, printername) : |
---|
[695] | 345 | """Returns the default policy for the current printer.""" |
---|
[1152] | 346 | validpolicies = [ "ALLOW", "DENY", "EXTERNAL" ] |
---|
[853] | 347 | try : |
---|
[1192] | 348 | fullpolicy = self.getPrinterOption(printername, "policy") |
---|
[853] | 349 | except PyKotaConfigError : |
---|
[1152] | 350 | return ("DENY", None) |
---|
| 351 | else : |
---|
| 352 | try : |
---|
| 353 | policy = [x.strip() for x in fullpolicy.split('(', 1)] |
---|
| 354 | except ValueError : |
---|
[1192] | 355 | raise PyKotaConfigError, _("Invalid policy %s for printer %s") % (fullpolicy, printername) |
---|
[1152] | 356 | if len(policy) == 1 : |
---|
| 357 | policy.append("") |
---|
| 358 | (policy, args) = policy |
---|
| 359 | if args.endswith(')') : |
---|
| 360 | args = args[:-1] |
---|
| 361 | policy = policy.upper() |
---|
| 362 | if (policy == "EXTERNAL") and not args : |
---|
[1192] | 363 | raise PyKotaConfigError, _("Invalid policy %s for printer %s") % (fullpolicy, printername) |
---|
[1152] | 364 | if policy not in validpolicies : |
---|
[1192] | 365 | raise PyKotaConfigError, _("Option policy in section %s only supports values in %s") % (printername, str(validpolicies)) |
---|
[1152] | 366 | return (policy, args) |
---|
[695] | 367 | |
---|
[1518] | 368 | def getCrashRecipient(self) : |
---|
| 369 | """Returns the email address of the software crash messages recipient.""" |
---|
| 370 | try : |
---|
| 371 | return self.getGlobalOption("crashrecipient") |
---|
| 372 | except : |
---|
| 373 | return |
---|
| 374 | |
---|
[695] | 375 | def getSMTPServer(self) : |
---|
| 376 | """Returns the SMTP server to use to send messages to users.""" |
---|
[853] | 377 | try : |
---|
| 378 | return self.getGlobalOption("smtpserver") |
---|
| 379 | except PyKotaConfigError : |
---|
| 380 | return "localhost" |
---|
[695] | 381 | |
---|
[1353] | 382 | def getMailDomain(self) : |
---|
| 383 | """Returns the mail domain to use to send messages to users.""" |
---|
| 384 | try : |
---|
| 385 | return self.getGlobalOption("maildomain") |
---|
| 386 | except PyKotaConfigError : |
---|
| 387 | return |
---|
| 388 | |
---|
[1192] | 389 | def getAdminMail(self, printername) : |
---|
[695] | 390 | """Returns the Email address of the Print Quota Administrator.""" |
---|
[853] | 391 | try : |
---|
[1192] | 392 | return self.getPrinterOption(printername, "adminmail") |
---|
[853] | 393 | except PyKotaConfigError : |
---|
| 394 | return "root@localhost" |
---|
[695] | 395 | |
---|
[1192] | 396 | def getAdmin(self, printername) : |
---|
[695] | 397 | """Returns the full name of the Print Quota Administrator.""" |
---|
[853] | 398 | try : |
---|
[1192] | 399 | return self.getPrinterOption(printername, "admin") |
---|
[853] | 400 | except PyKotaConfigError : |
---|
| 401 | return "root" |
---|
[708] | 402 | |
---|
[1192] | 403 | def getMailTo(self, printername) : |
---|
[852] | 404 | """Returns the recipient of email messages.""" |
---|
[1192] | 405 | validmailtos = [ "EXTERNAL", "NOBODY", "NONE", "NOONE", "BITBUCKET", "DEVNULL", "BOTH", "USER", "ADMIN" ] |
---|
[853] | 406 | try : |
---|
[1192] | 407 | fullmailto = self.getPrinterOption(printername, "mailto") |
---|
[853] | 408 | except PyKotaConfigError : |
---|
[1192] | 409 | return ("BOTH", None) |
---|
| 410 | else : |
---|
| 411 | try : |
---|
| 412 | mailto = [x.strip() for x in fullmailto.split('(', 1)] |
---|
| 413 | except ValueError : |
---|
| 414 | raise PyKotaConfigError, _("Invalid option mailto %s for printer %s") % (fullmailto, printername) |
---|
| 415 | if len(mailto) == 1 : |
---|
| 416 | mailto.append("") |
---|
| 417 | (mailto, args) = mailto |
---|
| 418 | if args.endswith(')') : |
---|
| 419 | args = args[:-1] |
---|
| 420 | mailto = mailto.upper() |
---|
| 421 | if (mailto == "EXTERNAL") and not args : |
---|
| 422 | raise PyKotaConfigError, _("Invalid option mailto %s for printer %s") % (fullmailto, printername) |
---|
| 423 | if mailto not in validmailtos : |
---|
| 424 | raise PyKotaConfigError, _("Option mailto in section %s only supports values in %s") % (printername, str(validmailtos)) |
---|
| 425 | return (mailto, args) |
---|
[852] | 426 | |
---|
[2054] | 427 | def getMaxDenyBanners(self, printername) : |
---|
| 428 | """Returns the maximum number of deny banners to be printed for a particular user on a particular printer.""" |
---|
| 429 | try : |
---|
| 430 | maxdb = self.getPrinterOption(printername, "maxdenybanners") |
---|
| 431 | except PyKotaConfigError : |
---|
| 432 | return 0 # default value is to forbid printing a deny banner. |
---|
| 433 | try : |
---|
| 434 | value = int(maxdb.strip()) |
---|
| 435 | if value < 0 : |
---|
| 436 | raise ValueError |
---|
| 437 | except (TypeError, ValueError) : |
---|
| 438 | raise PyKotaConfigError, _("Invalid maximal deny banners counter %s") % maxdb |
---|
| 439 | else : |
---|
| 440 | return value |
---|
[3124] | 441 | |
---|
| 442 | def getPrintCancelledBanners(self, printername) : |
---|
[3126] | 443 | """Returns True if a banner should be printed when a job is cancelled, else False.""" |
---|
[3124] | 444 | try : |
---|
| 445 | return self.isTrue(self.getPrinterOption(printername, "printcancelledbanners")) |
---|
| 446 | except PyKotaConfigError : |
---|
[3126] | 447 | return True |
---|
[3124] | 448 | |
---|
[1192] | 449 | def getGraceDelay(self, printername) : |
---|
[708] | 450 | """Returns the grace delay in days.""" |
---|
[731] | 451 | try : |
---|
[1192] | 452 | gd = self.getPrinterOption(printername, "gracedelay") |
---|
[853] | 453 | except PyKotaConfigError : |
---|
[1914] | 454 | gd = 7 # default value of 7 days |
---|
[853] | 455 | try : |
---|
[731] | 456 | return int(gd) |
---|
[1077] | 457 | except (TypeError, ValueError) : |
---|
[773] | 458 | raise PyKotaConfigError, _("Invalid grace delay %s") % gd |
---|
[1021] | 459 | |
---|
[1077] | 460 | def getPoorMan(self) : |
---|
[1646] | 461 | """Returns the poor man's threshold.""" |
---|
[1077] | 462 | try : |
---|
| 463 | pm = self.getGlobalOption("poorman") |
---|
| 464 | except PyKotaConfigError : |
---|
[1914] | 465 | pm = 1.0 # default value of 1 unit |
---|
[1077] | 466 | try : |
---|
| 467 | return float(pm) |
---|
| 468 | except (TypeError, ValueError) : |
---|
[1646] | 469 | raise PyKotaConfigError, _("Invalid poor man's threshold %s") % pm |
---|
[1077] | 470 | |
---|
[2692] | 471 | def getBalanceZero(self) : |
---|
| 472 | """Returns the value of the zero for balance limitation.""" |
---|
| 473 | try : |
---|
| 474 | bz = self.getGlobalOption("balancezero") |
---|
| 475 | except PyKotaConfigError : |
---|
| 476 | bz = 0.0 # default value, zero is 0.0 |
---|
| 477 | try : |
---|
| 478 | return float(bz) |
---|
| 479 | except (TypeError, ValueError) : |
---|
| 480 | raise PyKotaConfigError, _("Invalid balancezero value %s") % bz |
---|
| 481 | |
---|
[1077] | 482 | def getPoorWarn(self) : |
---|
| 483 | """Returns the poor man's warning message.""" |
---|
| 484 | try : |
---|
| 485 | return self.getGlobalOption("poorwarn") |
---|
| 486 | except PyKotaConfigError : |
---|
| 487 | 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.") |
---|
| 488 | |
---|
[1192] | 489 | def getHardWarn(self, printername) : |
---|
[1077] | 490 | """Returns the hard limit error message.""" |
---|
| 491 | try : |
---|
[1192] | 492 | return self.getPrinterOption(printername, "hardwarn") |
---|
[1077] | 493 | except PyKotaConfigError : |
---|
[1192] | 494 | return _("You are not allowed to print anymore because\nyour Print Quota is exceeded on printer %s.") % printername |
---|
[1077] | 495 | |
---|
[1192] | 496 | def getSoftWarn(self, printername) : |
---|
[1077] | 497 | """Returns the soft limit error message.""" |
---|
| 498 | try : |
---|
[1192] | 499 | return self.getPrinterOption(printername, "softwarn") |
---|
[1077] | 500 | except PyKotaConfigError : |
---|
[1192] | 501 | return _("You will soon be forbidden to print anymore because\nyour Print Quota is almost reached on printer %s.") % printername |
---|
[1077] | 502 | |
---|
[1875] | 503 | def getPrivacy(self) : |
---|
[3126] | 504 | """Returns True if privacy is activated, else False.""" |
---|
[1875] | 505 | return self.isTrue(self.getGlobalOption("privacy", ignore=1)) |
---|
| 506 | |
---|
[1021] | 507 | def getDebug(self) : |
---|
[3126] | 508 | """Returns True if debugging is activated, else False.""" |
---|
[1227] | 509 | return self.isTrue(self.getGlobalOption("debug", ignore=1)) |
---|
[1130] | 510 | |
---|
| 511 | def getCaching(self) : |
---|
[3126] | 512 | """Returns True if database caching is enabled, else False.""" |
---|
[1227] | 513 | return self.isTrue(self.getGlobalOption("storagecaching", ignore=1)) |
---|
[1148] | 514 | |
---|
[1356] | 515 | def getLDAPCache(self) : |
---|
[3126] | 516 | """Returns True if low-level LDAP caching is enabled, else False.""" |
---|
[1356] | 517 | return self.isTrue(self.getGlobalOption("ldapcache", ignore=1)) |
---|
| 518 | |
---|
[1148] | 519 | def getDisableHistory(self) : |
---|
[3126] | 520 | """Returns True if we want to disable history, else False.""" |
---|
[1227] | 521 | return self.isTrue(self.getGlobalOption("disablehistory", ignore=1)) |
---|
| 522 | |
---|
| 523 | def getUserNameToLower(self) : |
---|
[3169] | 524 | """Deprecated.""" |
---|
| 525 | return self.getGlobalOption("utolower", ignore=1) |
---|
[1757] | 526 | |
---|
[3169] | 527 | def getUserNameCase(self) : |
---|
| 528 | """Returns value for user name case: upper, lower or native""" |
---|
| 529 | validvalues = [ "upper", "lower", "native" ] |
---|
[3172] | 530 | try : |
---|
| 531 | value = self.getGlobalOption("usernamecase", ignore=1).strip().lower() |
---|
| 532 | except AttributeError : |
---|
[3169] | 533 | value = "native" |
---|
| 534 | if value not in validvalues : |
---|
| 535 | raise PyKotaConfigError, _("Option usernamecase only supports values in %s") % str(validvalues) |
---|
| 536 | return value |
---|
| 537 | |
---|
[1956] | 538 | def getRejectUnknown(self) : |
---|
[3126] | 539 | """Returns True if we want to reject the creation of unknown users or groups, else False.""" |
---|
[1956] | 540 | return self.isTrue(self.getGlobalOption("reject_unknown", ignore=1)) |
---|
| 541 | |
---|
[2405] | 542 | def getPrinterKeepFiles(self, printername) : |
---|
[3126] | 543 | """Returns True if files must be kept on disk, else False.""" |
---|
[2405] | 544 | try : |
---|
| 545 | return self.isTrue(self.getPrinterOption(printername, "keepfiles")) |
---|
| 546 | except PyKotaConfigError : |
---|
[3126] | 547 | return False |
---|
[2405] | 548 | |
---|
| 549 | def getPrinterDirectory(self, printername) : |
---|
| 550 | """Returns the path to our working directory, else a directory suitable for temporary files.""" |
---|
| 551 | try : |
---|
| 552 | return self.getPrinterOption(printername, "directory").strip() |
---|
| 553 | except PyKotaConfigError : |
---|
| 554 | return tempfile.gettempdir() |
---|
| 555 | |
---|
[2066] | 556 | def getDenyDuplicates(self, printername) : |
---|
[3126] | 557 | """Returns True or a command if we want to deny duplicate jobs, else False.""" |
---|
[2066] | 558 | try : |
---|
[2308] | 559 | denyduplicates = self.getPrinterOption(printername, "denyduplicates") |
---|
[2066] | 560 | except PyKotaConfigError : |
---|
[3126] | 561 | return False |
---|
[2308] | 562 | else : |
---|
| 563 | if self.isTrue(denyduplicates) : |
---|
[3126] | 564 | return True |
---|
[2308] | 565 | elif self.isFalse(denyduplicates) : |
---|
[3126] | 566 | return False |
---|
[2308] | 567 | else : |
---|
| 568 | # it's a command to run. |
---|
| 569 | return denyduplicates |
---|
[2692] | 570 | |
---|
| 571 | def getDuplicatesDelay(self, printername) : |
---|
| 572 | """Returns the number of seconds after which two identical jobs are not considered a duplicate anymore.""" |
---|
| 573 | try : |
---|
| 574 | duplicatesdelay = self.getPrinterOption(printername, "duplicatesdelay") |
---|
| 575 | except PyKotaConfigError : |
---|
| 576 | return 0 |
---|
| 577 | else : |
---|
| 578 | try : |
---|
| 579 | return int(duplicatesdelay) |
---|
| 580 | except (TypeError, ValueError) : |
---|
| 581 | raise PyKotaConfigError, _("Incorrect value %s for the duplicatesdelay directive in section %s") % (str(duplicatesdelay), printername) |
---|
[2066] | 582 | |
---|
[3025] | 583 | def getNoPrintingMaxDelay(self, printername) : |
---|
| 584 | """Returns the max number of seconds to wait for the printer to be in 'printing' mode.""" |
---|
| 585 | try : |
---|
| 586 | maxdelay = self.getPrinterOption(printername, "noprintingmaxdelay") |
---|
| 587 | except PyKotaConfigError : |
---|
| 588 | return None # tells to use hardcoded value |
---|
| 589 | else : |
---|
| 590 | try : |
---|
| 591 | maxdelay = int(maxdelay) |
---|
| 592 | if maxdelay < 0 : |
---|
| 593 | raise ValueError |
---|
| 594 | except (TypeError, ValueError) : |
---|
| 595 | raise PyKotaConfigError, _("Incorrect value %s for the noprintingmaxdelay directive in section %s") % (str(maxdelay), printername) |
---|
| 596 | else : |
---|
| 597 | return maxdelay |
---|
| 598 | |
---|
[3180] | 599 | def getStatusStabilizationLoops(self, printername) : |
---|
| 600 | """Returns the number of times the printer must return the 'idle' status to consider it stable.""" |
---|
| 601 | try : |
---|
| 602 | stab = self.getPrinterOption(printername, "statusstabilizationloops") |
---|
| 603 | except PyKotaConfigError : |
---|
| 604 | return None # tells to use hardcoded value |
---|
| 605 | else : |
---|
| 606 | try : |
---|
| 607 | stab = int(stab) |
---|
| 608 | if stab < 1 : |
---|
| 609 | raise ValueError |
---|
| 610 | except (TypeError, ValueError) : |
---|
| 611 | raise PyKotaConfigError, _("Incorrect value %s for the statusstabilizationloops directive in section %s") % (str(stab), printername) |
---|
| 612 | else : |
---|
| 613 | return stab |
---|
| 614 | |
---|
| 615 | def getStatusStabilizationDelay(self, printername) : |
---|
| 616 | """Returns the number of seconds to wait between two checks of the printer's status.""" |
---|
| 617 | try : |
---|
| 618 | stab = self.getPrinterOption(printername, "statusstabilizationdelay") |
---|
| 619 | except PyKotaConfigError : |
---|
| 620 | return None # tells to use hardcoded value |
---|
| 621 | else : |
---|
| 622 | try : |
---|
| 623 | stab = float(stab) |
---|
| 624 | if stab < 0.25 : |
---|
| 625 | raise ValueError |
---|
| 626 | except (TypeError, ValueError) : |
---|
| 627 | raise PyKotaConfigError, _("Incorrect value %s for the statusstabilizationdelay directive in section %s") % (str(stab), printername) |
---|
| 628 | else : |
---|
| 629 | return stab |
---|
| 630 | |
---|
[3190] | 631 | def getPrinterSNMPErrorMask(self, printername) : |
---|
| 632 | """Returns the SNMP error mask for a particular printer, or None if not defined.""" |
---|
| 633 | try : |
---|
| 634 | errmask = self.getPrinterOption(printername, "snmperrormask").lower() |
---|
| 635 | except PyKotaConfigError : |
---|
| 636 | return None # tells to use hardcoded value |
---|
| 637 | else : |
---|
| 638 | try : |
---|
| 639 | if errmask.startswith("0x") : |
---|
| 640 | value = int(errmask, 16) |
---|
| 641 | elif errmask.startswith("0") : |
---|
| 642 | value = int(errmask, 8) |
---|
| 643 | else : |
---|
| 644 | value = int(errmask) |
---|
| 645 | if 0 <= value < 65536 : |
---|
| 646 | return value |
---|
| 647 | else : |
---|
| 648 | raise ValueError |
---|
| 649 | except ValueError : |
---|
| 650 | raise PyKotaConfigError, _("Incorrect value %s for the snmperrormask directive in section %s") % (errmask, printername) |
---|
| 651 | |
---|
[1757] | 652 | def getWinbindSeparator(self) : |
---|
| 653 | """Returns the winbind separator's value if it is set, else None.""" |
---|
| 654 | return self.getGlobalOption("winbind_separator", ignore=1) |
---|
[1914] | 655 | |
---|
| 656 | def getAccountBanner(self, printername) : |
---|
| 657 | """Returns which banner(s) to account for: NONE, BOTH, STARTING, ENDING.""" |
---|
| 658 | validvalues = [ "NONE", "BOTH", "STARTING", "ENDING" ] |
---|
| 659 | try : |
---|
| 660 | value = self.getPrinterOption(printername, "accountbanner") |
---|
| 661 | except PyKotaConfigError : |
---|
| 662 | return "BOTH" # Default value of BOTH |
---|
| 663 | else : |
---|
[1916] | 664 | value = value.strip().upper() |
---|
[1914] | 665 | if value not in validvalues : |
---|
[1916] | 666 | raise PyKotaConfigError, _("Option accountbanner in section %s only supports values in %s") % (printername, str(validvalues)) |
---|
[1914] | 667 | return value |
---|
| 668 | |
---|
[3158] | 669 | def getAvoidDuplicateBanners(self, printername) : |
---|
| 670 | """Returns normalized value for avoiding extra banners. """ |
---|
| 671 | try : |
---|
| 672 | avoidduplicatebanners = self.getPrinterOption(printername, "avoidduplicatebanners").upper() |
---|
| 673 | except PyKotaConfigError : |
---|
| 674 | return "NO" |
---|
| 675 | else : |
---|
| 676 | try : |
---|
| 677 | value = int(avoidduplicatebanners) |
---|
| 678 | if value < 0 : |
---|
| 679 | raise ValueError |
---|
| 680 | except ValueError : |
---|
| 681 | if avoidduplicatebanners not in ["YES", "NO"] : |
---|
| 682 | raise PyKotaConfigError, _("Option avoidduplicatebanners only accepts 'yes', 'no', or a positive integer.") |
---|
| 683 | else : |
---|
| 684 | value = avoidduplicatebanners |
---|
| 685 | return value |
---|
| 686 | |
---|
[1914] | 687 | def getStartingBanner(self, printername) : |
---|
| 688 | """Returns the startingbanner value if set, else None.""" |
---|
| 689 | try : |
---|
[1916] | 690 | return self.getPrinterOption(printername, "startingbanner").strip() |
---|
[1914] | 691 | except PyKotaConfigError : |
---|
| 692 | return None |
---|
| 693 | |
---|
| 694 | def getEndingBanner(self, printername) : |
---|
| 695 | """Returns the endingbanner value if set, else None.""" |
---|
| 696 | try : |
---|
[1916] | 697 | return self.getPrinterOption(printername, "endingbanner").strip() |
---|
[1914] | 698 | except PyKotaConfigError : |
---|
| 699 | return None |
---|
[2062] | 700 | |
---|
| 701 | def getTrustJobSize(self, printername) : |
---|
| 702 | """Returns the normalized value of the trustjobsize's directive.""" |
---|
| 703 | try : |
---|
| 704 | value = self.getPrinterOption(printername, "trustjobsize").strip().upper() |
---|
| 705 | except PyKotaConfigError : |
---|
| 706 | return (None, "YES") |
---|
| 707 | else : |
---|
| 708 | if value == "YES" : |
---|
| 709 | return (None, "YES") |
---|
| 710 | try : |
---|
| 711 | (limit, replacement) = [p.strip() for p in value.split(">")[1].split(":")] |
---|
| 712 | limit = int(limit) |
---|
| 713 | try : |
---|
| 714 | replacement = int(replacement) |
---|
| 715 | except ValueError : |
---|
| 716 | if replacement != "PRECOMPUTED" : |
---|
| 717 | raise |
---|
| 718 | if limit < 0 : |
---|
| 719 | raise ValueError |
---|
| 720 | if (replacement != "PRECOMPUTED") and (replacement < 0) : |
---|
| 721 | raise ValueError |
---|
| 722 | except (IndexError, ValueError, TypeError) : |
---|
| 723 | raise PyKotaConfigError, _("Option trustjobsize for printer %s is incorrect") % printername |
---|
| 724 | return (limit, replacement) |
---|
[3031] | 725 | |
---|
| 726 | def getPrinterCoefficients(self, printername) : |
---|
| 727 | """Returns a mapping of coefficients for a particular printer.""" |
---|
| 728 | branchbasename = "coefficient_" |
---|
| 729 | try : |
---|
| 730 | globalbranches = [ (k, self.config.get("global", k)) for k in self.config.options("global") if k.startswith(branchbasename) ] |
---|
| 731 | except ConfigParser.NoSectionError, msg : |
---|
| 732 | raise PyKotaConfigError, "Invalid configuration file : %s" % msg |
---|
| 733 | try : |
---|
| 734 | sectionbranches = [ (k, self.config.get(printername, k)) for k in self.config.options(printername) if k.startswith(branchbasename) ] |
---|
| 735 | except ConfigParser.NoSectionError, msg : |
---|
| 736 | sectionbranches = [] |
---|
| 737 | branches = {} |
---|
| 738 | for (k, v) in globalbranches : |
---|
[3035] | 739 | k = k.split('_', 1)[1] |
---|
[3031] | 740 | value = v.strip() |
---|
| 741 | if value : |
---|
| 742 | try : |
---|
| 743 | branches[k] = float(value) |
---|
| 744 | except ValueError : |
---|
| 745 | raise PyKotaConfigError, "Invalid coefficient %s (%s) for printer %s" % (k, value, printername) |
---|
| 746 | |
---|
| 747 | for (k, v) in sectionbranches : |
---|
[3035] | 748 | k = k.split('_', 1)[1] |
---|
[3031] | 749 | value = v.strip() |
---|
| 750 | if value : |
---|
| 751 | try : |
---|
| 752 | branches[k] = float(value) # overwrite any global option or set a new value |
---|
| 753 | except ValueError : |
---|
| 754 | raise PyKotaConfigError, "Invalid coefficient %s (%s) for printer %s" % (k, value, printername) |
---|
| 755 | else : |
---|
| 756 | del branches[k] # empty value disables a global option |
---|
| 757 | return branches |
---|
[3162] | 758 | |
---|
| 759 | def getPrinterSkipInitialWait(self, printername) : |
---|
| 760 | """Returns True if we want to skip the initial waiting loop, else False.""" |
---|
| 761 | try : |
---|
| 762 | return self.isTrue(self.getPrinterOption(printername, "skipinitialwait")) |
---|
| 763 | except PyKotaConfigError : |
---|
| 764 | return False |
---|