Index: /pykota/trunk/pykota/tool.py
===================================================================
--- /pykota/trunk/pykota/tool.py (revision 788)
+++ /pykota/trunk/pykota/tool.py (revision 802)
@@ -15,4 +15,14 @@
 #
 # $Log$
+# Revision 1.20  2003/02/17 22:55:01  jalet
+# More options can now be set per printer or globally :
+#
+# 	admin
+# 	adminmail
+# 	gracedelay
+# 	requester
+#
+# the printer option has priority when both are defined.
+#
 # Revision 1.19  2003/02/10 11:28:45  jalet
 # Localization
@@ -116,6 +126,4 @@
         self.printername = os.environ.get("PRINTER", None)
         self.smtpserver = self.config.getSMTPServer()
-        self.admin = self.config.getAdmin()
-        self.adminmail = self.config.getAdminMail()
         
     def display_version_and_quit(self) :
@@ -202,20 +210,20 @@
         return 0
         
-    def sendMessage(self, touser, fullmessage) :
+    def sendMessage(self, adminmail, touser, fullmessage) :
         """Sends an email message containing headers to some user."""
         if "@" not in touser :
             touser = "%s@%s" % (touser, self.smtpserver)
         server = smtplib.SMTP(self.smtpserver)
-        server.sendmail(self.adminmail, [touser], fullmessage)
+        server.sendmail(adminmail, [touser], fullmessage)
         server.quit()
         
-    def sendMessageToUser(self, username, subject, message) :
+    def sendMessageToUser(self, admin, adminmail, username, subject, message) :
         """Sends an email message to a user."""
-        message += _("\n\nPlease contact your system administrator :\n\n\t%s - <%s>\n") % (self.admin, self.adminmail)
-        self.sendMessage(username, "Subject: %s\n\n%s" % (subject, message))
-        
-    def sendMessageToAdmin(self, subject, message) :
+        message += _("\n\nPlease contact your system administrator :\n\n\t%s - <%s>\n") % (admin, adminmail)
+        self.sendMessage(adminmail, username, "Subject: %s\n\n%s" % (subject, message))
+        
+    def sendMessageToAdmin(self, adminmail, subject, message) :
         """Sends an email message to the Print Quota administrator."""
-        self.sendMessage(self.adminmail, "Subject: %s\n\n%s" % (subject, message))
+        self.sendMessage(adminmail, adminmail, "Subject: %s\n\n%s" % (subject, message))
         
     def checkUserPQuota(self, username, printername) :
@@ -267,4 +275,6 @@
         """Checks a user quota and send him a message if quota is exceeded on current printer."""
         pname = printername or self.printername
+        admin = self.config.getAdmin(pname)
+        adminmail = self.config.getAdminMail(pname)
         (action, grace, gracedate) = self.checkUserPQuota(username, pname)
         if action == "DENY" :
@@ -273,11 +283,11 @@
                 adminmessage = _("Print Quota exceeded for user %s on printer %s") % (username, pname)
                 self.logger.log_message(adminmessage)
-                self.sendMessageToUser(username, _("Print Quota Exceeded"), _("You are not allowed to print anymore because\nyour Print Quota is exceeded on printer %s.") % pname)
-                self.sendMessageToAdmin("Print Quota", adminmessage)
+                self.sendMessageToUser(admin, adminmail, username, _("Print Quota Exceeded"), _("You are not allowed to print anymore because\nyour Print Quota is exceeded on printer %s.") % pname)
+                self.sendMessageToAdmin(adminmail, _("Print Quota"), adminmessage)
         elif action == "WARN" :    
             adminmessage = _("Print Quota soft limit exceeded for user %s on printer %s") % (username, pname)
             self.logger.log_message(adminmessage)
-            self.sendMessageToUser(username, _("Print Quota Exceeded"), _("You will soon be forbidden to print anymore because\nyour Print Quota is almost reached on printer %s.") % pname)
-            self.sendMessageToAdmin(_("Print Quota"), adminmessage)
+            self.sendMessageToUser(admin, adminmail, username, _("Print Quota Exceeded"), _("You will soon be forbidden to print anymore because\nyour Print Quota is almost reached on printer %s.") % pname)
+            self.sendMessageToAdmin(adminmail, _("Print Quota"), adminmessage)
         return action        
     
