Changeset 11285:c0fc66213bfd in orange


Ignore:
Timestamp:
01/25/13 17:42:19 (15 months ago)
Author:
Ales Erjavec <ales.erjavec@…>
Branch:
default
Message:

Changed discovery to run from arbitrary entry points iterator/group.

Location:
Orange/OrangeCanvas
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • Orange/OrangeCanvas/config.py

    r11250 r11285  
    77import logging 
    88import cPickle as pickle 
    9  
    10 log = logging.getLogger(__name__) 
     9import itertools 
     10 
     11import pkg_resources 
    1112 
    1213from PyQt4.QtGui import QDesktopServices 
     
    1415 
    1516from .utils.settings import Settings, config_slot 
     17 
     18log = logging.getLogger(__name__) 
    1619 
    1720 
     
    2124 
    2225    .. note:: This should not be run before QApplication has been initialized. 
    23               Otherwise it can brake Qt's plugin search paths. 
     26              Otherwise it can break Qt's plugin search paths. 
    2427 
    2528    """ 
     
    191194        with open(recent_filename, "wb") as f: 
    192195            pickle.dump(scheme_list, f) 
     196 
     197 
     198WIDGETS_ENTRY = "orange.widgets" 
     199 
     200 
     201# This could also be achieved by declaring the entry point in 
     202# Orange's setup.py, but that would not guaranty this entry point 
     203# is the first in a list. 
     204 
     205def default_entry_point(): 
     206    """ 
     207    Return a default orange.widgets entry point for loading 
     208    default Orange Widgets. 
     209 
     210    """ 
     211    dist = pkg_resources.get_distribution("Orange") 
     212    ep = pkg_resources.EntryPoint("Orange Widgets", "Orange.OrangeWidgets", 
     213                                  dist=dist) 
     214    return ep 
     215 
     216 
     217def widgets_entry_points(): 
     218    """ 
     219    Return an `EntryPoint` iterator for all 'orange.widget' entry 
     220    points plus the default Orange Widgets. 
     221 
     222    """ 
     223    ep_iter = pkg_resources.iter_entry_points(WIDGETS_ENTRY) 
     224    chain = [[default_entry_point()], 
     225             ep_iter 
     226             ] 
     227    return itertools.chain(*chain) 
  • Orange/OrangeCanvas/main.py

    r11258 r11285  
    3333 
    3434log = logging.getLogger(__name__) 
    35  
    36  
    37 def qt_logging_handle(msg_type, message): 
    38     print msg_type, message 
    3935 
    4036 
     
    203199        widget_registry = qt.QtWidgetRegistry(widget_registry) 
    204200    else: 
    205         widget_discovery.run() 
     201        widget_discovery.run(config.widgets_entry_points()) 
    206202        # Store cached descriptions 
    207203        cache.save_registry_cache(widget_discovery.cached_descriptions) 
  • Orange/OrangeCanvas/registry/__init__.py

    r11098 r11285  
    4040 
    4141from .base import WidgetRegistry, VERSION_HEX 
     42from . import discovery 
    4243 
    4344log = logging.getLogger(__name__) 
    4445 
    4546 
    46 __GLOBAL_REGISTRY = None 
     47__GLOBAL_REGISTRY = {} 
    4748 
    4849 
    49 def global_registry(): 
    50     """Return a global WidgetRegistry instance. 
     50def global_registry(entry_point_group="_default"): 
     51    """ 
     52    Return a global WidgetRegistry instance for the entry point group. 
     53    If none exists then it will be created. 
     54 
     55    .. note:: This will be deprecated when a proper replacement for it's 
     56              uses can be found. 
     57 
    5158    """ 
    5259    global __GLOBAL_REGISTRY 
    5360    # TODO: lock 
    54     if __GLOBAL_REGISTRY is None: 
     61    if __GLOBAL_REGISTRY.get(entry_point_group) is None: 
    5562        log.debug("'global_registry()' - running widget discovery.") 
    56         from . import discovery 
     63        if entry_point_group == "_default": 
     64            from ..config import widgets_entry_points 
     65            entry_points_iter = widgets_entry_points() 
     66        else: 
     67            entry_points_iter = entry_point_group 
    5768        reg = WidgetRegistry() 
    5869        disc = discovery.WidgetDiscovery(reg) 
    59         disc.run() 
     70        disc.run(entry_points_iter) 
    6071        log.info("'global_registry()' discovery finished.") 
    61         __GLOBAL_REGISTRY = reg 
     72        __GLOBAL_REGISTRY[entry_point_group] = reg 
    6273 
    63     return __GLOBAL_REGISTRY 
     74    return __GLOBAL_REGISTRY[entry_point_group] 
    6475 
    6576 
    66 def set_global_registry(registry): 
     77def set_global_registry(registry, entry_point_group="_default"): 
     78    """ 
     79    Set the global WidgetRegistry instance for the entry point group. 
     80 
     81    .. note:: Overrides previous registry. 
     82 
     83    """ 
    6784    global __GLOBAL_REGISTRY 
    6885    log.debug("'set_global_registry()' - setting registry.") 
    69     __GLOBAL_REGISTRY = registry 
     86    __GLOBAL_REGISTRY[entry_point_group] = registry 
  • Orange/OrangeCanvas/registry/discovery.py

    r11282 r11285  
    99import glob 
    1010import logging 
    11 import itertools 
    1211import types 
    1312import pkgutil 
     
    2120 
    2221from . import VERSION_HEX 
    23  
     22from . import cache, WidgetRegistry 
    2423 
    2524log = logging.getLogger(__name__) 
     
    5352 
    5453 
    55 WIDGETS_ENTRY = "orange.widgets" 
    56  
    57  
    58 # This could also be achieved by declaring the entry point in 
    59 # Orange's setup.py 
    60 def default_entry_point(): 
    61     """Return a default orange.widgets entry point for loading 
    62     default Orange Widgets. 
    63  
    64     """ 
    65     dist = pkg_resources.get_distribution("Orange") 
    66     ep = pkg_resources.EntryPoint("Orange Widgets", "Orange.OrangeWidgets", 
    67                                   dist=dist) 
    68     return ep 
    69  
    70  
    71 def widgets_entry_points(): 
    72     """Return an EntryPoint iterator for all 'orange.widget' entry 
    73     points including the default Orange Widgets. 
    74  
    75     """ 
    76     ep_iter = pkg_resources.iter_entry_points(WIDGETS_ENTRY) 
    77     chain = [[default_entry_point()], 
    78              ep_iter 
    79              ] 
    80     return itertools.chain(*chain) 
    81  
    82  
    8354class WidgetDiscovery(object): 
    84     """Base widget discovery runner. 
     55    """ 
     56    Base widget discovery runner. 
    8557    """ 
    8658 
     
    9365            self.cached_descriptions["!VERSION"] = version 
    9466 
    95     def run(self): 
    96         """Run the widget discovery process. 
    97         """ 
    98         for entry_point in widgets_entry_points(): 
     67    def run(self, entry_points_iter): 
     68        """ 
     69        Run the widget discovery process from an entry point iterator 
     70        (yielding :class:`pkg_resources.EntryPoint` instances). 
     71 
     72        """ 
     73        if isinstance(entry_points_iter, basestring): 
     74            entry_points_iter = \ 
     75                pkg_resources.iter_entry_points(entry_points_iter) 
     76 
     77        for entry_point in entry_points_iter: 
    9978            try: 
    10079                point = entry_point.load() 
    10180            except pkg_resources.DistributionNotFound: 
    102                 log.error("Could not load %r (unsatisfied dependencies).", 
    103                           entry_point.name, exc_info=True) 
     81                log.error("Could not load '%s' (unsatisfied dependencies).", 
     82                          entry_point, exc_info=True) 
    10483                continue 
    10584            except ImportError: 
    106                 log.error("An ImportError occurred while loading an " 
    107                           "entry point", exc_info=True) 
     85                log.error("An ImportError occurred while loading " 
     86                          "entry point '%s'", entry_point, exc_info=True) 
    10887                continue 
    10988            except Exception: 
    110                 log.error("An exception occurred while loading an " 
    111                           "entry point", exc_info=True) 
     89                log.error("An exception occurred while loading " 
     90                          "entry point '%s'", entry_point, exc_info=True) 
    11291                continue 
    11392 
     
    557536 
    558537 
    559 def run_discovery(cached=False): 
    560     """Run the default widget discovery and return the WidgetRegisty 
     538def run_discovery(entry_point, cached=False): 
     539    """ 
     540    Run the default widget discovery and return a :class:`WidgetRegistry` 
    561541    instance. 
    562542 
    563543    """ 
    564     from . import cache, WidgetRegistry 
    565544    reg_cache = {} 
    566545    if cached: 
  • Orange/OrangeCanvas/registry/qt.py

    r11252 r11285  
    2121from ..resources import icon_loader 
    2222 
    23 from . import NAMED_COLORS 
     23from . import cache, NAMED_COLORS 
    2424 
    2525 
     
    4242        WidgetDiscovery.__init__(self, registry, cached_descriptions) 
    4343 
    44     def run(self): 
     44    def run(self, entry_points_iter): 
    4545        self.discovery_start.emit() 
    46         WidgetDiscovery.run(self) 
     46        WidgetDiscovery.run(self, entry_points_iter) 
    4747        self.discovery_finished.emit() 
    4848 
     
    339339 
    340340 
    341 def run_discovery(cached=False): 
    342     """Run the default discovery and return an instance 
    343     of :class:`QtWidgetRegistry`. 
    344  
    345     """ 
    346     from . import cache 
     341def run_discovery(entry_points_iter, cached=False): 
     342    """ 
     343    Run the default discovery and return an instance of 
     344    :class:`QtWidgetRegistry`. 
     345 
     346    """ 
    347347    reg_cache = {} 
    348348    if cached: 
  • Orange/OrangeCanvas/registry/tests/test_discovery.py

    r11284 r11285  
    8484    def test_run(self): 
    8585        disc = self.discovery_class() 
    86         disc.run() 
     86        disc.run("example.does.not.exist.but.it.does.not.matter.") 
Note: See TracChangeset for help on using the changeset viewer.