X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;ds=sidebyside;f=plc_config.py;h=45805b969d4d3a8b65965d2f1d968c7ad1aaffbb;hb=b4f6124e813805155c9f5d83eee6d20f284931bb;hp=913ccfdc80f3a2849f7b45f6de1ac68c0ed5abad;hpb=26c36a2f8ba152ce101a24c95449bc2afb932b6c;p=myplc.git diff --git a/plc_config.py b/plc_config.py index 913ccfd..45805b9 100644 --- a/plc_config.py +++ b/plc_config.py @@ -7,10 +7,11 @@ # Mark Huang # Copyright (C) 2006 The Trustees of Princeton University # -# $Id: plc_config.py,v 1.3 2006/04/18 15:32:48 thierry Exp $ +# $Id$ # import xml.dom.minidom +from xml.parsers.expat import ExpatError from StringIO import StringIO import time import re @@ -20,6 +21,8 @@ import os import types +class ConfigurationException(Exception): pass + class PLCConfiguration: """ Configuration file store. Optionally instantiate with a file path @@ -187,7 +190,11 @@ class PLCConfiguration: Merge file into configuration store. """ - dom = xml.dom.minidom.parse(file) + try: + dom = xml.dom.minidom.parse(file) + except ExpatError, e: + raise ConfigurationException, e + if type(file) in types.StringTypes: self._files.append(os.path.abspath(file)) @@ -235,6 +242,44 @@ class PLCConfiguration: fileobj.close() + def verify(self, default, read, verify_variables={}): + """ 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: + + default configuration + site configuration + list of category/variable tuples to validate in these configurations + + Returns: + + dict of values for the category/variables passed in + If an exception is found, ConfigurationException is raised. + + """ + + validated_variables = {} + for category_id, variable_id in verify_variables.iteritems(): + category_id = category_id.lower() + variable_id = variable_id.lower() + variable_value = None + sources = (self, read, default) + for source in sources: + (category_value, variable_value) = source.get(category_id,variable_id) + if variable_value <> None: + entry = validated_variables.get(category_id,[]) + entry.append(variable_value['value']) + validated_variables["%s_%s"%(category_id.upper(),variable_id.upper())]=entry + break + if variable_value == None: + raise ConfigurationException("Cannot find %s_%s)" % \ + (category_id.upper(), + variable_id.upper())) + return validated_variables def get(self, category_id, variable_id): """ @@ -798,6 +843,6 @@ class TrimTextElement(xml.dom.minidom.Element): if __name__ == '__main__': import sys - if len(sys.argv) > 1 and sys.argv[1] in ['build', 'install']: + if len(sys.argv) > 1 and sys.argv[1] in ['build', 'install', 'uninstall']: from distutils.core import setup setup(py_modules=["plc_config"])