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