]> SALOME platform Git repositories - tools/eficas.git/blob - Extensions/pluginloader.py
Salome HOME
fin portage python 3
[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 (emprunte a HappyDoc)
22 """
23
24 from __future__ import absolute_import
25 import glob,os,sys,traceback
26 try:
27    from UserDict import UserDict
28 except ImportError:
29    from collections import UserDict
30
31
32 class PluginLoader(UserDict):
33    def __init__(self,module):
34       UserDict.__init__(self)
35       self.plugin_dir=module.__path__[0]
36       self.plugin_set_name=module.__name__
37       _module_list = glob.glob( os.path.join( self.plugin_dir,
38                               '%s*py' % self.plugin_set_name,
39                                            )
40                               )
41       _module_list.sort()
42
43       for _module_name in _module_list:
44
45         _module_name = os.path.basename(_module_name)[:-3]
46         _import_name = '%s.%s' % ( self.plugin_set_name,
47                                    _module_name )
48
49         try:
50           _module = __import__( _import_name )
51         except:
52           sys.stderr.write('\n--- Plugin Module Error ---\n')
53           traceback.print_exc()
54           sys.stderr.write('---------------------------\n\n')
55           continue
56         try:
57           _module = getattr(_module, _module_name)
58         except AttributeError:
59           sys.stderr.write('ERROR: Could not retrieve %s\n' % _import_name)
60
61         try:
62           info = _module.entryPoint()
63         except AttributeError:
64           pass
65         else:
66           self.addEntryPoint(info)
67
68
69    def addEntryPoint(self,infoDict):
70       name=infoDict['name']
71       factory=infoDict['factory']
72       self[name]=factory
73