1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2011-2012 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
20 # Author(s): Guillaume Boulant (23/03/2011)
25 from MESHJOB import ConfigParameter
26 from salome.kernel.uiexception import AdminException, UiException
28 from salome_pluginsmanager import PLUGIN_PATH_PATTERN
29 CONFIG_RELPATH = os.path.join(PLUGIN_PATH_PATTERN,'smesh')
30 CONFIG_FILENAME = "padder.cfg"
32 TYPE_REMOTE = 'remote'
33 TYPES=[TYPE_LOCAL, TYPE_REMOTE]
37 # The first step is to look for the config file. This file
38 # is supposed to be located in the same directory than the
39 # padder plugin. Then, we have to scan the directories
40 # specified in the SALOME plugins path.
41 self.__configFilename = None
43 smeshpath=os.environ["SMESH_ROOT_DIR"]
45 raise AdminException("You should define the variable SMESH_ROOT_DIR")
47 pluginspath = os.path.join(smeshpath,CONFIG_RELPATH)
48 filename = os.path.join(pluginspath,CONFIG_FILENAME)
49 if os.path.exists(filename):
50 self.__configFilename = filename
52 msg = "The configuration file %s can't be found in the SMESH plugins path %s"
53 raise AdminException(msg%(CONFIG_FILENAME,pluginspath))
55 print "The configuration file is : %s"%self.__configFilename
56 self.__configparser = ConfigParser.RawConfigParser()
58 self.__configparser.read(self.__configFilename)
59 except ConfigParser.ParsingError, ex:
60 raise AdminException(ex.message)
62 def getLocalConfig(self):
63 return self.__getConfig(TYPE_LOCAL)
65 def getRemoteConfig(self):
66 return self.__getConfig(TYPE_REMOTE)
68 def getDefaultConfig(self):
69 defaultType = self.__getDefaultType()
70 return self.__getConfig(defaultType)
72 def __getConfig(self, type=TYPE_LOCAL):
73 configName = self.__configparser.get('resources', type)
74 resname = self.__configparser.get(configName, 'resname')
75 binpath = self.__configparser.get(configName, 'binpath')
76 envpath = self.__configparser.get(configName, 'envpath')
77 config = ConfigParameter(resname, binpath, envpath)
78 config.resname = resname
81 def __getDefaultType(self):
82 '''This returns the default type read in the config file ([resources], default)'''
83 defaultType = self.__configparser.get('preferences', 'defaultres')
84 if defaultType not in TYPES:
88 def printConfig(config):
89 print "PADDER CONFIGURATION:"
90 print "\tconfig.resname = %s"%config.resname
91 print "\tconfig.binpath = %s"%config.binpath
92 print "\tconfig.envpath = %s"%config.envpath
94 def getPadderTestDir(config):
96 This function returns the directory of the SpherePadder
97 installation, where the tests cases are located. This should be
98 used for test only. It makes the hypothesis that the binpath to
99 the executable program is a path of the executable program of a
100 complete installation of SpherePadder.
102 testdir=os.path.join(os.path.abspath(os.path.dirname(config.binpath)),"tests")
106 # =========================================================================
108 # =========================================================================
110 def TEST_getDefaultConfig():
112 configReader = ConfigReader()
113 defaultConfig = configReader.getDefaultConfig()
114 print defaultConfig.resname
115 print defaultConfig.binpath
116 print defaultConfig.envpath
117 except Exception, ex:
118 sys.stderr.write('ERROR: %s\n' % str(ex))
123 def TEST_getDefaultConfig_withError():
124 global CONFIG_FILENAME
125 CONFIG_FILENAME = "toto.cfg"
127 configReader = ConfigReader()
128 defaultConfig = configReader.getDefaultConfig()
129 except UiException, err:
130 print 'ERROR: %s' % str(err)
136 from salome.kernel import unittester
137 moduleName = "configreader"
140 unittester.run(moduleName, "TEST_getDefaultConfig")
141 unittester.run(moduleName, "TEST_getDefaultConfig_withError")
143 if __name__ == "__main__":
145 #pluginspath=os.environ["SALOME_PLUGINS_PATH"]
146 #for path in pluginspath.split(":"):
147 # sys.path.insert(0,path)