1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2011-2013 EDF R&D
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License.
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # Lesser General Public License for more details.
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 # Author(s): Guillaume Boulant (23/03/2011)
26 from MESHJOB import ConfigParameter
27 from salome.kernel.uiexception import AdminException, UiException
29 from salome_pluginsmanager import PLUGIN_PATH_PATTERN
30 CONFIG_RELPATH = os.path.join(PLUGIN_PATH_PATTERN,'smesh')
31 CONFIG_FILENAME = "padder.cfg"
33 TYPE_REMOTE = 'remote'
34 TYPES=[TYPE_LOCAL, TYPE_REMOTE]
38 # The first step is to look for the config file. This file
39 # is supposed to be located in the same directory than the
40 # padder plugin. Then, we have to scan the directories
41 # specified in the SALOME plugins path.
42 self.__configFilename = None
44 smeshpath=os.environ["SMESH_ROOT_DIR"]
46 raise AdminException("You should define the variable SMESH_ROOT_DIR")
48 pluginspath = os.path.join(smeshpath,CONFIG_RELPATH)
49 filename = os.path.join(pluginspath,CONFIG_FILENAME)
50 if os.path.exists(filename):
51 self.__configFilename = filename
53 msg = "The configuration file %s can't be found in the SMESH plugins path %s"
54 raise AdminException(msg%(CONFIG_FILENAME,pluginspath))
56 print "The configuration file is : %s"%self.__configFilename
57 self.__configparser = ConfigParser.RawConfigParser()
59 self.__configparser.read(self.__configFilename)
60 except ConfigParser.ParsingError, ex:
61 raise AdminException(ex.message)
63 def getLocalConfig(self):
64 return self.__getConfig(TYPE_LOCAL)
66 def getRemoteConfig(self):
67 return self.__getConfig(TYPE_REMOTE)
69 def getDefaultConfig(self):
70 defaultType = self.__getDefaultType()
71 return self.__getConfig(defaultType)
73 def __getConfig(self, type=TYPE_LOCAL):
74 configName = self.__configparser.get('resources', type)
75 resname = self.__configparser.get(configName, 'resname')
76 binpath = self.__configparser.get(configName, 'binpath')
77 envpath = self.__configparser.get(configName, 'envpath')
78 config = ConfigParameter(resname, binpath, envpath)
79 config.resname = resname
82 def __getDefaultType(self):
83 '''This returns the default type read in the config file ([resources], default)'''
84 defaultType = self.__configparser.get('preferences', 'defaultres')
85 if defaultType not in TYPES:
89 def printConfig(config):
90 print "PADDER CONFIGURATION:"
91 print "\tconfig.resname = %s"%config.resname
92 print "\tconfig.binpath = %s"%config.binpath
93 print "\tconfig.envpath = %s"%config.envpath
95 def getPadderTestDir(config):
97 This function returns the directory of the SpherePadder
98 installation, where the tests cases are located. This should be
99 used for test only. It makes the hypothesis that the binpath to
100 the executable program is a path of the executable program of a
101 complete installation of SpherePadder.
103 testdir=os.path.join(os.path.abspath(os.path.dirname(config.binpath)),"tests")
107 # =========================================================================
109 # =========================================================================
111 def TEST_getDefaultConfig():
113 configReader = ConfigReader()
114 defaultConfig = configReader.getDefaultConfig()
115 print defaultConfig.resname
116 print defaultConfig.binpath
117 print defaultConfig.envpath
118 except Exception, ex:
119 sys.stderr.write('ERROR: %s\n' % str(ex))
124 def TEST_getDefaultConfig_withError():
125 global CONFIG_FILENAME
126 CONFIG_FILENAME = "toto.cfg"
128 configReader = ConfigReader()
129 defaultConfig = configReader.getDefaultConfig()
130 except UiException, err:
131 print 'ERROR: %s' % str(err)
137 from salome.kernel import unittester
138 moduleName = "configreader"
141 unittester.run(moduleName, "TEST_getDefaultConfig")
142 unittester.run(moduleName, "TEST_getDefaultConfig_withError")
144 if __name__ == "__main__":
146 #pluginspath=os.environ["SALOME_PLUGINS_PATH"]
147 #for path in pluginspath.split(":"):
148 # sys.path.insert(0,path)