Changeset 287

Show
Ignore:
Timestamp:
03/07/06 02:29:52 (3 years ago)
Author:
athomas
Message:
  • Added module attrtree.
  • Added util.dict_diff().
  • ComponentMeta.get_implementors() now returns a list rather than a dictionary.
  • Added some more component introspection to ComponentMeta.
  • Fixed some bugs in loader.
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • pycrash/trunk/crash/component.py

    r284 r287  
    2222#         Christopher Lenz <cmlenz@gmx.de> 
    2323 
     24__all__ = ['Component', 'ComponentManager', 'ExtensionPoint', 'implements', 
     25    'SelectedExtensionPoint', 'Interface', 'ComponentError'] 
     26 
    2427class ComponentError(Exception): pass 
    25  
    26 __all__ = ['Component', 'ComponentManager', 'ExtensionPoint', 'implements', 
    27     'SelectedExtensionPoint', 'Interface'] 
    2828 
    2929class Interface(object): 
     
    8181            raise ComponentError("can't find selected interface '%s' named '%s'" % (xtnpt.interface.__name__, seek)) 
    8282        else: 
    83             raise ComponentError("no component active for '%s' interface" % xtnpt.interface.__name__) 
     83            raise ComponentError("no component active for interface '%s'" % xtnpt.interface.__name__) 
    8484 
    8585class ComponentMeta(type): 
     
    220220 
    221221    def get_implementors(self, interface): 
    222         """ Get all component classes that implement a particular interface as 
    223         a dictionary, with the class name as the key and the class itself as 
    224         the value. Does not instantiate the component. """ 
    225         return dict((c.__name__, c) for c in ComponentMeta._components \ 
    226             if hasattr(c, '_implements') and interface in c._implements) 
     222        """ Get all component classes that implement a particular interface. 
     223            Does not instantiate the component. """ 
     224        return [c for c in ComponentMeta._components 
     225                if hasattr(c, '_implements') and interface in c._implements] 
     226 
     227    def get_components(self): 
     228        """ Get all components. """ 
     229        return ComponentMeta._components[:] 
     230 
     231    def get_interfaces(self): 
     232        """ Get all available interfaces. """ 
     233        interfaces = [] 
     234        for c in self.get_components(): 
     235            if hasattr(c, '_implements'): 
     236                for i in c._implements: 
     237                    if i not in interfaces: 
     238                        interfaces.append(i) 
     239        return interfaces 
     240 
     241    def get_provided_interfaces(self, component): 
     242        """ Get interfaces that a component provides. """ 
     243        if hasattr(component, '_implements'): 
     244            return component._implements[:] 
     245        return [] 
    227246 
    228247    def get_component_definition(self, name, interface = None): 
    229248        """ Get a component class by name. Does not instantiate the component. """ 
    230249        if interface is not None: 
    231             try: 
    232                 return get_implementors(interface)[name] 
    233             except KeyError: 
    234                 raise ComponentError, 'No component named %s implementing %s.' % (name, interface.__name__) 
     250            implementors = self.get_implementors(interface) 
     251            for i in implementors: 
     252                if i.__name__ == name: 
     253                    return i 
     254            raise ComponentError, 'No component named %s implementing %s.' % (name, interface.__name__) 
    235255        for c in ComponentMeta._components: 
    236256            if c.__name__ == name: 
  • pycrash/trunk/crash/loader.py

    r286 r287  
    88            raise KeyError 
    99    except KeyError: 
    10         # The last [''] is very important! 
    11         a_mod = __import__(module_path, globals(), locals(), ['']) 
     10        a_mod = __import__(module_path) 
    1211        sys.modules[module_path] = a_mod 
    1312    return a_mod 
     
    5150def load_all(module): 
    5251    """ Load all python modules in a module directory. Returns a tuple of 
    53         (success, failed) modules. """ 
     52        (success, failed) modules. Failed is a list of (module, exception, 
     53        traceback) tuples. """ 
     54    from crash.util import traceback_string 
    5455    from dircache import listdir 
    5556    import os 
     
    6970                    succeeded.append(m) 
    7071                except Exception, e: 
    71                     failed.append((plugin, e)) 
     72                    failed.append((plugin, e, traceback_string(e))) 
    7273    return (succeeded, failed) 
  • pycrash/trunk/crash/util.py

    r283 r287  
    532532    """ Replace all instances of old in seq with new """ 
    533533    return [x == old and new or old for x in seq] 
     534 
     535def dict_diff(old, new): 
     536    """ Return tuple of (added, modified, deleted) keys between two 
     537        dictionaries. """ 
     538    try: 
     539        set = set 
     540    except: 
     541        from sets import Set as set 
     542    all_keys = set(old.keys()).union(new.keys()) 
     543    added = []  
     544    modified = [] 
     545    deleted = [] 
     546    for key in all_keys: 
     547        if key in new: 
     548            if key in old: 
     549                if new[key] != old[key]: 
     550                    modified.append(key) 
     551            else: 
     552                added.append(key) 
     553        else: 
     554            deleted.append(key) 
     555    return added, modified, deleted 
     556