root / pykota / trunk / pykota / config.py @ 773

Revision 773, 6.3 kB (checked in by jalet, 21 years ago)

Internationalization continues...

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