Salome HOME
untabify
[modules/smesh.git] / src / Tools / padder / spadderpy / configreader.py
1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2011-2013  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
21 # Author(s): Guillaume Boulant (23/03/2011)
22 #
23
24 import sys, os
25 import ConfigParser
26 from MESHJOB import ConfigParameter
27 from salome.kernel.uiexception import AdminException, UiException
28
29 from salome_pluginsmanager import PLUGIN_PATH_PATTERN
30 CONFIG_RELPATH  = os.path.join(PLUGIN_PATH_PATTERN,'smesh')
31 CONFIG_FILENAME = "padder.cfg"
32 TYPE_LOCAL   = 'local'
33 TYPE_REMOTE  = 'remote'
34 TYPES=[TYPE_LOCAL, TYPE_REMOTE]
35
36 class ConfigReader:
37     def __init__(self):
38         # The first step is to look for the config file. This file
39         # is supposed to be located in the same directory than the
40         # padder plugin. Then, we have to scan the directories
41         # specified in the SALOME plugins path.
42         self.__configFilename = None
43         try:
44             smeshpath=os.environ["SMESH_ROOT_DIR"]
45         except KeyError, ex:
46             raise AdminException("You should define the variable SMESH_ROOT_DIR")
47
48         pluginspath = os.path.join(smeshpath,CONFIG_RELPATH)
49         filename    = os.path.join(pluginspath,CONFIG_FILENAME)
50         if os.path.exists(filename):
51             self.__configFilename = filename
52         else:
53             msg = "The configuration file %s can't be found in the SMESH plugins path %s"
54             raise AdminException(msg%(CONFIG_FILENAME,pluginspath))
55
56         print "The configuration file is : %s"%self.__configFilename
57         self.__configparser = ConfigParser.RawConfigParser()
58         try:
59             self.__configparser.read(self.__configFilename)
60         except ConfigParser.ParsingError, ex:
61             raise AdminException(ex.message)
62
63     def getLocalConfig(self):
64         return self.__getConfig(TYPE_LOCAL)
65     
66     def getRemoteConfig(self):
67         return self.__getConfig(TYPE_REMOTE)
68
69     def getDefaultConfig(self):
70         defaultType = self.__getDefaultType()
71         return self.__getConfig(defaultType)
72         
73     def __getConfig(self, type=TYPE_LOCAL):
74         configName = self.__configparser.get('resources', type)
75         resname = self.__configparser.get(configName, 'resname')
76         binpath = self.__configparser.get(configName, 'binpath')
77         envpath = self.__configparser.get(configName, 'envpath')
78         config = ConfigParameter(resname, binpath, envpath)
79         config.resname = resname
80         return config
81
82     def __getDefaultType(self):
83         '''This returns the default type read in the config file ([resources], default)'''
84         defaultType = self.__configparser.get('preferences', 'defaultres')
85         if defaultType not in TYPES:
86             return TYPE_LOCAL
87         return defaultType
88
89 def printConfig(config):
90     print "PADDER CONFIGURATION:"
91     print "\tconfig.resname = %s"%config.resname
92     print "\tconfig.binpath = %s"%config.binpath
93     print "\tconfig.envpath = %s"%config.envpath
94     
95 def getPadderTestDir(config):
96     """
97     This function returns the directory of the SpherePadder
98     installation, where the tests cases are located. This should be
99     used for test only. It makes the hypothesis that the binpath to
100     the executable program is a path of the executable program of a
101     complete installation of SpherePadder.
102     """
103     testdir=os.path.join(os.path.abspath(os.path.dirname(config.binpath)),"tests")
104     return testdir
105
106 #
107 # =========================================================================
108 # Test runner
109 # =========================================================================
110 #
111 def TEST_getDefaultConfig():
112     try:
113         configReader = ConfigReader()
114         defaultConfig = configReader.getDefaultConfig()
115         print defaultConfig.resname
116         print defaultConfig.binpath
117         print defaultConfig.envpath
118     except Exception, ex:
119         sys.stderr.write('ERROR: %s\n' % str(ex))
120         return False
121     
122     return True
123
124 def TEST_getDefaultConfig_withError():
125     global CONFIG_FILENAME
126     CONFIG_FILENAME = "toto.cfg"
127     try:
128         configReader = ConfigReader()
129         defaultConfig = configReader.getDefaultConfig()
130     except UiException, err:
131         print 'ERROR: %s' % str(err)
132         return True
133     
134     return False
135
136
137 from salome.kernel import unittester
138 moduleName = "configreader"
139
140 def testsuite():
141     unittester.run(moduleName, "TEST_getDefaultConfig")
142     unittester.run(moduleName, "TEST_getDefaultConfig_withError")
143     
144 if __name__ == "__main__":
145     #import os, sys
146     #pluginspath=os.environ["SALOME_PLUGINS_PATH"]
147     #for path in pluginspath.split(":"):
148     #    sys.path.insert(0,path)
149     
150     testsuite()