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

Revision 786, 7.3 kB (checked in by jalet, 21 years ago)

Small problem wrt external requester

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