Added inclusion logic to hooke.plugin
[hooke.git] / hooke / plugin / __init__.py
1 #!/usr/bin/env python
2 """The plugin module provides optional submodules that add new Hooke
3 commands.
4
5 All of the science happens in here.
6 """
7
8 import os.path
9
10 from ..config import Setting
11 from ..util.graph import Node, Graph
12
13 PLUGIN_MODULES = [
14     ('autopeak', True),
15     ('curvetools', True),
16     ('cut', True),
17     ('fit', True),
18     ('flatfilts-rolf', True),
19     ('flatfilts', True),
20     ('generalclamp', True),
21     ('generaltccd', True),
22     ('generalvclamp', True),
23     ('jumpstat', True),
24     ('macro', True),
25     ('massanalysis', True),
26     ('multidistance', True),
27     ('multifit', True),
28     ('pcluster', True),
29     ('procplots', True),
30     ('review', True),
31     ('showconvoluted', True),
32     ('superimpose', True),
33     ('tutorial', True),
34     ('viewer', True),
35     ]
36 """List of plugin modules and whether they should be included by
37 default.
38 """
39
40 def Plugin (object):
41     """The pluggable collection of Hooke commands.
42
43     Fulfills the same role for Hooke that a software package does for
44     an operating system.
45     """
46     def dependencies(self):
47         """Return a list of Plugins we require."""
48         return []
49     def commands(self):
50         """Return a list of Commands provided."""
51         return []
52     def default_settings(self):
53         """Return a list of hooke.config.Settings() for any
54         configurable module settings."""
55         return []
56
57 PLUGINS = {}
58 """(name,instance) :class:`dict` of all possible :class:`Plugin`\s.
59 """
60
61 print __name__
62 for plugin_modname,value in PLUGIN_MODULES:
63     this_mod = __import__(__name__, fromlist=[plugin_modname])
64     plugin_mod = getattr(this_mod, plugin_modname)
65     for objname in dir(plugin_mod):
66         obj = getattr(plugin_mod, objname)
67         if type(obj) == Plugin:
68             obj.module_name = plugin_modname
69             PLUGINS[p.name] = p
70
71 PLUGIN_GRAPH = Graph([Node(
72             [PLUGINS[name] for name in p.dependencies()]
73             )])
74 PLUGIN_GRAPH.topological_sort()
75
76 #class Commands(wx.Panel):
77 #
78 #    def __init__(self, parent):
79 #        # Use the WANTS_CHARS style so the panel doesn't eat the Return key.
80 #        wx.Panel.__init__(self, parent, -1, style=wx.WANTS_CHARS|wx.NO_BORDER, size=(160, 200))
81 #
82 #        self.CommandsTree = wx.TreeCtrl(self, -1, wx.Point(0, 0), wx.Size(160, 250), wx.TR_DEFAULT_STYLE|wx.NO_BORDER|wx.TR_HIDE_ROOT)
83 #        imglist = wx.ImageList(16, 16, True, 2)
84 #        imglist.Add(wx.ArtProvider.GetBitmap(wx.ART_FOLDER, wx.ART_OTHER, wx.Size(16, 16)))
85 #        imglist.Add(wx.ArtProvider.GetBitmap(wx.ART_EXECUTABLE_FILE, wx.ART_OTHER, wx.Size(16, 16)))
86 #        self.CommandsTree.AssignImageList(imglist)
87 #        self.CommandsTree.AddRoot('Commands and Settings', 0)
88 #
89 #        self.ExecuteButton = wx.Button(self, -1, 'Execute')
90 #
91 #        sizer = wx.BoxSizer(wx.VERTICAL)
92 #        sizer.Add(self.CommandsTree, 1, wx.EXPAND)
93 #        sizer.Add(self.ExecuteButton, 0, wx.EXPAND)
94 #
95 #        self.SetSizer(sizer)
96 #        sizer.Fit(self)
97 #
98 #    def Initialize(self, plugins):
99 #        tree_root = self.CommandsTree.GetRootItem()
100 #        for plugin in plugins:
101 #            filename = ''.join([plugin, '.ini'])
102 #            path = lh.get_file_path(filename, ['plugins'])
103 #            config = ConfigObj()
104 #            if os.path.isfile(path):
105 #                config.filename = path
106 #                config.reload()
107 #                #append the ini file to the plugin
108 #                plugin_root = self.CommandsTree.AppendItem(tree_root, plugin, 0, data=wx.TreeItemData(config))
109 #            else:
110 #                plugin_root = self.CommandsTree.AppendItem(tree_root, plugin, 0)
111 #
112 #            #add all commands to the tree
113 #            for command in plugins[plugin]:
114 #                command_label = command.replace('do_', '')
115 #                #do not add the ini file to the command (we'll access the ini file of the plugin (ie parent) instead, see above)
116 #                self.CommandsTree.AppendItem(plugin_root, command_label, 1)
117 #            self.CommandsTree.Expand(plugin_root)
118
119 def default_settings(self):
120     settings = [Setting(
121             'plugins', help='Enable/disable default plugins.')]
122     for pnode in PLUGIN_GRAPH:
123         settings.append(Setting(p.name, str(PLUGIN_MODULES[p.module_name][1])))      
124     for pnode in PLUGIN_GRAPH:
125         plugin = pnode.data
126         settings.extend(plugin.default_settings())
127     return settings
128
129 class Command (object):
130     pass