Index: /pykota/trunk/pykota/storage.py
===================================================================
--- /pykota/trunk/pykota/storage.py (revision 800)
+++ /pykota/trunk/pykota/storage.py (revision 802)
@@ -15,4 +15,14 @@
 #
 # $Log$
+# Revision 1.9  2003/02/17 22:55:01  jalet
+# More options can now be set per printer or globally :
+#
+# 	admin
+# 	adminmail
+# 	gracedelay
+# 	requester
+#
+# the printer option has priority when both are defined.
+#
 # Revision 1.8  2003/02/17 22:05:50  jalet
 # Storage backend now supports admin and user passwords (untested)
@@ -111,5 +121,4 @@
 def openConnection(config, asadmin=0) :
     """Returns a connection handle to the appropriate Quota Storage Database."""
-    (backend, host, database, admin, user) = config.getStorageBackend()
     backendinfo = config.getStorageBackend()
     backend = backendinfo["storagebackend"]
Index: /pykota/trunk/pykota/config.py
===================================================================
--- /pykota/trunk/pykota/config.py (revision 800)
+++ /pykota/trunk/pykota/config.py (revision 802)
@@ -15,4 +15,14 @@
 #
 # $Log$
+# Revision 1.15  2003/02/17 22:55:01  jalet
+# More options can now be set per printer or globally :
+#
+# 	admin
+# 	adminmail
+# 	gracedelay
+# 	requester
+#
+# the printer option has priority when both are defined.
+#
 # Revision 1.14  2003/02/17 22:05:50  jalet
 # Storage backend now supports admin and user passwords (untested)
