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 CONFIG_FILENAME = "padder.cfg"
30 TYPE_REMOTE = 'remote'
31 TYPES=[TYPE_LOCAL, TYPE_REMOTE]
35 # The first step is to look for the config file. This file
36 # is supposed to be located in the same directory than the
37 # padder plugin. Then, we have to scan the directories
38 # specified in the SALOME plugins path.
39 self.__configFilename = None
40 pluginspath=os.environ["SALOME_PLUGINS_PATH"]
41 for path in pluginspath.split(":"):
42 filename = os.path.join(path,CONFIG_FILENAME)
43 if os.path.exists(filename):
44 self.__configFilename = filename
46 if self.__configFilename is None:
47 msg = "The configuration file %s can't be found in SALOME_PLUGINS_PATH"
48 raise AdminException(msg%CONFIG_FILENAME)
50 print "The configuration file is : %s"%self.__configFilename
51 self.__configparser = ConfigParser.RawConfigParser()
52 self.__configparser.read(self.__configFilename)
54 def getLocalConfig(self):
55 return self.__getConfig(TYPE_LOCAL)
57 def getRemoteConfig(self):
58 return self.__getConfig(TYPE_REMOTE)
60 def getDefaultConfig(self):
61 defaultType = self.__getDefaultType()
62 return self.__getConfig(defaultType)
64 def __getConfig(self, type=TYPE_LOCAL):
65 configName = self.__configparser.get('resources', type)
66 resname = self.__configparser.get(configName, 'resname')
67 binpath = self.__configparser.get(configName, 'binpath')
68 envpath = self.__configparser.get(configName, 'envpath')
69 config = ConfigParameter(resname, binpath, envpath)
70 config.resname = resname
73 def __getDefaultType(self):
74 '''This returns the default type read in the config file ([resources], default)'''
75 defaultType = self.__configparser.get('preferences', 'defaultres')
76 if defaultType not in TYPES:
81 # =========================================================================
83 # =========================================================================
85 def TEST_getDefaultConfig():
87 configReader = ConfigReader()
88 defaultConfig = configReader.getDefaultConfig()
89 print defaultConfig.resname
90 print defaultConfig.binpath
91 print defaultConfig.envpath
93 sys.stderr.write('ERROR: %s\n' % str(ex))
98 def TEST_getDefaultConfig_withError():
99 global CONFIG_FILENAME
100 CONFIG_FILENAME = "toto.cfg"
102 configReader = ConfigReader()
103 defaultConfig = configReader.getDefaultConfig()
104 except UiException, err:
105 print 'ERROR: %s' % str(err)
111 from salome.kernel import unittester
112 moduleName = "configreader"
115 unittester.run(moduleName, "TEST_getDefaultConfig")
116 unittester.run(moduleName, "TEST_getDefaultConfig_withError")
118 if __name__ == "__main__":
120 pluginspath=os.environ["SALOME_PLUGINS_PATH"]
121 for path in pluginspath.split(":"):
122 sys.path.insert(0,path)