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