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

Revision 731, 6.2 kB (checked in by jalet, 21 years ago)

repykota should be ok

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