1 # Copyright (C) 2010 W. Trevor King <wking@drexel.edu>
3 # This file is part of Hooke.
5 # Hooke is free software: you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation, either
8 # version 3 of the License, or (at your option) any later version.
10 # Hooke is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU Lesser General Public License for more details.
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with Hooke. If not, see
17 # <http://www.gnu.org/licenses/>.
22 from ..compat.odict import odict
23 from .graph import Node, Graph
26 class IsSubclass (object):
27 """A safe subclass comparator.
37 >>> is_subclass = IsSubclass(A)
40 >>> is_subclass = IsSubclass(A, blacklist=[A])
48 def __init__(self, base_class, blacklist=None):
49 self.base_class = base_class
52 self.blacklist = blacklist
53 def __call__(self, other):
55 subclass = issubclass(other, self.base_class)
58 if other in self.blacklist:
63 def construct_odict(this_modname, submodnames, class_selector,
65 """Search the submodules `submodnames` of a module `this_modname`
66 for class objects for which `class_selector(class)` returns
67 `True`. If `instantiate == True` these classes are instantiated
68 and stored in the returned :class:`hooke.compat.odict.odict` in
69 the order in which they were discovered. Otherwise, the class
73 for submodname in submodnames:
74 count = len([s for s in submodnames if s == submodname])
75 assert count > 0, 'No %s entries: %s' % (submodname, submodnames)
76 assert count == 1, 'Multiple (%d) %s entries: %s' \
77 % (count, submodname, submodnames)
78 this_mod = __import__(this_modname, fromlist=[submodname])
79 submod = getattr(this_mod, submodname)
80 for objname in dir(submod):
81 obj = getattr(submod, objname)
82 if class_selector(obj):
83 if instantiate == True:
85 name = getattr(obj, 'name', submodname)
90 def construct_graph(this_modname, submodnames, class_selector,
91 assert_name_match=True):
92 """Search the submodules `submodnames` of a module `this_modname`
93 for class objects for which `class_selector(class)` returns
94 `True`. These classes are instantiated, and the `instance.name`
95 is compared to the `submodname` (if `assert_name_match` is
98 The instances are further arranged into a dependency
99 :class:`hooke.util.graph.Graph` according to their
100 `instance.dependencies()` values. The topologically sorted graph
104 for submodname in submodnames:
105 count = len([s for s in submodnames if s == submodname])
106 assert count > 0, 'No %s entries: %s' % (submodname, submodnames)
107 assert count == 1, 'Multiple (%d) %s entries: %s' \
108 % (count, submodname, submodnames)
109 this_mod = __import__(this_modname, fromlist=[submodname])
110 submod = getattr(this_mod, submodname)
111 for objname in dir(submod):
112 obj = getattr(submod, objname)
113 if class_selector(obj):
115 if assert_name_match == True and instance.name != submodname:
117 'Instance name %s does not match module name %s'
118 % (instance.name, submodname))
119 instances[instance.name] = instance
121 for i in instances.values(): # make nodes for each instance
122 nodes[i.name] = Node(data=i)
123 for n in nodes.values(): # fill in dependencies for each node
124 n.extend([nodes[name] for name in n.data.dependencies()])
125 graph = Graph(nodes.values())
126 graph.topological_sort()