Salome HOME
Merge from V6_main_20120808 08Aug12
[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
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
96 #
97 # =========================================================================
98 # Test runner
99 # =========================================================================
100 #
101 def TEST_getDefaultConfig():
102     try:
103         configReader = ConfigReader()
104         defaultConfig = configReader.getDefaultConfig()
105         print defaultConfig.resname
106         print defaultConfig.binpath
107         print defaultConfig.envpath
108     except Exception, ex:
109         sys.stderr.write('ERROR: %s\n' % str(ex))
110         return False
111     
112     return True
113
114 def TEST_getDefaultConfig_withError():
115     global CONFIG_FILENAME
116     CONFIG_FILENAME = "toto.cfg"
117     try:
118         configReader = ConfigReader()
119         defaultConfig = configReader.getDefaultConfig()
120     except UiException, err:
121         print 'ERROR: %s' % str(err)
122         return True
123     
124     return False
125
126
127 from salome.kernel import unittester
128 moduleName = "configreader"
129
130 def testsuite():
131     unittester.run(moduleName, "TEST_getDefaultConfig")
132     unittester.run(moduleName, "TEST_getDefaultConfig_withError")
133     
134 if __name__ == "__main__":
135     #import os, sys
136     #pluginspath=os.environ["SALOME_PLUGINS_PATH"]
137     #for path in pluginspath.split(":"):
138     #    sys.path.insert(0,path)
139     
140     testsuite()