Package pallavi :: Module PluginManager
[hide private]
[frames] | no frames]

Source Code for Module pallavi.PluginManager

  1  # Copyright (c) 2006 Dusty Phillips 
  2   
  3  # Permission is hereby granted, free of charge, to any person obtaining a 
  4  # copy of this software and associated documentation files (the "Software"), 
  5  # to deal in the Software without restriction, including without limitation 
  6  # the rights to use, copy, modify, merge, publish, distribute, sublicense, 
  7  # and/or sell copies of the Software, and to permit persons to whom the 
  8  # Software is furnished to do so, subject to the following conditions: 
  9   
 10  # The above copyright notice and this permission notice shall be included in 
 11  # all copies or substantial portions of the Software. 
 12   
 13  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 14  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 15  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
 16  # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 17  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
 18  # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 19  # DEALINGS IN THE SOFTWARE. 
 20   
 21  '''This module manages plugins and macro code. The difference is that 
 22  macros are loaded dynamically and plugins are loaded from files. Macros tend 
 23  to be shorter snippets executed once while plugins are loaded into memory and 
 24  interact with the core.''' 
 25   
 26  import sys, traceback 
 27   
28 -class PluginManager(object):
29 - def __init__(self, pallavi):
30 self.pallavi = pallavi
31
32 - def LoadPlugin(self, pluginName):
33 '''Load a Python plugin. It needs to be on the path (ie: in the plugins dir) 34 A plugin is simply a module with a setup function. Variables are set in the module 35 for the view, config, eventBus, actions list and pluginManager before setup 36 is called''' 37 try: 38 plugin = self.__MyImport(pluginName) 39 except: 40 traceback.print_exc() 41 print "-" * 40 + "\n\nError Loading %s" % pluginName 42 return 43 plugin.pallavi = self.pallavi 44 plugin.pluginManager = self 45 plugin.config = self.pallavi.config 46 plugin.eventBus = self.pallavi.eventBus 47 plugin.actions = self.pallavi.actions 48 plugin.setup()
49
50 - def LoadPlugins(self, pluginList):
51 '''Load a list of plugins. The list should be of names of python modules in 52 the path (normally the plugins dir).''' 53 for plugin in pluginList: 54 self.LoadPlugin(plugin)
55
56 - def LoadDefaultPlugins(self, pluginList):
57 '''Load a list of plugins. The list should be names of python modules in the 58 pallavi.plugins directory. For example, to load the plugin 59 pallavi.plugins.Default_Keybinding, the list should contain 'Default_Keybinding'; 60 the prefix will be prepended automatically.''' 61 newlist = [] 62 for plugin in pluginList: 63 newlist.append("pallavi.plugins." + plugin) 64 self.LoadPlugins(newlist)
65
66 - def ExecString(self, string):
67 '''Execute an arbitrary string. These variables will be defined: 68 69 pallavi 70 config 71 pluginManager 72 eventBus 73 actions 74 75 Note: This method is extremely unsafe. It allows arbitrary Python code 76 to be executed, including possibly malicious code. However, Python has 77 no sandbox or safeguards, the restricted environment is broken, and 78 there is no way around this. IMHO, its better to provide the macro 79 functionality and accept the risks. I suppose we could add a config 80 option to disable macros if necessary. Or this simple functionality 81 could actually be moved into a plugin itself.''' 82 pallavi = self.pallavi 83 config = self.pallavi.config 84 pluginManager = self 85 eventBus = self.pallavi.eventBus 86 actions = self.pallavi.actions 87 88 try: 89 exec(string) 90 except Exception, ex: 91 print ex
92
93 - def __MyImport(self, name):
94 '''Borrowed from python tutorial, does basic importing, I believe''' 95 mod = __import__(name) 96 components = name.split('.') 97 for comp in components[1:]: 98 mod = getattr(mod, comp) 99 return mod
100