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

Revision 713, 6.0 kB (checked in by jalet, 21 years ago)

More robust behavior (hopefully) when the user or printer is not managed
correctly by the Quota System : e.g. cupsFilter added in ppd file, but
printer and/or user not 'yet?' in storage.

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