Salome HOME
premiere version
[tools/eficas.git] / Extensions / pluginloader.py
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2007-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     Ce module contient le chargeur dynamique de plugins (emprunté à HappyDoc)
22 """
23
24 import glob,os,sys,traceback
25 import UserDict
26
27 class PluginLoader(UserDict.UserDict):
28    def __init__(self,module):
29       UserDict.UserDict.__init__(self)
30       self.plugin_dir=module.__path__[0]
31       self.plugin_set_name=module.__name__
32       _module_list = glob.glob( os.path.join( self.plugin_dir,
33                               '%s*py' % self.plugin_set_name,
34                                            )
35                               )
36       _module_list.sort()
37
38       for _module_name in _module_list:
39
40         _module_name = os.path.basename(_module_name)[:-3]
41         _import_name = '%s.%s' % ( self.plugin_set_name,
42                                    _module_name )
43
44         try:
45           _module = __import__( _import_name )
46         except:
47           sys.stderr.write('\n--- Plugin Module Error ---\n')
48           traceback.print_exc()
49           sys.stderr.write('---------------------------\n\n')
50           continue
51         try:
52           _module = getattr(_module, _module_name)
53         except AttributeError:
54           sys.stderr.write('ERROR: Could not retrieve %s\n' % _import_name)
55
56         try:
57           info = _module.entryPoint()
58         except AttributeError:
59           pass
60         else:
61           self.addEntryPoint(info)
62
63
64    def addEntryPoint(self,infoDict):
65       name=infoDict['name']
66       factory=infoDict['factory']
67       self[name]=factory
68