~gladex/gladex/plugin-impl

« back to all changes in this revision

Viewing changes to src/plugins/__init__.py

  • Committer: Oumar Aziz OUATTARA
  • Date: 2008-02-25 20:11:35 UTC
  • mfrom: (155.1.10 gladex)
  • Revision ID: wattazoum@wattazoum-laptop-20080225201135-wm13wuexdferqf9a
Plugin interface designed

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#try:
2
 
        #import python
3
 
#except:
4
 
        #pass
5
 
#try:
6
 
        #import test_mod
7
 
#except:
8
 
        #pass
9
 
#try:
10
 
        #import ruby
11
 
#except:
12
 
        #pass
13
 
#try:
14
 
        #import perl
15
 
#except:
16
 
        #pass
17
 
##
18
 
 
19
 
import python
20
 
# import test_mod # Use this as a template. This plugin has the minimum
21
 
                  # necessary functions.
22
 
import ruby
23
 
import perl
 
1
#    This program is free software; you can redistribute it and/or modify
 
2
#    it under the terms of the GNU General Public License as published by
 
3
#    the Free Software Foundation; either version 2 of the License, or
 
4
#    (at your option) any later version.
 
5
#
 
6
#    This program is distributed in the hope that it will be useful,
 
7
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
8
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
9
#    GNU General Public License for more details.
 
10
#
 
11
#    You should have received a copy of the GNU General Public License
 
12
#    along with this program; if not, write to the Free Software
 
13
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
14
# Authors :
 
15
#       Ouattara Oumar Aziz ( alias wattazoum ) <wattazoum@gmail.com>
 
16
 
 
17
import inspect
 
18
import os
 
19
import sys
 
20
import glob
 
21
import gettext.gettext as _
 
22
 
 
23
class Plugin:
 
24
        """
 
25
        This should be the plugin master class. To make a plugin you should inherit of this class
 
26
        """
 
27
        
 
28
        def getName(self):
 
29
                """
 
30
                Returns the name of the plugin
 
31
                @return: the name of the plugin
 
32
                @rtype: str
 
33
                """
 
34
                raise Exception('This method is mandatory !')
 
35
        
 
36
        def getDoc(self):
 
37
                """
 
38
                This method should give a little documentation about the this plugin.
 
39
                @return: The doc string
 
40
                """
 
41
                raise Exception("Help not implemented for this plugin")
 
42
        
 
43
        def getWidget(self):
 
44
                """
 
45
                Return the plugin widget to be added to the main window when the plugin is selected
 
46
                @return: gtk.Widget
 
47
                """
 
48
                raise Exception('This method is mandatory')
 
49
        
 
50
        def execute(self, gladeFile, destinationDir):
 
51
                """
 
52
                This method should do the task of generating the source code from the glade file.
 
53
                @param gladeFile: The glade file to generate th code from
 
54
                @param destinationDir: The destination directory to generate the file in.
 
55
                @raise Exception: An exception should be raised if anything went fine.
 
56
                """
 
57
                raise Exception('This method is mandatory')
 
58
 
 
59
 
 
60
class PluginManager :
 
61
        """
 
62
        """
 
63
        # This should be a dictionary of plugins
 
64
        __pluginList = None
 
65
 
 
66
        def getPlugins(self):
 
67
                """
 
68
                Search for plugins into the plugin directory and load them.
 
69
                @return : The plugins dictionary list {'name':class}. Look at Plugin to know how it's used
 
70
                """
 
71
                global __pluginList
 
72
                
 
73
                if self.__pluginList : return self.__pluginList
 
74
                else :
 
75
                        self.__pluginList = dict()
 
76
                        tmp = inspect.getabsfile(inspect.getmodule(self))
 
77
                        plugins_dir = os.path.dirname(tmp)
 
78
                        if os.path.isdir(plugins_dir):
 
79
                                if plugins_dir not in sys.path:
 
80
                                        sys.path.append(plugins_dir)
 
81
                           
 
82
                                for file in glob.glob('%s/*/*.py' % plugins_dir):
 
83
                                        try:
 
84
                                                module_filename = os.path.basename(file)
 
85
                                                module_name, _ = os.path.splitext(module_filename)
 
86
                                                plugin = __import__(module_name, '', module_filename)
 
87
                                                for symbol_name in dir(plugin):
 
88
                                                        symbol = getattr(plugin, symbol_name)
 
89
                                                        if inspect.isclass(symbol) and symbol != Plugin and \
 
90
                                       issubclass(symbol, Plugin):
 
91
                                    #symbol.enabled = symbol.name in plugin_names
 
92
                                                                self.__pluginList[symbol_name] = symbol
 
93
                                    
 
94
                                        except Exception, e:
 
95
                                                print(_("Could not import plugin %(plugin_name)s ! Cause : %(error_cause)s ") % {'plugin_name':file,'error_cause': str(e)})                    
 
96
                                                continue
 
97
                        return self.__pluginList
 
98
                
 
99