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