[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 |
---|
[2302] | 19 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
[695] | 20 | # |
---|
| 21 | # $Id$ |
---|
| 22 | # |
---|
[2074] | 23 | # |
---|
[695] | 24 | |
---|
| 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) : |
---|
| 56 | """Returns 1 if option is set to true, else 0.""" |
---|
[2308] | 57 | if (option is not None) and (option.strip().upper() in ['Y', 'YES', '1', 'ON', 'T', 'TRUE']) : |
---|
[1227] | 58 | return 1 |
---|
| 59 | else : |
---|
| 60 | return 0 |
---|
[695] | 61 | |
---|
[2308] | 62 | def isFalse(self, option) : |
---|
| 63 | """Returns 1 if option is set to false, else 0.""" |
---|
| 64 | if (option is not None) and (option.strip().upper() in ['N', 'NO', '0', 'OFF', 'F', 'FALSE']) : |
---|
| 65 | return 1 |
---|
| 66 | else : |
---|
| 67 | return 0 |
---|
| 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 : |
---|
| 79 | return |
---|
| 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 \ |
---|
| 183 | "http://www.librelogiciel.com/software/PyKota/pykota.png" |
---|
| 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 \ |
---|
| 189 | "http://www.librelogiciel.com/software/" |
---|
| 190 | return url.strip() |
---|
[2262] | 191 | |
---|
[1192] | 192 | def getAccounterBackend(self, printername) : |
---|
[973] | 193 | """Returns the accounter backend to use for a given printer. |
---|
| 194 | |
---|
[1475] | 195 | if it is not set, it defaults to 'hardware' which means ask printer |
---|
[973] | 196 | for its internal lifetime page counter. |
---|
| 197 | """ |
---|
[1475] | 198 | validaccounters = [ "hardware", "software" ] |
---|
[1371] | 199 | fullaccounter = self.getPrinterOption(printername, "accounter").strip() |
---|
[1483] | 200 | flower = fullaccounter.lower() |
---|
| 201 | if flower.startswith("software") or flower.startswith("hardware") : |
---|
[980] | 202 | try : |
---|
| 203 | (accounter, args) = [x.strip() for x in fullaccounter.split('(', 1)] |
---|
| 204 | except ValueError : |
---|
[1483] | 205 | raise PyKotaConfigError, _("Invalid accounter %s for printer %s") % (fullaccounter, printername) |
---|
[980] | 206 | if args.endswith(')') : |
---|
[2074] | 207 | args = args[:-1].strip() |
---|
| 208 | if (accounter == "hardware") and not args : |
---|
[1483] | 209 | raise PyKotaConfigError, _("Invalid accounter %s for printer %s") % (fullaccounter, printername) |
---|
[2422] | 210 | return (accounter.lower(), args) |
---|
[1483] | 211 | else : |
---|
[1192] | 212 | raise PyKotaConfigError, _("Option accounter in section %s only supports values in %s") % (printername, str(validaccounters)) |
---|
[973] | 213 | |
---|
[1371] | 214 | def getPreHook(self, printername) : |
---|
| 215 | """Returns the prehook command line to launch, or None if unset.""" |
---|
| 216 | try : |
---|
| 217 | return self.getPrinterOption(printername, "prehook").strip() |
---|
| 218 | except PyKotaConfigError : |
---|
| 219 | return # No command to launch in the pre-hook |
---|
| 220 | |
---|
| 221 | def getPostHook(self, printername) : |
---|
| 222 | """Returns the posthook command line to launch, or None if unset.""" |
---|
| 223 | try : |
---|
| 224 | return self.getPrinterOption(printername, "posthook").strip() |
---|
| 225 | except PyKotaConfigError : |
---|
| 226 | return # No command to launch in the post-hook |
---|
| 227 | |
---|
[2307] | 228 | def getStripTitle(self, printername) : |
---|
| 229 | """Returns the striptitle directive's content, or None if unset.""" |
---|
| 230 | try : |
---|
| 231 | return self.getPrinterOption(printername, "striptitle").strip() |
---|
| 232 | except PyKotaConfigError : |
---|
| 233 | return # No prefix to strip off |
---|
| 234 | |
---|
[2385] | 235 | def getOverwriteJobTicket(self, printername) : |
---|
| 236 | """Returns the overwrite_jobticket directive's content, or None if unset.""" |
---|
| 237 | try : |
---|
| 238 | return self.getPrinterOption(printername, "overwrite_jobticket").strip() |
---|
| 239 | except PyKotaConfigError : |
---|
| 240 | return # No overwriting will be done |
---|
| 241 | |
---|
| 242 | def getUnknownBillingCode(self, printername) : |
---|
| 243 | """Returns the unknown_billingcode directive's content, or the default value if unset.""" |
---|
| 244 | validvalues = [ "CREATE", "DENY" ] |
---|
| 245 | try : |
---|
| 246 | fullvalue = self.getPrinterOption(printername, "unknown_billingcode") |
---|
| 247 | except PyKotaConfigError : |
---|
| 248 | return ("CREATE", None) |
---|
| 249 | else : |
---|
| 250 | try : |
---|
| 251 | value = [x.strip() for x in fullvalue.split('(', 1)] |
---|
| 252 | except ValueError : |
---|
| 253 | raise PyKotaConfigError, _("Invalid unknown_billingcode directive %s for printer %s") % (fullvalue, printername) |
---|
| 254 | if len(value) == 1 : |
---|
| 255 | value.append("") |
---|
| 256 | (value, args) = value |
---|
| 257 | if args.endswith(')') : |
---|
| 258 | args = args[:-1] |
---|
| 259 | value = value.upper() |
---|
| 260 | if (value == "DENY") and not args : |
---|
| 261 | return ("DENY", None) |
---|
| 262 | if value not in validvalues : |
---|
| 263 | raise PyKotaConfigError, _("Directive unknown_billingcode in section %s only supports values in %s") % (printername, str(validvalues)) |
---|
| 264 | return (value, args) |
---|
| 265 | |
---|
[1495] | 266 | def getPrinterEnforcement(self, printername) : |
---|
| 267 | """Returns if quota enforcement should be strict or laxist for the current printer.""" |
---|
| 268 | validenforcements = [ "STRICT", "LAXIST" ] |
---|
| 269 | try : |
---|
| 270 | enforcement = self.getPrinterOption(printername, "enforcement") |
---|
| 271 | except PyKotaConfigError : |
---|
| 272 | return "LAXIST" |
---|
| 273 | else : |
---|
| 274 | enforcement = enforcement.upper() |
---|
| 275 | if enforcement not in validenforcements : |
---|
| 276 | raise PyKotaConfigError, _("Option enforcement in section %s only supports values in %s") % (printername, str(validenforcements)) |
---|
| 277 | return enforcement |
---|
| 278 | |
---|
[2583] | 279 | def getPrinterOnBackendError(self, printername) : |
---|
| 280 | """Returns what must be done whenever the real CUPS backend fails.""" |
---|
| 281 | validactions = [ "CHARGE", "NOCHARGE" ] |
---|
| 282 | try : |
---|
| 283 | action = self.getPrinterOption(printername, "onbackenderror") |
---|
| 284 | except PyKotaConfigError : |
---|
| 285 | return "NOCHARGE" |
---|
| 286 | else : |
---|
| 287 | action = action.upper() |
---|
| 288 | if action not in validactions : |
---|
| 289 | raise PyKotaConfigError, _("Option onbackenderror in section %s only supports values in %s") % (printername, str(validactions)) |
---|
| 290 | return action |
---|
| 291 | |
---|
[1687] | 292 | def getPrinterOnAccounterError(self, printername) : |
---|
| 293 | """Returns what must be done whenever the accounter fails.""" |
---|
| 294 | validactions = [ "CONTINUE", "STOP" ] |
---|
| 295 | try : |
---|
| 296 | action = self.getPrinterOption(printername, "onaccountererror") |
---|
| 297 | except PyKotaConfigError : |
---|
| 298 | return "STOP" |
---|
| 299 | else : |
---|
| 300 | action = action.upper() |
---|
| 301 | if action not in validactions : |
---|
| 302 | raise PyKotaConfigError, _("Option onaccountererror in section %s only supports values in %s") % (printername, str(validactions)) |
---|
| 303 | return action |
---|
| 304 | |
---|
[1192] | 305 | def getPrinterPolicy(self, printername) : |
---|
[695] | 306 | """Returns the default policy for the current printer.""" |
---|
[1152] | 307 | validpolicies = [ "ALLOW", "DENY", "EXTERNAL" ] |
---|
[853] | 308 | try : |
---|
[1192] | 309 | fullpolicy = self.getPrinterOption(printername, "policy") |
---|
[853] | 310 | except PyKotaConfigError : |
---|
[1152] | 311 | return ("DENY", None) |
---|
| 312 | else : |
---|
| 313 | try : |
---|
| 314 | policy = [x.strip() for x in fullpolicy.split('(', 1)] |
---|
| 315 | except ValueError : |
---|
[1192] | 316 | raise PyKotaConfigError, _("Invalid policy %s for printer %s") % (fullpolicy, printername) |
---|
[1152] | 317 | if len(policy) == 1 : |
---|
| 318 | policy.append("") |
---|
| 319 | (policy, args) = policy |
---|
| 320 | if args.endswith(')') : |
---|
| 321 | args = args[:-1] |
---|
| 322 | policy = policy.upper() |
---|
| 323 | if (policy == "EXTERNAL") and not args : |
---|
[1192] | 324 | raise PyKotaConfigError, _("Invalid policy %s for printer %s") % (fullpolicy, printername) |
---|
[1152] | 325 | if policy not in validpolicies : |
---|
[1192] | 326 | raise PyKotaConfigError, _("Option policy in section %s only supports values in %s") % (printername, str(validpolicies)) |
---|
[1152] | 327 | return (policy, args) |
---|
[695] | 328 | |
---|
[1518] | 329 | def getCrashRecipient(self) : |
---|
| 330 | """Returns the email address of the software crash messages recipient.""" |
---|
| 331 | try : |
---|
| 332 | return self.getGlobalOption("crashrecipient") |
---|
| 333 | except : |
---|
| 334 | return |
---|
| 335 | |
---|
[695] | 336 | def getSMTPServer(self) : |
---|
| 337 | """Returns the SMTP server to use to send messages to users.""" |
---|
[853] | 338 | try : |
---|
| 339 | return self.getGlobalOption("smtpserver") |
---|
| 340 | except PyKotaConfigError : |
---|
| 341 | return "localhost" |
---|
[695] | 342 | |
---|
[1353] | 343 | def getMailDomain(self) : |
---|
| 344 | """Returns the mail domain to use to send messages to users.""" |
---|
| 345 | try : |
---|
| 346 | return self.getGlobalOption("maildomain") |
---|
| 347 | except PyKotaConfigError : |
---|
| 348 | return |
---|
| 349 | |
---|
[1192] | 350 | def getAdminMail(self, printername) : |
---|
[695] | 351 | """Returns the Email address of the Print Quota Administrator.""" |
---|
[853] | 352 | try : |
---|
[1192] | 353 | return self.getPrinterOption(printername, "adminmail") |
---|
[853] | 354 | except PyKotaConfigError : |
---|
| 355 | return "root@localhost" |
---|
[695] | 356 | |
---|
[1192] | 357 | def getAdmin(self, printername) : |
---|
[695] | 358 | """Returns the full name of the Print Quota Administrator.""" |
---|
[853] | 359 | try : |
---|
[1192] | 360 | return self.getPrinterOption(printername, "admin") |
---|
[853] | 361 | except PyKotaConfigError : |
---|
| 362 | return "root" |
---|
[708] | 363 | |
---|
[1192] | 364 | def getMailTo(self, printername) : |
---|
[852] | 365 | """Returns the recipient of email messages.""" |
---|
[1192] | 366 | validmailtos = [ "EXTERNAL", "NOBODY", "NONE", "NOONE", "BITBUCKET", "DEVNULL", "BOTH", "USER", "ADMIN" ] |
---|
[853] | 367 | try : |
---|
[1192] | 368 | fullmailto = self.getPrinterOption(printername, "mailto") |
---|
[853] | 369 | except PyKotaConfigError : |
---|
[1192] | 370 | return ("BOTH", None) |
---|
| 371 | else : |
---|
| 372 | try : |
---|
| 373 | mailto = [x.strip() for x in fullmailto.split('(', 1)] |
---|
| 374 | except ValueError : |
---|
| 375 | raise PyKotaConfigError, _("Invalid option mailto %s for printer %s") % (fullmailto, printername) |
---|
| 376 | if len(mailto) == 1 : |
---|
| 377 | mailto.append("") |
---|
| 378 | (mailto, args) = mailto |
---|
| 379 | if args.endswith(')') : |
---|
| 380 | args = args[:-1] |
---|
| 381 | mailto = mailto.upper() |
---|
| 382 | if (mailto == "EXTERNAL") and not args : |
---|
| 383 | raise PyKotaConfigError, _("Invalid option mailto %s for printer %s") % (fullmailto, printername) |
---|
| 384 | if mailto not in validmailtos : |
---|
| 385 | raise PyKotaConfigError, _("Option mailto in section %s only supports values in %s") % (printername, str(validmailtos)) |
---|
| 386 | return (mailto, args) |
---|
[852] | 387 | |
---|
[2054] | 388 | def getMaxDenyBanners(self, printername) : |
---|
| 389 | """Returns the maximum number of deny banners to be printed for a particular user on a particular printer.""" |
---|
| 390 | try : |
---|
| 391 | maxdb = self.getPrinterOption(printername, "maxdenybanners") |
---|
| 392 | except PyKotaConfigError : |
---|
| 393 | return 0 # default value is to forbid printing a deny banner. |
---|
| 394 | try : |
---|
| 395 | value = int(maxdb.strip()) |
---|
| 396 | if value < 0 : |
---|
| 397 | raise ValueError |
---|
| 398 | except (TypeError, ValueError) : |
---|
| 399 | raise PyKotaConfigError, _("Invalid maximal deny banners counter %s") % maxdb |
---|
| 400 | else : |
---|
| 401 | return value |
---|
| 402 | |
---|
[1192] | 403 | def getGraceDelay(self, printername) : |
---|
[708] | 404 | """Returns the grace delay in days.""" |
---|
[731] | 405 | try : |
---|
[1192] | 406 | gd = self.getPrinterOption(printername, "gracedelay") |
---|
[853] | 407 | except PyKotaConfigError : |
---|
[1914] | 408 | gd = 7 # default value of 7 days |
---|
[853] | 409 | try : |
---|
[731] | 410 | return int(gd) |
---|
[1077] | 411 | except (TypeError, ValueError) : |
---|
[773] | 412 | raise PyKotaConfigError, _("Invalid grace delay %s") % gd |
---|
[1021] | 413 | |
---|
[1077] | 414 | def getPoorMan(self) : |
---|
[1646] | 415 | """Returns the poor man's threshold.""" |
---|
[1077] | 416 | try : |
---|
| 417 | pm = self.getGlobalOption("poorman") |
---|
| 418 | except PyKotaConfigError : |
---|
[1914] | 419 | pm = 1.0 # default value of 1 unit |
---|
[1077] | 420 | try : |
---|
| 421 | return float(pm) |
---|
| 422 | except (TypeError, ValueError) : |
---|
[1646] | 423 | raise PyKotaConfigError, _("Invalid poor man's threshold %s") % pm |
---|
[1077] | 424 | |
---|
| 425 | def getPoorWarn(self) : |
---|
| 426 | """Returns the poor man's warning message.""" |
---|
| 427 | try : |
---|
| 428 | return self.getGlobalOption("poorwarn") |
---|
| 429 | except PyKotaConfigError : |
---|
| 430 | 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.") |
---|
| 431 | |
---|
[1192] | 432 | def getHardWarn(self, printername) : |
---|
[1077] | 433 | """Returns the hard limit error message.""" |
---|
| 434 | try : |
---|
[1192] | 435 | return self.getPrinterOption(printername, "hardwarn") |
---|
[1077] | 436 | except PyKotaConfigError : |
---|
[1192] | 437 | return _("You are not allowed to print anymore because\nyour Print Quota is exceeded on printer %s.") % printername |
---|
[1077] | 438 | |
---|
[1192] | 439 | def getSoftWarn(self, printername) : |
---|
[1077] | 440 | """Returns the soft limit error message.""" |
---|
| 441 | try : |
---|
[1192] | 442 | return self.getPrinterOption(printername, "softwarn") |
---|
[1077] | 443 | except PyKotaConfigError : |
---|
[1192] | 444 | return _("You will soon be forbidden to print anymore because\nyour Print Quota is almost reached on printer %s.") % printername |
---|
[1077] | 445 | |
---|
[1875] | 446 | def getPrivacy(self) : |
---|
| 447 | """Returns 1 if privacy is activated, else 0.""" |
---|
| 448 | return self.isTrue(self.getGlobalOption("privacy", ignore=1)) |
---|
| 449 | |
---|
[1021] | 450 | def getDebug(self) : |
---|
| 451 | """Returns 1 if debugging is activated, else 0.""" |
---|
[1227] | 452 | return self.isTrue(self.getGlobalOption("debug", ignore=1)) |
---|
[1130] | 453 | |
---|
| 454 | def getCaching(self) : |
---|
| 455 | """Returns 1 if database caching is enabled, else 0.""" |
---|
[1227] | 456 | return self.isTrue(self.getGlobalOption("storagecaching", ignore=1)) |
---|
[1148] | 457 | |
---|
[1356] | 458 | def getLDAPCache(self) : |
---|
| 459 | """Returns 1 if low-level LDAP caching is enabled, else 0.""" |
---|
| 460 | return self.isTrue(self.getGlobalOption("ldapcache", ignore=1)) |
---|
| 461 | |
---|
[1148] | 462 | def getDisableHistory(self) : |
---|
| 463 | """Returns 1 if we want to disable history, else 0.""" |
---|
[1227] | 464 | return self.isTrue(self.getGlobalOption("disablehistory", ignore=1)) |
---|
| 465 | |
---|
| 466 | def getUserNameToLower(self) : |
---|
| 467 | """Returns 1 if we want to convert usernames to lowercase when printing, else 0.""" |
---|
| 468 | return self.isTrue(self.getGlobalOption("utolower", ignore=1)) |
---|
[1757] | 469 | |
---|
[1956] | 470 | def getRejectUnknown(self) : |
---|
| 471 | """Returns 1 if we want to reject the creation of unknown users or groups, else 0.""" |
---|
| 472 | return self.isTrue(self.getGlobalOption("reject_unknown", ignore=1)) |
---|
| 473 | |
---|
[2405] | 474 | def getPrinterKeepFiles(self, printername) : |
---|
| 475 | """Returns 1 if files must be kept on disk, else 0.""" |
---|
| 476 | try : |
---|
| 477 | return self.isTrue(self.getPrinterOption(printername, "keepfiles")) |
---|
| 478 | except PyKotaConfigError : |
---|
| 479 | return 0 |
---|
| 480 | |
---|
| 481 | def getPrinterDirectory(self, printername) : |
---|
| 482 | """Returns the path to our working directory, else a directory suitable for temporary files.""" |
---|
| 483 | try : |
---|
| 484 | return self.getPrinterOption(printername, "directory").strip() |
---|
| 485 | except PyKotaConfigError : |
---|
| 486 | return tempfile.gettempdir() |
---|
| 487 | |
---|
[2066] | 488 | def getDenyDuplicates(self, printername) : |
---|
[2308] | 489 | """Returns 1 or a command if we want to deny duplicate jobs, else 0.""" |
---|
[2066] | 490 | try : |
---|
[2308] | 491 | denyduplicates = self.getPrinterOption(printername, "denyduplicates") |
---|
[2066] | 492 | except PyKotaConfigError : |
---|
| 493 | return 0 |
---|
[2308] | 494 | else : |
---|
| 495 | if self.isTrue(denyduplicates) : |
---|
| 496 | return 1 |
---|
| 497 | elif self.isFalse(denyduplicates) : |
---|
| 498 | return 0 |
---|
| 499 | else : |
---|
| 500 | # it's a command to run. |
---|
| 501 | return denyduplicates |
---|
[2066] | 502 | |
---|
[1757] | 503 | def getWinbindSeparator(self) : |
---|
| 504 | """Returns the winbind separator's value if it is set, else None.""" |
---|
| 505 | return self.getGlobalOption("winbind_separator", ignore=1) |
---|
[1914] | 506 | |
---|
| 507 | def getAccountBanner(self, printername) : |
---|
| 508 | """Returns which banner(s) to account for: NONE, BOTH, STARTING, ENDING.""" |
---|
| 509 | validvalues = [ "NONE", "BOTH", "STARTING", "ENDING" ] |
---|
| 510 | try : |
---|
| 511 | value = self.getPrinterOption(printername, "accountbanner") |
---|
| 512 | except PyKotaConfigError : |
---|
| 513 | return "BOTH" # Default value of BOTH |
---|
| 514 | else : |
---|
[1916] | 515 | value = value.strip().upper() |
---|
[1914] | 516 | if value not in validvalues : |
---|
[1916] | 517 | raise PyKotaConfigError, _("Option accountbanner in section %s only supports values in %s") % (printername, str(validvalues)) |
---|
[1914] | 518 | return value |
---|
| 519 | |
---|
| 520 | def getStartingBanner(self, printername) : |
---|
| 521 | """Returns the startingbanner value if set, else None.""" |
---|
| 522 | try : |
---|
[1916] | 523 | return self.getPrinterOption(printername, "startingbanner").strip() |
---|
[1914] | 524 | except PyKotaConfigError : |
---|
| 525 | return None |
---|
| 526 | |
---|
| 527 | def getEndingBanner(self, printername) : |
---|
| 528 | """Returns the endingbanner value if set, else None.""" |
---|
| 529 | try : |
---|
[1916] | 530 | return self.getPrinterOption(printername, "endingbanner").strip() |
---|
[1914] | 531 | except PyKotaConfigError : |
---|
| 532 | return None |
---|
[2062] | 533 | |
---|
| 534 | def getTrustJobSize(self, printername) : |
---|
| 535 | """Returns the normalized value of the trustjobsize's directive.""" |
---|
| 536 | try : |
---|
| 537 | value = self.getPrinterOption(printername, "trustjobsize").strip().upper() |
---|
| 538 | except PyKotaConfigError : |
---|
| 539 | return (None, "YES") |
---|
| 540 | else : |
---|
| 541 | if value == "YES" : |
---|
| 542 | return (None, "YES") |
---|
| 543 | try : |
---|
| 544 | (limit, replacement) = [p.strip() for p in value.split(">")[1].split(":")] |
---|
| 545 | limit = int(limit) |
---|
| 546 | try : |
---|
| 547 | replacement = int(replacement) |
---|
| 548 | except ValueError : |
---|
| 549 | if replacement != "PRECOMPUTED" : |
---|
| 550 | raise |
---|
| 551 | if limit < 0 : |
---|
| 552 | raise ValueError |
---|
| 553 | if (replacement != "PRECOMPUTED") and (replacement < 0) : |
---|
| 554 | raise ValueError |
---|
| 555 | except (IndexError, ValueError, TypeError) : |
---|
| 556 | raise PyKotaConfigError, _("Option trustjobsize for printer %s is incorrect") % printername |
---|
| 557 | return (limit, replacement) |
---|