Module globalPluginHandler
[hide private]
[frames] | no frames]

Source Code for Module globalPluginHandler

 1  #globalPluginHandler.py 
 2  #A part of NonVisual Desktop Access (NVDA) 
 3  #This file is covered by the GNU General Public License. 
 4  #See the file COPYING for more details. 
 5  #Copyright (C) 2010 James Teh <jamie@jantrid.net> 
 6   
 7  import sys 
 8  import pkgutil 
 9  import config 
10  import baseObject 
11  from logHandler import log 
12  import globalPlugins 
13   
14  #: All currently running global plugins. 
15  runningPlugins = set() 
16   
17 -def listPlugins():
18 for loader, name, isPkg in pkgutil.iter_modules(globalPlugins.__path__): 19 if name.startswith("_"): 20 continue 21 try: 22 plugin = __import__("globalPlugins.%s" % name, globals(), locals(), ("globalPlugins",)).GlobalPlugin 23 except: 24 log.error("Error importing global plugin %s" % name, exc_info=True) 25 continue 26 yield plugin
27
28 -def initialize():
29 config.addConfigDirsToPythonPackagePath(globalPlugins) 30 for plugin in listPlugins(): 31 try: 32 runningPlugins.add(plugin()) 33 except: 34 log.error("Error initializing global plugin %r" % plugin, exc_info=True)
35
36 -def terminate():
37 for plugin in list(runningPlugins): 38 runningPlugins.discard(plugin) 39 try: 40 plugin.terminate() 41 except: 42 log.exception("Error terminating global plugin %r" % plugin)
43
44 -def reloadGlobalPlugins():
45 """Reloads running global plugins. 46 """ 47 global globalPlugins 48 terminate() 49 del globalPlugins 50 mods=[k for k,v in sys.modules.iteritems() if k.startswith("globalPlugins") and v is not None] 51 for mod in mods: 52 del sys.modules[mod] 53 import globalPlugins 54 initialize()
55
56 -class GlobalPlugin(baseObject.ScriptableObject):
57 """Base global plugin. 58 Global plugins facilitate the implementation of new global commands, 59 support for objects which may be found across many applications, etc. 60 Each global plugin should be a separate Python module in the globalPlugins package containing a C{GlobalPlugin} class which inherits from this base class. 61 Global plugins can implement and bind gestures to scripts which will take effect at all times. 62 See L{ScriptableObject} for details. 63 Global plugins can also receive NVDAObject events for all NVDAObjects. 64 This is done by implementing methods called C{event_eventName}, 65 where C{eventName} is the name of the event; e.g. C{event_gainFocus}. 66 These event methods take two arguments: the NVDAObject on which the event was fired 67 and a callable taking no arguments which calls the next event handler. 68 """ 69
70 - def terminate(self):
71 """Terminate this global plugin. 72 This will be called when NVDA is finished with this global plugin. 73 """
74
75 - def chooseNVDAObjectOverlayClasses(self, obj, clsList):
76 """Choose NVDAObject overlay classes for a given NVDAObject. 77 This is called when an NVDAObject is being instantiated after L{NVDAObjects.NVDAObject.findOverlayClasses} has been called on the API-level class. 78 This allows a global plugin to add or remove overlay classes. 79 See L{NVDAObjects.NVDAObject.findOverlayClasses} for details about overlay classes. 80 @param obj: The object being created. 81 @type obj: L{NVDAObjects.NVDAObject} 82 @param clsList: The list of classes, which will be modified by this method if appropriate. 83 @type clsList: list of L{NVDAObjects.NVDAObject} 84 """
85