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

Source Code for Module plugin

  1  # Name:         plugin.py 
  2  # Purpose:      Pluggable component support 
  3  # Author:       Roman Rolinsky <rolinsky@femagsoft.com> 
  4  # Created:      31.05.2007 
  5  # RCS-ID:       $Id: plugin.py 48573 2007-09-05 19:48:04Z ROL $ 
  6   
  7  ''' 
  8  Functions for loading plugins. 
  9  ''' 
 10   
 11  import os, sys, glob 
 12  from xml.dom import minidom 
 13  from globals import * 
 14  from presenter import Manager 
 15  import component 
 16  import meta 
 17   
18 -def load_plugins_from_dirs():
19 '''Load plugins from XRCEDPATH directories.''' 20 dirs = os.getenv('XRCEDPATH') 21 if dirs: 22 for dir in dirs.split(':'): 23 if os.path.isdir(dir): 24 load_plugins(dir)
25
26 -def load_plugins(dir):
27 '''Load plugins from C{dir}.''' 28 sys_path = sys.path 29 cwd = os.getcwd() 30 dir = os.path.abspath(os.path.normpath(dir)) 31 TRACE('* load_plugins %s' % dir) 32 os.chdir(dir) 33 sys.path = sys_path + [dir] 34 try: 35 ff_py = glob.glob('[!_]*.py') 36 for f in ff_py: 37 name = os.path.splitext(f)[0] 38 TRACE('* __import__ %s', name) 39 try: 40 __import__(name, globals(), locals(), ['*']) 41 except ImportError: 42 logger.exception('importing %s failed', name) 43 ff_crx = glob.glob('*.crx') 44 for crx in ff_crx: 45 TRACE('* load_crx %s', crx) 46 try: 47 load_crx(crx) 48 except: 49 logger.exception('parsing CRX file %s failed', crx) 50 dirs = glob.glob('*/') 51 for dir in dirs: 52 if os.path.isfile(os.path.join(dir, '__init__.py')): 53 TRACE('* __import__ %s/__init__.py', f) 54 try: 55 __import__(dir, globals(), locals(), ['*']) 56 except ImportError: 57 logger.exception('importing %s/__init__.py failed', f) 58 finally: 59 sys.path = sys_path 60 os.chdir(cwd)
61
62 -def load_crx(filename):
63 '''Load components defined in a manifest file.''' 64 dom = minidom.parse(filename) 65 for node in dom.documentElement.childNodes: 66 if node.nodeType == node.ELEMENT_NODE and node.tagName == 'component': 67 create_component(node)
68
69 -def create_component(node):
70 '''Create component from a manifest data. 71 72 @param node: DOM C{Element} object containing component manifest data. 73 ''' 74 klass = node.getAttribute('class') 75 name = node.getAttribute('name') 76 TRACE('create_component %s', name) 77 comp = getattr(meta, klass) 78 compClass = getattr(component, comp.klass) # get component class 79 attributesIn = comp.getAttribute(node, 'attributes') 80 # Process attr:klass pairs 81 attributes = [] 82 specials = {} 83 for a in attributesIn: 84 i = a.find(':') 85 if i != -1: 86 a,kl = a[:i],a[i+1:] 87 specials[a] = getattr(component, kl) 88 attributes.append(a) 89 groups = comp.getAttribute(node, 'groups') 90 styles = comp.getAttribute(node, 'styles') 91 events = comp.getAttribute(node, 'events') 92 c = compClass(name, groups, attributes, specials=specials, events=events) 93 c.hasName = bool(comp.getAttribute(node, 'has-name')) 94 c.addStyles(*styles) 95 Manager.register(c) 96 menu = comp.getAttribute(node, 'menu') 97 label = comp.getAttribute(node, 'label') 98 if menu and label: 99 try: 100 index = int(comp.getAttribute(node, 'index')) 101 except: 102 index = 1000 103 help = comp.getAttribute(node, 'help') 104 Manager.setMenu(c, menu, label, help, index) 105 panel = comp.getAttribute(node, 'panel') 106 if panel: 107 try: 108 pos = map(int, comp.getAttribute(node, 'pos').split(',')) 109 except: 110 pos = component.DEFAULT_POS 111 try: 112 span = map(int, comp.getAttribute(node, 'span').split(',')) 113 except: 114 span = (1, 1) 115 Manager.setTool(c, panel, pos=pos, span=span) 116 dlName = comp.getAttribute(node, 'DL') 117 if dlName: 118 TRACE('Loading dynamic library: %s', dlName) 119 if not g._CFuncPtr: 120 try: 121 import ctypes 122 g._CFuncPtr = ctypes._CFuncPtr 123 except: 124 print 'import ctypes module failed' 125 if g._CFuncPtr: 126 dl = ctypes.CDLL(dlName) 127 try: 128 Manager.addXmlHandler(dl.AddXmlHandlers) 129 except: 130 logger.exception('DL registration failed') 131 module = comp.getAttribute(node, 'module') 132 handler = comp.getAttribute(node, 'handler') 133 if module and handler: 134 TRACE('importing handler %s from %s', handler, module) 135 try: 136 mod = __import__(module, globals(), locals(), [handler]) 137 Manager.addXmlHandler(getattr(mod, handler)) 138 except ImportError: 139 logger.exception("can't import handler module") 140 except AttributeError: 141 logger.exception("can't find handler class")
142