Salome HOME
Padder execution error management (the errors are raised toward the gui)
[modules/smesh.git] / src / Tools / padder / spadderpy / configreader.py
1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2011-2012  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 from salome_pluginsmanager import PLUGIN_PATH_PATTERN
29 CONFIG_RELPATH  = os.path.join(PLUGIN_PATH_PATTERN,'smesh')
30 CONFIG_FILENAME = "padder.cfg"
31 TYPE_LOCAL   = 'local'
32 TYPE_REMOTE  = 'remote'
33 TYPES=[TYPE_LOCAL, TYPE_REMOTE]
34
35 class ConfigReader:
36     def __init__(self):
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
42         try:
43             smeshpath=os.environ["SMESH_ROOT_DIR"]
44         except KeyError, ex:
45             raise AdminException("You should define the variable SMESH_ROOT_DIR")
46
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
51         else:
52             msg = "The configuration file %s can't be found in the SMESH plugins path %s"
53             raise AdminException(msg%(CONFIG_FILENAME,pluginspath))
54
55         print "The configuration file is : %s"%self.__configFilename
56         self.__configparser = ConfigParser.RawConfigParser()
57         try:
58             self.__configparser.read(self.__configFilename)
59         except ConfigParser.ParsingError, ex:
60             raise AdminException(ex.message)
61
62     def getLocalConfig(self):
63         return self.__getConfig(TYPE_LOCAL)
64     
65     def getRemoteConfig(self):
66         return self.__getConfig(TYPE_REMOTE)
67
68     def getDefaultConfig(self):
69         defaultType = self.__getDefaultType()
70         return self.__getConfig(defaultType)
71         
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
79         return config
80
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:
85             return TYPE_LOCAL
86         return defaultType
87
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
93     
94 def getPadderTestDir(config):
95     """
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.
101     """
102     testdir=os.path.join(os.path.abspath(os.path.dirname(config.binpath)),"tests")
103     return testdir
104
105 #
106 # =========================================================================
107 # Test runner
108 # =========================================================================
109 #
110 def TEST_getDefaultConfig():
111     try:
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))
119         return False
120     
121     return True
122
123 def TEST_getDefaultConfig_withError():
124     global CONFIG_FILENAME
125     CONFIG_FILENAME = "toto.cfg"
126     try:
127         configReader = ConfigReader()
128         defaultConfig = configReader.getDefaultConfig()
129     except UiException, err:
130         print 'ERROR: %s' % str(err)
131         return True
132     
133     return False
134
135
136 from salome.kernel import unittester
137 moduleName = "configreader"
138
139 def testsuite():
140     unittester.run(moduleName, "TEST_getDefaultConfig")
141     unittester.run(moduleName, "TEST_getDefaultConfig_withError")
142     
143 if __name__ == "__main__":
144     #import os, sys
145     #pluginspath=os.environ["SALOME_PLUGINS_PATH"]
146     #for path in pluginspath.split(":"):
147     #    sys.path.insert(0,path)
148     
149     testsuite()