comparison orpg/tools/pluginui.py @ 0:4385a7d0efd1 grumpy-goblin

Deleted and repushed it with the 'grumpy-goblin' branch. I forgot a y
author sirebral
date Tue, 14 Jul 2009 16:41:58 -0500
parents
children c54768cffbd4
comparison
equal deleted inserted replaced
-1:000000000000 0:4385a7d0efd1
1 from orpg.orpg_wx import *
2 from orpg.orpgCore import *
3 import orpg.plugindb as plugindb
4 import orpg.dirpath
5
6 sys.path.append(orpg.dirpath.dir_struct["plugins"])
7
8 class PluginFrame(wx.Frame):
9 def __init__(self, parent):
10 wx.Frame.__init__(self, parent, wx.ID_ANY, "Plugin Control Panel")
11 self.panel = wx.Panel(self, wx.ID_ANY)
12 self.plugindb = plugindb.PluginDB()
13 self.parent = parent
14 self.startplugs = self.plugindb.GetList("plugincontroller", "startup_plugins", [])
15 self.available_plugins = {}
16 self.enabled_plugins = {}
17 self.pluginNames = []
18 self.SetMinSize((380, 480))
19 self._selectedPlugin = None
20 self.Bind(wx.EVT_CLOSE, self._close)
21
22 #Public Methods
23 def Start(self):
24 self.__buildGUI()
25 self._update(None)
26 self.base_sizer = wx.BoxSizer(wx.VERTICAL)
27 self.base_sizer.Add(self.panel, 1, wx.EXPAND)
28 self.panel.SetSizer(self.main_sizer)
29 self.panel.SetAutoLayout(True)
30 self.panel.Fit()
31 self.SetSizer(self.base_sizer)
32 self.SetAutoLayout(True)
33 self.Fit()
34
35 def get_activeplugins(self):
36 return self.enabled_plugins
37
38 def get_startplugins(self):
39 return self.startplugs
40
41 #Private Methods
42 def __buildGUI(self):
43 self.main_sizer = wx.BoxSizer(wx.VERTICAL)
44 self.btn_sizer = wx.BoxSizer(wx.HORIZONTAL)
45 self.btn_sizer2 = wx.BoxSizer(wx.HORIZONTAL)
46 self.head_sizer = wx.BoxSizer(wx.HORIZONTAL)
47 #pnl = wx.Panel(self.panel, wx.ID_ANY)
48 self.err_sizer = wx.BoxSizer(wx.VERTICAL)
49 self.err_sizer.Add(self.head_sizer, 0, wx.EXPAND)
50 self.errorMessage = wx.StaticText(self.panel, wx.ID_ANY, "")
51 self.err_sizer.Add(self.errorMessage, 0, wx.EXPAND)
52 self.main_sizer.Add(self.err_sizer, 0, wx.EXPAND)
53 self.pluginList = wx.ListCtrl(self.panel, wx.ID_ANY, style=wx.LC_SINGLE_SEL|wx.LC_REPORT|wx.LC_HRULES|wx.LC_SORT_ASCENDING)
54 self.pluginList.InsertColumn(1, "Name")
55 self.pluginList.InsertColumn(2, "Autostart")
56 self.pluginList.InsertColumn(3, "Author")
57 self.Bind(wx.EVT_LIST_ITEM_SELECTED, self._selectPlugin, self.pluginList)
58 self.Bind(wx.EVT_LIST_ITEM_DESELECTED, self._deselectPlugin, self.pluginList)
59 self.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self._togglePlugin, self.pluginList)
60 self.Bind(wx.EVT_LIST_COL_CLICK, self._sort, self.pluginList)
61 self.main_sizer.Add(self.pluginList, 1, wx.EXPAND)
62
63 self.enableAllBtn = wx.Button(self.panel, wx.ID_ANY, "Enable All")
64 self.enableBtn = wx.Button(self.panel, wx.ID_ANY, "Enable")
65 self.disableAllBtn = wx.Button(self.panel, wx.ID_ANY, "Disable All")
66 self.disableBtn = wx.Button(self.panel, wx.ID_ANY, "Disable")
67 self.autostartBtn = wx.Button(self.panel, wx.ID_ANY, "Autostart")
68 self.helpBtn = wx.Button(self.panel, wx.ID_ANY, "Plugin Info")
69 self.updateBtn = wx.Button(self.panel, wx.ID_ANY, "Update List")
70
71 self.Bind(wx.EVT_BUTTON, self._enableAll, self.enableAllBtn)
72 self.Bind(wx.EVT_BUTTON, self._enable, self.enableBtn)
73 self.Bind(wx.EVT_BUTTON, self._disableAll, self.disableAllBtn)
74 self.Bind(wx.EVT_BUTTON, self._disable, self.disableBtn)
75 self.Bind(wx.EVT_BUTTON, self._autostart, self.autostartBtn)
76 self.Bind(wx.EVT_BUTTON, self._help, self.helpBtn)
77 self.Bind(wx.EVT_BUTTON, self._update, self.updateBtn)
78
79 self.btn_sizer.Add(self.enableBtn, 0, wx.EXPAND)
80 self.btn_sizer.Add(self.disableBtn, 0, wx.EXPAND)
81 self.btn_sizer.Add(self.autostartBtn, 0, wx.EXPAND)
82 self.btn_sizer.Add(self.helpBtn, 0, wx.EXPAND)
83
84 self.btn_sizer2.Add(self.updateBtn, 0, wx.EXPAND)
85 self.btn_sizer2.Add(self.enableAllBtn, 0, wx.EXPAND)
86 self.btn_sizer2.Add(self.disableAllBtn, 0, wx.EXPAND)
87
88 self.__disablePluginBtns()
89
90 self.main_sizer.Add(self.btn_sizer, 0, wx.EXPAND)
91 self.main_sizer.Add(self.btn_sizer2, 0, wx.EXPAND)
92
93 def __disablePluginBtns(self):
94 self.enableBtn.Disable()
95 self.disableBtn.Disable()
96 self.autostartBtn.Disable()
97 self.helpBtn.Disable()
98
99 def __enablePluginBtns(self):
100 self.autostartBtn.Label = "Autostart"
101 self.enableBtn.Enable()
102 self.disableBtn.Enable()
103 self.autostartBtn.Enable()
104 self.helpBtn.Enable()
105
106 def __error(self, errMsg):
107 self.errorMessage.Label += "\n" + str(errMsg)
108 self.__doLayout()
109
110 def __clearError(self):
111 self.errorMessage.Label = ""
112 self.__doLayout()
113
114 def __checkIdx(self, evt):
115 if isinstance(evt, int):
116 return evt
117 elif self._selectedPlugin is not None:
118 return self._selectedPlugin
119 else:
120 dlg = wx.MessageDialog(None, "You need to select a plugin before you can use this!", 'ERROR', wx.OK)
121 dlg.ShowModal()
122 dlg.Destroy()
123 return None
124
125 def __impPlugin(self, pname):
126 try:
127 if "plugins." + pname in sys.modules:
128 del sys.modules["plugins." + pname]#to ensure that the newly-imported one will be used correctly. No, reload() is not a better way to do this.
129 mod = __import__("plugins." + pname)
130 plugin = getattr(mod, pname)
131 pdata = plugin.Plugin(self.plugindb, self.parent)
132 self.available_plugins[pdata.name] = [pname, pdata, pdata.author, pdata.help]
133 return plugin
134
135 except Exception, e:
136 self.__error(e)
137 traceback.print_exc()
138 print e
139
140 def __doLayout(self):
141 self.Freeze()
142 self.panel.Layout()
143 self.Fit()
144 self.Thaw()
145
146 def __pluginSort(self, item1, item2):
147 return cmp(self.pluginNames[item1], self.pluginNames[item2])
148
149 #Events
150 def _selectPlugin(self, evt):
151 self._selectedPlugin = evt.GetIndex()
152 self.__enablePluginBtns()
153 pname = self.pluginList.GetItem(self._selectedPlugin, 0).GetText()
154 info = self.available_plugins[pname]
155
156 if info[0] in self.enabled_plugins:
157 self.enableBtn.Disable()
158 else:
159 self.disableBtn.Disable()
160 if self.pluginList.GetItem(self._selectedPlugin, 1).GetText() == "X":
161 self.autostartBtn.Label = "Disable Autostart"
162
163 self.__doLayout()
164 self.pluginList.SetItemState(self._selectedPlugin, wx.LIST_STATE_SELECTED, wx.LIST_STATE_SELECTED)
165
166 def _deselectPlugin(self, evt):
167 self.__disablePluginBtns()
168 self._selectedPlugin = None
169
170 def _togglePlugin(self, evt):
171 idx = evt.GetIndex()
172 pname = self.pluginList.GetItem(idx, 0).GetText()
173 info = self.available_plugins[pname]
174
175 if info[0] in self.enabled_plugins:
176 self._disable(idx)
177 else:
178 self._enable(idx)
179
180 self.pluginList.SetItemState(self._selectedPlugin, 0, wx.LIST_STATE_SELECTED)
181
182 def _enableAll(self, evt):
183 for pname in self.available_plugins.iterkeys():
184 info = self.available_plugins[pname]
185 if info[0] not in self.enabled_plugins:
186 idx = self.pluginList.FindItem(-1, pname)
187 self._enable(idx)
188
189 def _enable(self, evt):
190 idx = self.__checkIdx(evt)
191 if idx is None:
192 return
193 pname = self.pluginList.GetItem(idx, 0).GetText()
194 info = self.available_plugins[pname]
195 info[1].menu_start()
196
197 try:
198 info[1].plugin_enabled()
199 except Exception, e:
200 self.__error(e)
201 traceback.print_exc()
202 print e
203 self.pluginList.SetItemBackgroundColour(idx, (255,0,0))
204 info[1].menu_cleanup()
205 return
206
207 self.enabled_plugins[info[0]] = info[1]
208 self.pluginList.SetItemBackgroundColour(idx, (0,255,0))
209 self.enableBtn.Disable()
210 self.disableBtn.Enable()
211
212 def _disableAll(self, evt):
213 for entry in self.enabled_plugins.keys():
214 idx = self.pluginList.FindItem(0, self.enabled_plugins[entry].name)
215 self._disable(idx)
216
217 def _disable(self, evt):
218 idx = self.__checkIdx(evt)
219 if idx is None:
220 return
221 pname = self.pluginList.GetItem(idx, 0).GetText()
222 info = self.available_plugins[pname]
223 info[1].menu_cleanup()
224 try:
225 info[1].plugin_disabled()
226 del self.enabled_plugins[info[0]]
227 except Exception, e:
228 self.__error(e)
229 traceback.print_exc()
230 print e
231 self.pluginList.SetItemBackgroundColour(idx, (255,0,0))
232 return
233 self.pluginList.SetItemBackgroundColour(idx, (255,255,255))
234 self.disableBtn.Disable()
235 self.enableBtn.Enable()
236
237 def _autostart(self, evt):
238 idx = self.__checkIdx(evt)
239 if idx is None:
240 return
241 if self.pluginList.GetItem(idx, 1).GetText() == "X":
242 self.startplugs.remove(self.pluginList.GetItem(idx, 0).GetText())
243 self.pluginList.SetStringItem(idx, 1, "")
244 self.autostartBtn.Label = "Autostart"
245 else:
246 self.startplugs.append(self.pluginList.GetItem(idx, 0).GetText())
247 self.pluginList.SetStringItem(idx, 1, "X")
248 self.autostartBtn.Label = "Disable Autostart"
249
250 self.plugindb.SetList("plugincontroller", "startup_plugins", self.startplugs)
251 self.__doLayout()
252
253 def _help(self, evt):
254 if isinstance(evt, int):
255 idx = evt
256 elif self._selectedPlugin is not None:
257 idx = self._selectedPlugin
258 else:
259 dlg = wx.MessageDialog(None, "You need to select a plugin before you can use this!", 'ERROR', wx.OK)
260 dlg.ShowModal()
261 dlg.Destroy()
262 return
263
264 pname = self.pluginList.GetItem(idx, 0).GetText()
265 info = self.available_plugins[pname]
266
267 msg = "Author(s):\t" + info[2] + "\n\n" + info[3]
268
269 dlg = wx.MessageDialog(None, msg, 'Plugin Information: ' + pname, wx.OK)
270 dlg.ShowModal()
271 dlg.Destroy()
272
273 def _update(self, evt):
274 self.__clearError()
275 self._disableAll(None)
276 self.available_plugins = {}
277 self.errorMessage.Label = ""
278 self.pluginList.DeleteAllItems()
279 self.pluginNames = []
280
281 list_of_plugin_dir = os.listdir(orpg.dirpath.dir_struct["plugins"])
282 for p in list_of_plugin_dir:
283 #print p[:2]; print p[-4:]
284 if p[:2].lower()=="xx" and p[-3:]==".py":
285 self.__impPlugin(p[:-3])
286 elif p[:2].lower()=="xx" and p[-4:]==".pyc":
287 self.__impPlugin(p[:-4])
288
289 i = 0
290 for plugname, info in self.available_plugins.iteritems():
291 self.pluginNames.append(plugname)
292 idx = self.pluginList.InsertStringItem(self.pluginList.GetItemCount(), plugname)
293 self.pluginList.SetStringItem(idx, 2, info[2])
294 if plugname in self.startplugs:
295 self.pluginList.SetStringItem(idx, 1, "X")
296 self._enable(idx)
297 self.pluginList.SetItemData(idx, i)
298 i += 1
299 self.pluginList.SetColumnWidth(0, wx.LIST_AUTOSIZE)
300 self.__doLayout()
301 self.__disablePluginBtns()
302
303 def _close(self, evt):
304 self.Hide()
305
306 def _sort(self, evt):
307 self.pluginList.SortItems(self.__pluginSort)