1 | # PyKota |
---|
2 | # |
---|
3 | # PyKota : Print Quotas for CUPS |
---|
4 | # |
---|
5 | # (c) 2003 Jerome Alet <alet@librelogiciel.com> |
---|
6 | # This program is free software; you can redistribute it and/or modify |
---|
7 | # it under the terms of the GNU General Public License as published by |
---|
8 | # the Free Software Foundation; either version 2 of the License, or |
---|
9 | # (at your option) any later version. |
---|
10 | # |
---|
11 | # This program is distributed in the hope that it will be useful, |
---|
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | # GNU General Public License for more details. |
---|
15 | # |
---|
16 | # You should have received a copy of the GNU General Public License |
---|
17 | # along with this program; if not, write to the Free Software |
---|
18 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
19 | # |
---|
20 | # $Id$ |
---|
21 | # |
---|
22 | # $Log$ |
---|
23 | # Revision 1.21 2003/03/29 13:45:27 jalet |
---|
24 | # GPL paragraphs were incorrectly (from memory) copied into the sources. |
---|
25 | # Two README files were added. |
---|
26 | # Upgrade script for PostgreSQL pre 1.01 schema was added. |
---|
27 | # |
---|
28 | # Revision 1.20 2003/03/29 13:08:28 jalet |
---|
29 | # Configuration is now expected to be found in /etc/pykota.conf instead of |
---|
30 | # in /etc/cups/pykota.conf |
---|
31 | # Installation script can move old config files to the new location if needed. |
---|
32 | # Better error handling if configuration file is absent. |
---|
33 | # |
---|
34 | # Revision 1.19 2003/03/16 09:56:52 jalet |
---|
35 | # Mailto option now accepts some additional values which all mean that |
---|
36 | # nobody will receive any email message. |
---|
37 | # Mailto option now works. Version 1.01 is now officially out. |
---|
38 | # |
---|
39 | # Revision 1.18 2003/03/16 08:00:50 jalet |
---|
40 | # Default hard coded options are now used if they are not set in the |
---|
41 | # configuration file. |
---|
42 | # |
---|
43 | # Revision 1.17 2003/03/15 23:01:28 jalet |
---|
44 | # New mailto option in configuration file added. |
---|
45 | # No time to test this tonight (although it should work). |
---|
46 | # |
---|
47 | # Revision 1.16 2003/02/17 23:01:56 jalet |
---|
48 | # Typos |
---|
49 | # |
---|
50 | # Revision 1.15 2003/02/17 22:55:01 jalet |
---|
51 | # More options can now be set per printer or globally : |
---|
52 | # |
---|
53 | # admin |
---|
54 | # adminmail |
---|
55 | # gracedelay |
---|
56 | # requester |
---|
57 | # |
---|
58 | # the printer option has priority when both are defined. |
---|
59 | # |
---|
60 | # Revision 1.14 2003/02/17 22:05:50 jalet |
---|
61 | # Storage backend now supports admin and user passwords (untested) |
---|
62 | # |
---|
63 | # Revision 1.13 2003/02/10 11:47:39 jalet |
---|
64 | # Moved some code down into the requesters |
---|
65 | # |
---|
66 | # Revision 1.12 2003/02/10 10:36:33 jalet |
---|
67 | # Small problem wrt external requester |
---|
68 | # |
---|
69 | # Revision 1.11 2003/02/10 08:50:45 jalet |
---|
70 | # External requester seems to be finally ok now |
---|
71 | # |
---|
72 | # Revision 1.10 2003/02/10 08:19:57 jalet |
---|
73 | # tell ConfigParser to return raw data, this allows our own strings |
---|
74 | # interpolations in the requester |
---|
75 | # |
---|
76 | # Revision 1.9 2003/02/10 00:44:38 jalet |
---|
77 | # Typos |
---|
78 | # |
---|
79 | # Revision 1.8 2003/02/10 00:42:17 jalet |
---|
80 | # External requester should be ok (untested) |
---|
81 | # New syntax for configuration file wrt requesters |
---|
82 | # |
---|
83 | # Revision 1.7 2003/02/09 13:05:43 jalet |
---|
84 | # Internationalization continues... |
---|
85 | # |
---|
86 | # Revision 1.6 2003/02/07 22:00:09 jalet |
---|
87 | # Bad cut&paste |
---|
88 | # |
---|
89 | # Revision 1.5 2003/02/06 23:58:05 jalet |
---|
90 | # repykota should be ok |
---|
91 | # |
---|
92 | # Revision 1.4 2003/02/06 09:19:02 jalet |
---|
93 | # More robust behavior (hopefully) when the user or printer is not managed |
---|
94 | # correctly by the Quota System : e.g. cupsFilter added in ppd file, but |
---|
95 | # printer and/or user not 'yet?' in storage. |
---|
96 | # |
---|
97 | # Revision 1.3 2003/02/05 23:26:22 jalet |
---|
98 | # Incorrect handling of grace delay |
---|
99 | # |
---|
100 | # Revision 1.2 2003/02/05 23:09:20 jalet |
---|
101 | # Name conflict |
---|
102 | # |
---|
103 | # Revision 1.1 2003/02/05 21:28:17 jalet |
---|
104 | # Initial import into CVS |
---|
105 | # |
---|
106 | # |
---|
107 | # |
---|
108 | |
---|
109 | import sys |
---|
110 | import os |
---|
111 | import ConfigParser |
---|
112 | |
---|
113 | class PyKotaConfigError(Exception): |
---|
114 | """An exception for PyKota config related stuff.""" |
---|
115 | def __init__(self, message = ""): |
---|
116 | self.message = message |
---|
117 | Exception.__init__(self, message) |
---|
118 | def __repr__(self): |
---|
119 | return self.message |
---|
120 | __str__ = __repr__ |
---|
121 | |
---|
122 | class PyKotaConfig : |
---|
123 | """A class to deal with PyKota's configuration.""" |
---|
124 | def __init__(self, directory) : |
---|
125 | """Reads and checks the configuration file.""" |
---|
126 | self.filename = os.path.join(directory, "pykota.conf") |
---|
127 | if not os.path.isfile(self.filename) : |
---|
128 | raise PyKotaConfigError, _("Configuration file %s not found.") % self.filename |
---|
129 | self.config = ConfigParser.ConfigParser() |
---|
130 | self.config.read([self.filename]) |
---|
131 | self.checkConfiguration() |
---|
132 | |
---|
133 | def checkConfiguration(self) : |
---|
134 | """Checks if configuration is correct. |
---|
135 | |
---|
136 | raises PyKotaConfigError in case a problem is detected |
---|
137 | """ |
---|
138 | validmethods = [ "lazy" ] # TODO add more methods |
---|
139 | if self.config.get("global", "method", raw=1).lower() not in validmethods : |
---|
140 | raise PyKotaConfigError, _("Option method only supports values in %s") % str(validmethods) |
---|
141 | |
---|
142 | def getPrinterNames(self) : |
---|
143 | """Returns the list of configured printers, i.e. all sections names minus 'global'.""" |
---|
144 | return [pname for pname in self.config.sections() if pname != "global"] |
---|
145 | |
---|
146 | def getGlobalOption(self, option, ignore=0) : |
---|
147 | """Returns an option from the global section, or raises a PyKotaConfigError if ignore is not set, else returns None.""" |
---|
148 | try : |
---|
149 | return self.config.get("global", option, raw=1) |
---|
150 | except (ConfigParser.NoSectionError, ConfigParser.NoOptionError) : |
---|
151 | if ignore : |
---|
152 | return |
---|
153 | else : |
---|
154 | raise PyKotaConfigError, _("Option %s not found in section global of %s") % (option, self.filename) |
---|
155 | |
---|
156 | def getPrinterOption(self, printer, option) : |
---|
157 | """Returns an option from the printer section, or the global section, or raises a PyKotaConfigError.""" |
---|
158 | globaloption = self.getGlobalOption(option, ignore=1) |
---|
159 | try : |
---|
160 | return self.config.get(printer, option, raw=1) |
---|
161 | except (ConfigParser.NoSectionError, ConfigParser.NoOptionError) : |
---|
162 | if globaloption is not None : |
---|
163 | return globaloption |
---|
164 | else : |
---|
165 | raise PyKotaConfigError, _("Option %s not found in section %s of %s") % (option, printer, self.filename) |
---|
166 | |
---|
167 | def getStorageBackend(self) : |
---|
168 | """Returns the storage backend information as a Python mapping.""" |
---|
169 | backendinfo = {} |
---|
170 | for option in [ "storagebackend", "storageserver", \ |
---|
171 | "storagename", "storageadmin", \ |
---|
172 | "storageuser", \ |
---|
173 | ] : |
---|
174 | backendinfo[option] = self.getGlobalOption(option) |
---|
175 | for option in [ "storageadminpw", "storageuserpw" ] : |
---|
176 | backendinfo[option] = self.getGlobalOption(option, ignore=1) |
---|
177 | return backendinfo |
---|
178 | |
---|
179 | def getLoggingBackend(self) : |
---|
180 | """Returns the logging backend information.""" |
---|
181 | validloggers = [ "stderr", "system" ] |
---|
182 | try : |
---|
183 | logger = self.getGlobalOption("logger").lower() |
---|
184 | except PyKotaConfigError : |
---|
185 | logger = "system" |
---|
186 | if logger not in validloggers : |
---|
187 | raise PyKotaConfigError, _("Option logger only supports values in %s") % str(validloggers) |
---|
188 | return logger |
---|
189 | |
---|
190 | def getRequesterBackend(self, printer) : |
---|
191 | """Returns the requester backend to use for a given printer, with its arguments.""" |
---|
192 | fullrequester = self.getPrinterOption(printer, "requester") |
---|
193 | try : |
---|
194 | (requester, args) = [x.strip() for x in fullrequester.split('(', 1)] |
---|
195 | except ValueError : |
---|
196 | raise PyKotaConfigError, _("Invalid requester %s for printer %s") % (fullrequester, printer) |
---|
197 | if args.endswith(')') : |
---|
198 | args = args[:-1] |
---|
199 | if not args : |
---|
200 | raise PyKotaConfigError, _("Invalid requester %s for printer %s") % (fullrequester, printer) |
---|
201 | validrequesters = [ "snmp", "external" ] # TODO : add more requesters |
---|
202 | if requester not in validrequesters : |
---|
203 | raise PyKotaConfigError, _("Option requester for printer %s only supports values in %s") % (printer, str(validrequesters)) |
---|
204 | return (requester, args) |
---|
205 | |
---|
206 | def getPrinterPolicy(self, printer) : |
---|
207 | """Returns the default policy for the current printer.""" |
---|
208 | validpolicies = [ "ALLOW", "DENY" ] |
---|
209 | try : |
---|
210 | policy = self.getPrinterOption(printer, "policy").upper() |
---|
211 | except PyKotaConfigError : |
---|
212 | policy = "ALLOW" |
---|
213 | if policy not in validpolicies : |
---|
214 | raise PyKotaConfigError, _("Option policy in section %s only supports values in %s") % (printer, str(validpolicies)) |
---|
215 | return policy |
---|
216 | |
---|
217 | def getSMTPServer(self) : |
---|
218 | """Returns the SMTP server to use to send messages to users.""" |
---|
219 | try : |
---|
220 | return self.getGlobalOption("smtpserver") |
---|
221 | except PyKotaConfigError : |
---|
222 | return "localhost" |
---|
223 | |
---|
224 | def getAdminMail(self, printer) : |
---|
225 | """Returns the Email address of the Print Quota Administrator.""" |
---|
226 | try : |
---|
227 | return self.getPrinterOption(printer, "adminmail") |
---|
228 | except PyKotaConfigError : |
---|
229 | return "root@localhost" |
---|
230 | |
---|
231 | def getAdmin(self, printer) : |
---|
232 | """Returns the full name of the Print Quota Administrator.""" |
---|
233 | try : |
---|
234 | return self.getPrinterOption(printer, "admin") |
---|
235 | except PyKotaConfigError : |
---|
236 | return "root" |
---|
237 | |
---|
238 | def getMailTo(self, printer) : |
---|
239 | """Returns the recipient of email messages.""" |
---|
240 | validmailtos = [ "NOBODY", "NONE", "NOONE", "BITBUCKET", "DEVNULL", "BOTH", "USER", "ADMIN" ] |
---|
241 | try : |
---|
242 | mailto = self.getPrinterOption(printer, "mailto").upper() |
---|
243 | except PyKotaConfigError : |
---|
244 | mailto = "BOTH" |
---|
245 | if mailto not in validmailtos : |
---|
246 | raise PyKotaConfigError, _("Option mailto in section %s only supports values in %s") % (printer, str(validmailtos)) |
---|
247 | return mailto |
---|
248 | |
---|
249 | def getGraceDelay(self, printer) : |
---|
250 | """Returns the grace delay in days.""" |
---|
251 | try : |
---|
252 | gd = self.getPrinterOption(printer, "gracedelay") |
---|
253 | except PyKotaConfigError : |
---|
254 | gd = 7 |
---|
255 | try : |
---|
256 | return int(gd) |
---|
257 | except ValueError : |
---|
258 | raise PyKotaConfigError, _("Invalid grace delay %s") % gd |
---|