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