@@ -91,44 +101,32 @@
            raises PyKotaConfigError in case a problem is detected
         """
-        for option in [ "storagebackend", "storageserver", \
-                        "storagename", "storageadmin", \
-                        "storageuser", 
-                        "logger", "admin", "adminmail",
-                        "smtpserver", "method", "gracedelay" ] :
-            if not self.config.has_option("global", option) :            
-                raise PyKotaConfigError, _("Option %s not found in section global of %s") % (option, self.filename)
-                
-        # more precise checks        
-        validloggers = [ "stderr", "system" ] 
-        if self.config.get("global", "logger", raw=1).lower() not in validloggers :             
-            raise PyKotaConfigError, _("Option logger only supports values in %s") % str(validloggers)
-            
         validmethods = [ "lazy" ] # TODO add more methods            
         if self.config.get("global", "method", raw=1).lower() not in validmethods :             
             raise PyKotaConfigError, _("Option method only supports values in %s") % str(validmethods)
-            
-        # check all printers now 
-        for printer in self.getPrinterNames() :
-            for poption in [ "requester", "policy" ] : 
-                if not self.config.has_option(printer, poption) :
-                    raise PyKotaConfigError, _("Option %s not found in section %s of %s") % (option, printer, self.filename)
-                    
-            validpolicies = [ "ALLOW", "DENY" ]     
-            if self.config.get(printer, "policy", raw=1).upper() not in validpolicies :
-                raise PyKotaConfigError, _("Option policy in section %s only supports values in %s") % (printer, str(validpolicies))
-            
-            validrequesters = [ "snmp", "external" ] # TODO : add more requesters
-            fullrequester = self.config.get(printer, "requester", raw=1)
-            try :
-                (requester, args) = [x.strip() for x in fullrequester.split('(', 1)]
-            except ValueError :    
-                raise PyKotaConfigError, _("Invalid requester %s for printer %s") % (fullrequester, printer)
-            else :
-                if requester not in validrequesters :
-                    raise PyKotaConfigError, _("Option requester for printer %s only supports values in %s") % (printer, str(validrequesters))
                         
     def getPrinterNames(self) :    
         """Returns the list of configured printers, i.e. all sections names minus 'global'."""
         return [pname for pname in self.config.sections() if pname != "global"]
+        
+    def getGlobalOption(self, option, ignore=0) :    
+        """Returns an option from the global section, or raises a PyKotaConfigError if ignore is not set, else returns None."""
+        try :
+            return self.config.get("global", option, raw=1)
+        except (ConfigParser.NoSectionError, ConfigParser.NoOptionError) :    
+            if ignore :
+                return
+            else :
+                raise PyKotaConfigError, _("Option %s not found in section global of %s") % (option, self.filename)
+                
+    def getPrinterOption(self, printer, option) :    
+        """Returns an option from the printer section, or the global section, or raises a PyKotaConfigError."""
+        globaloption = self.getGlobalOption(option, ignore=1)
+        try :
+            return self.config.get(printer, option, raw=1)
+        except (ConfigParser.NoSectionError, ConfigParser.NoOptionError) :    
+            if globaloption is not None :
+                return globaloption
+            else :
+                raise PyKotaConfigError, _("Option %s not found in section %s of %s") % (option, printer, self.filename)
         
     def getStorageBackend(self) :    
@@ -139,45 +137,56 @@
                         "storageuser", \
                       ] :
-            backendinfo[option] = self.config.get("global", option, raw=1)
+            backendinfo[option] = self.getGlobalOption((option)
         for option in [ "storageadminpw", "storageuserpw" ] :    
-            if self.config.has_option("global", option) :
-                backendinfo[option] = self.config.get("global", option, raw=1)
-            else :    
-                backendinfo[option] = None
+            backendinfo[option] = self.getGlobalOption(option, ignore=1)
         return backendinfo
         
     def getLoggingBackend(self) :    
         """Returns the logging backend information."""
-        return self.config.get("global", "logger", raw=1).lower()
+        validloggers = [ "stderr", "system" ] 
+        logger = self.getGlobalOption("logger").lower()
+        if logger not in validloggers :             
+            raise PyKotaConfigError, _("Option logger only supports values in %s") % str(validloggers)
+        return logger    
         
     def getRequesterBackend(self, printer) :    
         """Returns the requester backend to use for a given printer, with its arguments."""
-        fullrequester = self.config.get(printer, "requester", raw=1)
-        (requester, args) = [x.strip() for x in fullrequester.split('(', 1)]
+        fullrequester = self.getPrinterOption(printer, "requester")
+        try :
+            (requester, args) = [x.strip() for x in fullrequester.split('(', 1)]
+        except ValueError :    
+            raise PyKotaConfigError, _("Invalid requester %s for printer %s") % (fullrequester, printer)
         if args.endswith(')') :
             args = args[:-1]
         if not args :
             raise PyKotaConfigError, _("Invalid requester %s for printer %s") % (fullrequester, printer)
+        validrequesters = [ "snmp", "external" ] # TODO : add more requesters
+        if requester not in validrequesters :
+            raise PyKotaConfigError, _("Option requester for printer %s only supports values in %s") % (printer, str(validrequesters))
         return (requester, args)
         
     def getPrinterPolicy(self, printer) :    
         """Returns the default policy for the current printer."""
-        return self.config.get(printer, "policy", raw=1).upper()
+        validpolicies = [ "ALLOW", "DENY" ]     
+        policy = self.getPrinterOption(printer, "policy").upper()
+        if policy not in validpolicies :
+            raise PyKotaConfigError, _("Option policy in section %s only supports values in %s") % (printer, str(validpolicies))
+        return policy
         
     def getSMTPServer(self) :    
         """Returns the SMTP server to use to send messages to users."""
-        return self.config.get("global", "smtpserver", raw=1)
+        return self.getGlobalOption("smtpserver")
         
-    def getAdminMail(self) :    
+    def getAdminMail(self, printer) :    
         """Returns the Email address of the Print Quota Administrator."""
-        return self.config.get("global", "adminmail", raw=1)
+        return self.getPrinterOption(printer, "adminmail")
         
-    def getAdmin(self) :    
+    def getAdmin(self, printer) :    
         """Returns the full name of the Print Quota Administrator."""
-        return self.config.get("global", "admin", raw=1)
+        return self.getPrinterOption(printer, "admin")
         
     def getGraceDelay(self) :    
         """Returns the grace delay in days."""
-        gd = self.config.get("global", "gracedelay", raw=1)
+        gd = self.getPrinterOption(printer, "gracedelay")
         try :
             return int(gd)
