From e0b30d4f77d56db96fc7fdba75bc5d1a55326260 Mon Sep 17 00:00:00 2001 From: Edward George Date: Sun, 3 Feb 2013 19:42:50 +0000 Subject: [PATCH] hubs: EVENTLET_HUB can point to external modules --- eventlet/hubs/__init__.py | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/eventlet/hubs/__init__.py b/eventlet/hubs/__init__.py index 261831c..81d76b4 100644 --- a/eventlet/hubs/__init__.py +++ b/eventlet/hubs/__init__.py @@ -3,6 +3,14 @@ import os from eventlet.support import greenlets as greenlet from eventlet import patcher +try: + # try and import pkg_resources ... + import pkg_resources +except ImportError: + # ... but do not depend on it + pkg_resources = None + + __all__ = ["use_hub", "get_hub", "get_default_hub", "trampoline"] threading = patcher.original('threading') @@ -49,7 +57,10 @@ def use_hub(mod=None): event hub. Usually not required; the default hub is usually fine. Mod can be an actual module, a string, or None. If *mod* is a module, - it uses it directly. If *mod* is a string, use_hub tries to import + it uses it directly. If *mod* is a string and contains either '.' or ':' + use_hub tries to import the hub using the 'package.subpackage.module:Class' + convention, otherwise use_hub looks for a matching setuptools entry point + in the 'eventlet.hubs' group to load or finally tries to import `eventlet.hubs.mod` and use that as the hub module. If *mod* is None, use_hub uses the default hub. Only call use_hub during application initialization, because it resets the hub's state and any existing @@ -63,7 +74,20 @@ def use_hub(mod=None): del _threadlocal.hub if isinstance(mod, str): assert mod.strip(), "Need to specify a hub" - mod = __import__('eventlet.hubs.' + mod, globals(), locals(), ['Hub']) + if '.' in mod or ':' in mod: + modulename, _, classname = mod.strip().partition(':') + mod = __import__(modulename, globals(), locals(), [classname]) + if classname: + mod = getattr(mod, classname) + else: + found = False + if pkg_resources is not None: + for entry in pkg_resources.iter_entry_points( + group='eventlet.hubs', name=mod): + mod, found = entry.load(), True + break + if not found: + mod = __import__('eventlet.hubs.' + mod, globals(), locals(), ['Hub']) if hasattr(mod, 'Hub'): _threadlocal.Hub = mod.Hub else: