1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2011 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 SALOME_PLUGINS_PATH")
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:
89 # =========================================================================
91 # =========================================================================
93 def TEST_getDefaultConfig():
95 configReader = ConfigReader()
96 defaultConfig = configReader.getDefaultConfig()
97 print defaultConfig.resname
98 print defaultConfig.binpath
99 print defaultConfig.envpath
100 except Exception, ex:
101 sys.stderr.write('ERROR: %s\n' % str(ex))
106 def TEST_getDefaultConfig_withError():
107 global CONFIG_FILENAME
108 CONFIG_FILENAME = "toto.cfg"
110 configReader = ConfigReader()
111 defaultConfig = configReader.getDefaultConfig()
112 except UiException, err:
113 print 'ERROR: %s' % str(err)
119 from salome.kernel import unittester
120 moduleName = "configreader"
123 unittester.run(moduleName, "TEST_getDefaultConfig")
124 unittester.run(moduleName, "TEST_getDefaultConfig_withError")
126 if __name__ == "__main__":
128 pluginspath=os.environ["SALOME_PLUGINS_PATH"]
129 for path in pluginspath.split(":"):
130 sys.path.insert(0,path)