a9f883737a8560d1da4a3144cfb0a62027784d38
[hooke.git] / hooke / plugin / __init__.py
1 #!/usr/bin/env python
2 '''
3 Commands and settings panel for Hooke
4
5 Displays commands and settings for Hooke in a tree control
6 (c) Dr. Rolf Schmidt, 2009
7 '''
8
9 from configobj import ConfigObj
10 import os.path
11 from validate import Validator
12 import wx
13
14 import libhooke as lh
15
16 class Commands(wx.Panel):
17
18     def __init__(self, parent):
19         # Use the WANTS_CHARS style so the panel doesn't eat the Return key.
20         wx.Panel.__init__(self, parent, -1, style=wx.WANTS_CHARS|wx.NO_BORDER, size=(160, 200))
21
22         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)
23         imglist = wx.ImageList(16, 16, True, 2)
24         imglist.Add(wx.ArtProvider.GetBitmap(wx.ART_FOLDER, wx.ART_OTHER, wx.Size(16, 16)))
25         imglist.Add(wx.ArtProvider.GetBitmap(wx.ART_EXECUTABLE_FILE, wx.ART_OTHER, wx.Size(16, 16)))
26         self.CommandsTree.AssignImageList(imglist)
27         self.CommandsTree.AddRoot('Commands and Settings', 0)
28
29         self.ExecuteButton = wx.Button(self, -1, 'Execute')
30
31         sizer = wx.BoxSizer(wx.VERTICAL)
32         sizer.Add(self.CommandsTree, 1, wx.EXPAND)
33         sizer.Add(self.ExecuteButton, 0, wx.EXPAND)
34
35         self.SetSizer(sizer)
36         sizer.Fit(self)
37
38     def Initialize(self, plugins):
39         tree_root = self.CommandsTree.GetRootItem()
40         for plugin in plugins:
41             filename = ''.join([plugin, '.ini'])
42             path = lh.get_file_path(filename, ['plugins'])
43             config = ConfigObj()
44             if os.path.isfile(path):
45                 config.filename = path
46                 config.reload()
47                 #append the ini file to the plugin
48                 plugin_root = self.CommandsTree.AppendItem(tree_root, plugin, 0, data=wx.TreeItemData(config))
49             else:
50                 plugin_root = self.CommandsTree.AppendItem(tree_root, plugin, 0)
51
52             #add all commands to the tree
53             for command in plugins[plugin]:
54                 command_label = command.replace('do_', '')
55                 #do not add the ini file to the command (we'll access the ini file of the plugin (ie parent) instead, see above)
56                 self.CommandsTree.AppendItem(plugin_root, command_label, 1)
57             self.CommandsTree.Expand(plugin_root)