From: W. Trevor King Date: Tue, 27 Jul 2010 16:14:14 +0000 (-0400) Subject: Break out hooke.util.pluggable.submods. X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=1ce8a5ded9cf646bf63daecf4aa725f0d3f46197;p=hooke.git Break out hooke.util.pluggable.submods. --- diff --git a/hooke/util/pluggable.py b/hooke/util/pluggable.py index 14eaeb0..f13b9a5 100644 --- a/hooke/util/pluggable.py +++ b/hooke/util/pluggable.py @@ -60,6 +60,19 @@ class IsSubclass (object): return subclass +def submods(this_modname, submodnames): + """Iterate through (submodname, submod) pairs. + """ + for submodname in submodnames: + count = len([s for s in submodnames if s == submodname]) + assert count > 0, 'No %s entries: %s' % (submodname, submodnames) + assert count == 1, 'Multiple (%d) %s entries: %s' \ + % (count, submodname, submodnames) + this_mod = __import__(this_modname, fromlist=[submodname]) + submod = getattr(this_mod, submodname) + yield (submodname, submod) + + def construct_odict(this_modname, submodnames, class_selector, instantiate=True): """Search the submodules `submodnames` of a module `this_modname` @@ -70,13 +83,7 @@ def construct_odict(this_modname, submodnames, class_selector, itself is stored. """ objs = odict() - for submodname in submodnames: - count = len([s for s in submodnames if s == submodname]) - assert count > 0, 'No %s entries: %s' % (submodname, submodnames) - assert count == 1, 'Multiple (%d) %s entries: %s' \ - % (count, submodname, submodnames) - this_mod = __import__(this_modname, fromlist=[submodname]) - submod = getattr(this_mod, submodname) + for submodname,submod in submods(this_modname, submodnames): for objname in dir(submod): obj = getattr(submod, objname) if class_selector(obj): @@ -101,13 +108,7 @@ def construct_graph(this_modname, submodnames, class_selector, is returned. """ instances = {} - for submodname in submodnames: - count = len([s for s in submodnames if s == submodname]) - assert count > 0, 'No %s entries: %s' % (submodname, submodnames) - assert count == 1, 'Multiple (%d) %s entries: %s' \ - % (count, submodname, submodnames) - this_mod = __import__(this_modname, fromlist=[submodname]) - submod = getattr(this_mod, submodname) + for submodname,submod in submods(this_modname, submodnames): for objname in dir(submod): obj = getattr(submod, objname) if class_selector(obj):