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