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

Revision 708, 5.7 kB (checked in by jalet, 21 years ago)

Incorrect handling of grace delay

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