[695] | 1 | # PyKota |
---|
| 2 | # |
---|
| 3 | # PyKota : Print Quotas for CUPS |
---|
| 4 | # |
---|
| 5 | # (c) 2003 Jerome Alet <alet@librelogiciel.com> |
---|
| 6 | # You're welcome to redistribute this software under the |
---|
| 7 | # terms of the GNU General Public Licence version 2.0 |
---|
| 8 | # or, at your option, any higher version. |
---|
| 9 | # |
---|
| 10 | # You can read the complete GNU GPL in the file COPYING |
---|
| 11 | # which should come along with this software, or visit |
---|
| 12 | # the Free Software Foundation's WEB site http://www.fsf.org |
---|
| 13 | # |
---|
| 14 | # $Id$ |
---|
| 15 | # |
---|
| 16 | # $Log$ |
---|
[802] | 17 | # Revision 1.15 2003/02/17 22:55:01 jalet |
---|
| 18 | # More options can now be set per printer or globally : |
---|
| 19 | # |
---|
| 20 | # admin |
---|
| 21 | # adminmail |
---|
| 22 | # gracedelay |
---|
| 23 | # requester |
---|
| 24 | # |
---|
| 25 | # the printer option has priority when both are defined. |
---|
| 26 | # |
---|
[800] | 27 | # Revision 1.14 2003/02/17 22:05:50 jalet |
---|
| 28 | # Storage backend now supports admin and user passwords (untested) |
---|
| 29 | # |
---|
[789] | 30 | # Revision 1.13 2003/02/10 11:47:39 jalet |
---|
| 31 | # Moved some code down into the requesters |
---|
| 32 | # |
---|
[786] | 33 | # Revision 1.12 2003/02/10 10:36:33 jalet |
---|
| 34 | # Small problem wrt external requester |
---|
| 35 | # |
---|
[785] | 36 | # Revision 1.11 2003/02/10 08:50:45 jalet |
---|
| 37 | # External requester seems to be finally ok now |
---|
| 38 | # |
---|
[783] | 39 | # Revision 1.10 2003/02/10 08:19:57 jalet |
---|
| 40 | # tell ConfigParser to return raw data, this allows our own strings |
---|
| 41 | # interpolations in the requester |
---|
| 42 | # |
---|
[781] | 43 | # Revision 1.9 2003/02/10 00:44:38 jalet |
---|
| 44 | # Typos |
---|
| 45 | # |
---|
[780] | 46 | # Revision 1.8 2003/02/10 00:42:17 jalet |
---|
| 47 | # External requester should be ok (untested) |
---|
| 48 | # New syntax for configuration file wrt requesters |
---|
| 49 | # |
---|
[773] | 50 | # Revision 1.7 2003/02/09 13:05:43 jalet |
---|
| 51 | # Internationalization continues... |
---|
| 52 | # |
---|
[747] | 53 | # Revision 1.6 2003/02/07 22:00:09 jalet |
---|
| 54 | # Bad cut&paste |
---|
| 55 | # |
---|
[731] | 56 | # Revision 1.5 2003/02/06 23:58:05 jalet |
---|
| 57 | # repykota should be ok |
---|
| 58 | # |
---|
[713] | 59 | # Revision 1.4 2003/02/06 09:19:02 jalet |
---|
| 60 | # More robust behavior (hopefully) when the user or printer is not managed |
---|
| 61 | # correctly by the Quota System : e.g. cupsFilter added in ppd file, but |
---|
| 62 | # printer and/or user not 'yet?' in storage. |
---|
| 63 | # |
---|
[708] | 64 | # Revision 1.3 2003/02/05 23:26:22 jalet |
---|
| 65 | # Incorrect handling of grace delay |
---|
| 66 | # |
---|
[707] | 67 | # Revision 1.2 2003/02/05 23:09:20 jalet |
---|
| 68 | # Name conflict |
---|
| 69 | # |
---|
[695] | 70 | # Revision 1.1 2003/02/05 21:28:17 jalet |
---|
| 71 | # Initial import into CVS |
---|
| 72 | # |
---|
| 73 | # |
---|
| 74 | # |
---|
| 75 | |
---|
| 76 | import sys |
---|
| 77 | import os |
---|
| 78 | import ConfigParser |
---|
| 79 | |
---|
| 80 | class PyKotaConfigError(Exception): |
---|
| 81 | """An exception for PyKota config related stuff.""" |
---|
| 82 | def __init__(self, message = ""): |
---|
| 83 | self.message = message |
---|
| 84 | Exception.__init__(self, message) |
---|
| 85 | def __repr__(self): |
---|
| 86 | return self.message |
---|
| 87 | __str__ = __repr__ |
---|
| 88 | |
---|
| 89 | class PyKotaConfig : |
---|
| 90 | """A class to deal with PyKota's configuration.""" |
---|
| 91 | def __init__(self, directory) : |
---|
| 92 | """Reads and checks the configuration file.""" |
---|
| 93 | self.filename = os.path.join(directory, "pykota.conf") |
---|
| 94 | self.config = ConfigParser.ConfigParser() |
---|
| 95 | self.config.read([self.filename]) |
---|
| 96 | self.checkConfiguration() |
---|
| 97 | |
---|
| 98 | def checkConfiguration(self) : |
---|
| 99 | """Checks if configuration is correct. |
---|
| 100 | |
---|
| 101 | raises PyKotaConfigError in case a problem is detected |
---|
| 102 | """ |
---|
| 103 | validmethods = [ "lazy" ] # TODO add more methods |
---|
[783] | 104 | if self.config.get("global", "method", raw=1).lower() not in validmethods : |
---|
[773] | 105 | raise PyKotaConfigError, _("Option method only supports values in %s") % str(validmethods) |
---|
[695] | 106 | |
---|
| 107 | def getPrinterNames(self) : |
---|
| 108 | """Returns the list of configured printers, i.e. all sections names minus 'global'.""" |
---|
| 109 | return [pname for pname in self.config.sections() if pname != "global"] |
---|
| 110 | |
---|
[802] | 111 | def getGlobalOption(self, option, ignore=0) : |
---|
| 112 | """Returns an option from the global section, or raises a PyKotaConfigError if ignore is not set, else returns None.""" |
---|
| 113 | try : |
---|
| 114 | return self.config.get("global", option, raw=1) |
---|
| 115 | except (ConfigParser.NoSectionError, ConfigParser.NoOptionError) : |
---|
| 116 | if ignore : |
---|
| 117 | return |
---|
| 118 | else : |
---|
| 119 | raise PyKotaConfigError, _("Option %s not found in section global of %s") % (option, self.filename) |
---|
| 120 | |
---|
| 121 | def getPrinterOption(self, printer, option) : |
---|
| 122 | """Returns an option from the printer section, or the global section, or raises a PyKotaConfigError.""" |
---|
| 123 | globaloption = self.getGlobalOption(option, ignore=1) |
---|
| 124 | try : |
---|
| 125 | return self.config.get(printer, option, raw=1) |
---|
| 126 | except (ConfigParser.NoSectionError, ConfigParser.NoOptionError) : |
---|
| 127 | if globaloption is not None : |
---|
| 128 | return globaloption |
---|
| 129 | else : |
---|
| 130 | raise PyKotaConfigError, _("Option %s not found in section %s of %s") % (option, printer, self.filename) |
---|
| 131 | |
---|
[695] | 132 | def getStorageBackend(self) : |
---|
[800] | 133 | """Returns the storage backend information as a Python mapping.""" |
---|
| 134 | backendinfo = {} |
---|
[695] | 135 | for option in [ "storagebackend", "storageserver", \ |
---|
| 136 | "storagename", "storageadmin", \ |
---|
[800] | 137 | "storageuser", \ |
---|
[695] | 138 | ] : |
---|
[802] | 139 | backendinfo[option] = self.getGlobalOption((option) |
---|
[800] | 140 | for option in [ "storageadminpw", "storageuserpw" ] : |
---|
[802] | 141 | backendinfo[option] = self.getGlobalOption(option, ignore=1) |
---|
[800] | 142 | return backendinfo |
---|
[695] | 143 | |
---|
| 144 | def getLoggingBackend(self) : |
---|
| 145 | """Returns the logging backend information.""" |
---|
[802] | 146 | validloggers = [ "stderr", "system" ] |
---|
| 147 | logger = self.getGlobalOption("logger").lower() |
---|
| 148 | if logger not in validloggers : |
---|
| 149 | raise PyKotaConfigError, _("Option logger only supports values in %s") % str(validloggers) |
---|
| 150 | return logger |
---|
[695] | 151 | |
---|
| 152 | def getRequesterBackend(self, printer) : |
---|
[780] | 153 | """Returns the requester backend to use for a given printer, with its arguments.""" |
---|
[802] | 154 | fullrequester = self.getPrinterOption(printer, "requester") |
---|
| 155 | try : |
---|
| 156 | (requester, args) = [x.strip() for x in fullrequester.split('(', 1)] |
---|
| 157 | except ValueError : |
---|
| 158 | raise PyKotaConfigError, _("Invalid requester %s for printer %s") % (fullrequester, printer) |
---|
[780] | 159 | if args.endswith(')') : |
---|
| 160 | args = args[:-1] |
---|
| 161 | if not args : |
---|
[786] | 162 | raise PyKotaConfigError, _("Invalid requester %s for printer %s") % (fullrequester, printer) |
---|
[802] | 163 | validrequesters = [ "snmp", "external" ] # TODO : add more requesters |
---|
| 164 | if requester not in validrequesters : |
---|
| 165 | raise PyKotaConfigError, _("Option requester for printer %s only supports values in %s") % (printer, str(validrequesters)) |
---|
[780] | 166 | return (requester, args) |
---|
[695] | 167 | |
---|
| 168 | def getPrinterPolicy(self, printer) : |
---|
| 169 | """Returns the default policy for the current printer.""" |
---|
[802] | 170 | validpolicies = [ "ALLOW", "DENY" ] |
---|
| 171 | policy = self.getPrinterOption(printer, "policy").upper() |
---|
| 172 | if policy not in validpolicies : |
---|
| 173 | raise PyKotaConfigError, _("Option policy in section %s only supports values in %s") % (printer, str(validpolicies)) |
---|
| 174 | return policy |
---|
[695] | 175 | |
---|
| 176 | def getSMTPServer(self) : |
---|
| 177 | """Returns the SMTP server to use to send messages to users.""" |
---|
[802] | 178 | return self.getGlobalOption("smtpserver") |
---|
[695] | 179 | |
---|
[802] | 180 | def getAdminMail(self, printer) : |
---|
[695] | 181 | """Returns the Email address of the Print Quota Administrator.""" |
---|
[802] | 182 | return self.getPrinterOption(printer, "adminmail") |
---|
[695] | 183 | |
---|
[802] | 184 | def getAdmin(self, printer) : |
---|
[695] | 185 | """Returns the full name of the Print Quota Administrator.""" |
---|
[802] | 186 | return self.getPrinterOption(printer, "admin") |
---|
[708] | 187 | |
---|
| 188 | def getGraceDelay(self) : |
---|
| 189 | """Returns the grace delay in days.""" |
---|
[802] | 190 | gd = self.getPrinterOption(printer, "gracedelay") |
---|
[731] | 191 | try : |
---|
| 192 | return int(gd) |
---|
| 193 | except ValueError : |
---|
[773] | 194 | raise PyKotaConfigError, _("Invalid grace delay %s") % gd |
---|