Back to index

salome-smesh  6.5.0
configreader.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 # Copyright (C) 2011-2012  EDF R&D
00003 #
00004 # This library is free software; you can redistribute it and/or
00005 # modify it under the terms of the GNU Lesser General Public
00006 # License as published by the Free Software Foundation; either
00007 # version 2.1 of the License.
00008 #
00009 # This library is distributed in the hope that it will be useful,
00010 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 # Lesser General Public License for more details.
00013 #
00014 # You should have received a copy of the GNU Lesser General Public
00015 # License along with this library; if not, write to the Free Software
00016 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00017 #
00018 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00019 #
00020 # Author(s): Guillaume Boulant (23/03/2011)
00021 #
00022 
00023 import sys, os
00024 import ConfigParser
00025 from MESHJOB import ConfigParameter
00026 from salome.kernel.uiexception import AdminException, UiException
00027 
00028 from salome_pluginsmanager import PLUGIN_PATH_PATTERN
00029 CONFIG_RELPATH  = os.path.join(PLUGIN_PATH_PATTERN,'smesh')
00030 CONFIG_FILENAME = "padder.cfg"
00031 TYPE_LOCAL   = 'local'
00032 TYPE_REMOTE  = 'remote'
00033 TYPES=[TYPE_LOCAL, TYPE_REMOTE]
00034 
00035 class ConfigReader:
00036     def __init__(self):
00037         # The first step is to look for the config file. This file
00038         # is supposed to be located in the same directory than the
00039         # padder plugin. Then, we have to scan the directories
00040         # specified in the SALOME plugins path.
00041         self.__configFilename = None
00042         try:
00043             smeshpath=os.environ["SMESH_ROOT_DIR"]
00044         except KeyError, ex:
00045             raise AdminException("You should define the variable SMESH_ROOT_DIR")
00046 
00047         pluginspath = os.path.join(smeshpath,CONFIG_RELPATH)
00048         filename    = os.path.join(pluginspath,CONFIG_FILENAME)
00049         if os.path.exists(filename):
00050             self.__configFilename = filename
00051         else:
00052             msg = "The configuration file %s can't be found in the SMESH plugins path %s"
00053             raise AdminException(msg%(CONFIG_FILENAME,pluginspath))
00054 
00055         print "The configuration file is : %s"%self.__configFilename
00056         self.__configparser = ConfigParser.RawConfigParser()
00057         try:
00058             self.__configparser.read(self.__configFilename)
00059         except ConfigParser.ParsingError, ex:
00060             raise AdminException(ex.message)
00061 
00062     def getLocalConfig(self):
00063         return self.__getConfig(TYPE_LOCAL)
00064     
00065     def getRemoteConfig(self):
00066         return self.__getConfig(TYPE_REMOTE)
00067 
00068     def getDefaultConfig(self):
00069         defaultType = self.__getDefaultType()
00070         return self.__getConfig(defaultType)
00071         
00072     def __getConfig(self, type=TYPE_LOCAL):
00073         configName = self.__configparser.get('resources', type)
00074         resname = self.__configparser.get(configName, 'resname')
00075         binpath = self.__configparser.get(configName, 'binpath')
00076         envpath = self.__configparser.get(configName, 'envpath')
00077         config = ConfigParameter(resname, binpath, envpath)
00078         config.resname = resname
00079         return config
00080 
00081     def __getDefaultType(self):
00082         '''This returns the default type read in the config file ([resources], default)'''
00083         defaultType = self.__configparser.get('preferences', 'defaultres')
00084         if defaultType not in TYPES:
00085             return TYPE_LOCAL
00086         return defaultType
00087 
00088 
00089 def printConfig(config):
00090     print "PADDER CONFIGURATION:"
00091     print "\tconfig.resname = %s"%config.resname
00092     print "\tconfig.binpath = %s"%config.binpath
00093     print "\tconfig.envpath = %s"%config.envpath
00094     
00095 
00096 #
00097 # =========================================================================
00098 # Test runner
00099 # =========================================================================
00100 #
00101 def TEST_getDefaultConfig():
00102     try:
00103         configReader = ConfigReader()
00104         defaultConfig = configReader.getDefaultConfig()
00105         print defaultConfig.resname
00106         print defaultConfig.binpath
00107         print defaultConfig.envpath
00108     except Exception, ex:
00109         sys.stderr.write('ERROR: %s\n' % str(ex))
00110         return False
00111     
00112     return True
00113 
00114 def TEST_getDefaultConfig_withError():
00115     global CONFIG_FILENAME
00116     CONFIG_FILENAME = "toto.cfg"
00117     try:
00118         configReader = ConfigReader()
00119         defaultConfig = configReader.getDefaultConfig()
00120     except UiException, err:
00121         print 'ERROR: %s' % str(err)
00122         return True
00123     
00124     return False
00125 
00126 
00127 from salome.kernel import unittester
00128 moduleName = "configreader"
00129 
00130 def testsuite():
00131     unittester.run(moduleName, "TEST_getDefaultConfig")
00132     unittester.run(moduleName, "TEST_getDefaultConfig_withError")
00133     
00134 if __name__ == "__main__":
00135     #import os, sys
00136     #pluginspath=os.environ["SALOME_PLUGINS_PATH"]
00137     #for path in pluginspath.split(":"):
00138     #    sys.path.insert(0,path)
00139     
00140     testsuite()