[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$ |
---|
| 17 | # Revision 1.1 2003/02/05 21:28:17 jalet |
---|
| 18 | # Initial import into CVS |
---|
| 19 | # |
---|
| 20 | # |
---|
| 21 | # |
---|
| 22 | |
---|
| 23 | import sys |
---|
| 24 | import os |
---|
| 25 | import ConfigParser |
---|
| 26 | |
---|
| 27 | class PyKotaConfigError(Exception): |
---|
| 28 | """An exception for PyKota config related stuff.""" |
---|
| 29 | def __init__(self, message = ""): |
---|
| 30 | self.message = message |
---|
| 31 | Exception.__init__(self, message) |
---|
| 32 | def __repr__(self): |
---|
| 33 | return self.message |
---|
| 34 | __str__ = __repr__ |
---|
| 35 | |
---|
| 36 | class PyKotaConfig : |
---|
| 37 | """A class to deal with PyKota's configuration.""" |
---|
| 38 | def __init__(self, directory) : |
---|
| 39 | """Reads and checks the configuration file.""" |
---|
| 40 | self.filename = os.path.join(directory, "pykota.conf") |
---|
| 41 | self.config = ConfigParser.ConfigParser() |
---|
| 42 | self.config.read([self.filename]) |
---|
| 43 | self.checkConfiguration() |
---|
| 44 | |
---|
| 45 | def checkConfiguration(self) : |
---|
| 46 | """Checks if configuration is correct. |
---|
| 47 | |
---|
| 48 | raises PyKotaConfigError in case a problem is detected |
---|
| 49 | """ |
---|
| 50 | for option in [ "storagebackend", "storageserver", \ |
---|
| 51 | "storagename", "storageadmin", \ |
---|
| 52 | "storageuser", # TODO : "storageadminpw", "storageusepw", \ |
---|
| 53 | "logger", "admin", "adminmail", |
---|
| 54 | "smtpserver", "method", "gracedelay" ] : |
---|
| 55 | if not self.config.has_option("global", option) : |
---|
| 56 | raise PyKotaConfigError, "Option %s not found in section global of %s" % (option, self.filename) |
---|
| 57 | |
---|
| 58 | # more precise checks |
---|
| 59 | validloggers = [ "stderr", "syslog" ] |
---|
| 60 | if self.config.get("global", "logger").lower() not in validloggers : |
---|
| 61 | raise PyKotaConfigError, "Option logger only supports values in %s" % str(validloggers) |
---|
| 62 | |
---|
| 63 | validmethods = [ "lazy" ] # TODO add more methods |
---|
| 64 | if self.config.get("global", "method").lower() not in validmethods : |
---|
| 65 | raise PyKotaConfigError, "Option method only supports values in %s" % str(validmethods) |
---|
| 66 | |
---|
| 67 | # check all printers now |
---|
| 68 | for printer in self.getPrinterNames() : |
---|
| 69 | for poption in [ "requester", "policy" ] : |
---|
| 70 | if not self.config.has_option(printer, poption) : |
---|
| 71 | raise PyKotaConfigError, "Option %s not found in section %s of %s" % (option, printer, self.filename) |
---|
| 72 | |
---|
| 73 | validpolicies = [ "accept", "deny" ] |
---|
| 74 | if self.config.get(printer, "policy").lower() not in validpolicies : |
---|
| 75 | raise PyKotaConfigError, "Option policy in section %s only supports values in %s" % (printer, str(validrequesters)) |
---|
| 76 | |
---|
| 77 | validrequesters = [ "snmp" ] # TODO : add more requesters |
---|
| 78 | requester = self.config.get(printer, "requester").lower() |
---|
| 79 | if requester not in validrequesters : |
---|
| 80 | raise PyKotaConfigError, "Option requester in section %s only supports values in %s" % (printer, str(validrequesters)) |
---|
| 81 | if requester == "snmp" : |
---|
| 82 | for poption in [ "snmpcmnty", "snmpoid" ] : |
---|
| 83 | if not self.config.has_option(printer, poption) : |
---|
| 84 | raise PyKotaConfigError, "Option %s not found in section %s of %s" % (option, printer, self.filename) |
---|
| 85 | |
---|
| 86 | def getPrinterNames(self) : |
---|
| 87 | """Returns the list of configured printers, i.e. all sections names minus 'global'.""" |
---|
| 88 | return [pname for pname in self.config.sections() if pname != "global"] |
---|
| 89 | |
---|
| 90 | def getStorageBackend(self) : |
---|
| 91 | """Returns the storage backend information as a tuple. |
---|
| 92 | |
---|
| 93 | The tuple has the form : |
---|
| 94 | |
---|
| 95 | (backend, host, database, admin, user) |
---|
| 96 | """ |
---|
| 97 | backendinfo = [] |
---|
| 98 | for option in [ "storagebackend", "storageserver", \ |
---|
| 99 | "storagename", "storageadmin", \ |
---|
| 100 | "storageuser", # TODO : "storageadminpw", "storageusepw", \ |
---|
| 101 | ] : |
---|
| 102 | backendinfo.append(self.config.get("global", option)) |
---|
| 103 | return tuple(backendinfo) |
---|
| 104 | |
---|
| 105 | def getLoggingBackend(self) : |
---|
| 106 | """Returns the logging backend information.""" |
---|
| 107 | return self.config.get("global", "logger").lower() |
---|
| 108 | |
---|
| 109 | def getRequesterBackend(self, printer) : |
---|
| 110 | """Returns the requester backend to use for a given printer.""" |
---|
| 111 | return self.config.get(printer, "requester").lower() |
---|
| 112 | |
---|
| 113 | def getPrinterPolicy(self, printer) : |
---|
| 114 | """Returns the default policy for the current printer.""" |
---|
| 115 | return self.config.get(printer, "policy").lower() |
---|
| 116 | |
---|
| 117 | def getSMTPServer(self) : |
---|
| 118 | """Returns the SMTP server to use to send messages to users.""" |
---|
| 119 | return self.config.get("global", "smtpserver").lower() |
---|
| 120 | |
---|
| 121 | def getAdminMail(self) : |
---|
| 122 | """Returns the Email address of the Print Quota Administrator.""" |
---|
| 123 | return self.config.get("global", "adminmail") |
---|
| 124 | |
---|
| 125 | def getAdmin(self) : |
---|
| 126 | """Returns the full name of the Print Quota Administrator.""" |
---|
| 127 | return self.config.get("global", "admin") |
---|