Salome HOME
Integration of the PADDER algorithm as a simple SALOME plugin.
[modules/smesh.git] / src / Tools / padder / spadderpy / configreader.py
1 # -*- coding: iso-8859-1 -*-
2 #  Copyright (C) 2011 EDF R&D
3 #
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.
8 #
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.
13 #
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
17 #
18 #  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20 # Author(s): Guillaume Boulant (23/03/2011)
21 #
22
23 import sys, os
24 import ConfigParser
25 from MESHJOB import ConfigParameter
26 from salome.kernel.uiexception import AdminException, UiException
27
28 CONFIG_FILENAME = "padder.cfg"
29 TYPE_LOCAL   = 'local'
30 TYPE_REMOTE  = 'remote'
31 TYPES=[TYPE_LOCAL, TYPE_REMOTE]
32
33 class ConfigReader:
34     def __init__(self):
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
45                 break
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)
49
50         print "The configuration file is : %s"%self.__configFilename
51         self.__configparser = ConfigParser.RawConfigParser()
52         self.__configparser.read(self.__configFilename)
53
54     def getLocalConfig(self):
55         return self.__getConfig(TYPE_LOCAL)
56     
57     def getRemoteConfig(self):
58         return self.__getConfig(TYPE_REMOTE)
59
60     def getDefaultConfig(self):
61         defaultType = self.__getDefaultType()
62         return self.__getConfig(defaultType)
63         
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
71         return config
72
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:
77             return TYPE_LOCAL
78         return defaultType
79
80 #
81 # =========================================================================
82 # Test runner
83 # =========================================================================
84 #
85 def TEST_getDefaultConfig():
86     try:
87         configReader = ConfigReader()
88         defaultConfig = configReader.getDefaultConfig()
89         print defaultConfig.resname
90         print defaultConfig.binpath
91         print defaultConfig.envpath
92     except Exception, ex:
93         sys.stderr.write('ERROR: %s\n' % str(ex))
94         return False
95     
96     return True
97
98 def TEST_getDefaultConfig_withError():
99     global CONFIG_FILENAME
100     CONFIG_FILENAME = "toto.cfg"
101     try:
102         configReader = ConfigReader()
103         defaultConfig = configReader.getDefaultConfig()
104     except UiException, err:
105         print 'ERROR: %s' % str(err)
106         return True
107     
108     return False
109
110
111 from salome.kernel import unittester
112 moduleName = "configreader"
113
114 def testsuite():
115     unittester.run(moduleName, "TEST_getDefaultConfig")
116     unittester.run(moduleName, "TEST_getDefaultConfig_withError")
117     
118 if __name__ == "__main__":
119     import os, sys
120     pluginspath=os.environ["SALOME_PLUGINS_PATH"]
121     for path in pluginspath.split(":"):
122         sys.path.insert(0,path)
123     
124     testsuite()