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

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

Bad cut&paste

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