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