Added a verify() routine to the PLCConfiguration object in order to enforce
authorStephen Soltesz <soltesz@cs.princeton.edu>
Wed, 23 Jul 2008 16:05:32 +0000 (16:05 +0000)
committerStephen Soltesz <soltesz@cs.princeton.edu>
Wed, 23 Jul 2008 16:05:32 +0000 (16:05 +0000)
that PLC_ROOT_USER and PLC_API_MAINTENANCE_USER are different.

By adding the verify() function, I think other checks can be added easily.  As
well, the try/except around saving the configuration can catch the different
cases.

plc-config-tty
plc_config.py

index 67ebe61..1e45cdc 100755 (executable)
@@ -18,6 +18,7 @@ import readline
 import getopt
 
 from plc_config import PLCConfiguration
+from plc_config import ConfigurationException
 
 ####################
 release_id = "$Id$"
@@ -360,8 +361,14 @@ def mainloop (cdef, cread, cwrite, default_config, site_config, consolidated_con
             return
         elif (command in "wW"):
             try:
+                # Confirm that various constraints are met before saving file.
+                cwrite.verify(cdef, cread)
                 cwrite.save(site_config)
+            except ConfigurationException, e:
+                print "Save failed due to a configuration exception: %s" % e
+                break;
             except:
+                import traceback; print traceback.print_exc()
                 print ("Could not save -- fix write access on %s" % site_config)
                 break
             print ("Wrote %s" % site_config)
index c9382d2..bbc2f51 100644 (file)
@@ -20,6 +20,8 @@ import os
 import types
 
 
+class ConfigurationException(Exception): pass
+
 class PLCConfiguration:
     """
     Configuration file store. Optionally instantiate with a file path
@@ -235,6 +237,42 @@ class PLCConfiguration:
 
         fileobj.close()
 
+    def verify(self, default, read):
+        """ Confirm that the existing configuration is consistent according to
+        the checks below.
+
+            It looks for filled-in values in the order of, local object (self),
+            followed by cread (read values), and finally default values.
+
+        Arguments: 
+
+            None
+
+        Returns:
+
+            None.  If an exception is found, ConfigurationException is raised.
+
+        """
+
+        maint_user = self.get('plc_api', 'maintenance_user')
+        if maint_user == (None, None):
+            maint_user = read.get('plc_api', 'maintenance_user')
+            if maint_user == (None, None):
+                maint_user = default.get('plc_api', 'maintenance_user')
+
+        root_user = self.get('plc', 'root_user')
+        if root_user == (None, None):
+            root_user = read.get('plc', 'root_user')
+            if root_user == (None, None):
+                root_user = default.get('plc', 'root_user')
+
+        muser= maint_user[1]['value']
+        ruser= root_user[1]['value']
+
+        if muser == ruser:
+            raise ConfigurationException("The Maintenance Account email address cannot be the same as the Root User email address")
+        return
+
 
     def get(self, category_id, variable_id):
         """