Skip to content

Commit

Permalink
First version of the internal filtering capability
Browse files Browse the repository at this point in the history
The Manager is already capable of receiving a filter callback function
to filter the returned implementors list.

ref #11
  • Loading branch information
daltonmatos committed Jun 16, 2012
1 parent a0b59ec commit 8dc1de9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
5 changes: 3 additions & 2 deletions plugnplay/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ def add_implementor(self, interface, implementor_instance):
self.iface_implementors.setdefault(interface, [])
self.iface_implementors[interface].append(implementor_instance)

def implementors(self, interface):
return self.iface_implementors.get(interface, [])
def implementors(self, interface, filter_callback=None):
all_implementors = self.iface_implementors.get(interface, [])
return filter(filter_callback, all_implementors)
17 changes: 17 additions & 0 deletions tests/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,20 @@ def test_add_second_implementor_of_an_interface(self):
'''
def test_return_empty_list_of_implementors(self):
self.assertEquals(0, len(self.man.implementors(SomeInterface)))


class FilteredImplementorsTest(unittest.TestCase):

def setUp(self):
self.man = Manager()

def test_simple_call_back_no_parameters(self):
def _simple_callback_filter(implementor):
return implementor.__class__.__name__.endswith("A")
implementor_a = PlugA()
self.man.add_implementor(SomeInterface, implementor_a)
self.man.add_implementor(SomeInterface, PlugB())
assert 2 == len(self.man.implementors(SomeInterface))
filtered_implementors = self.man.implementors(SomeInterface, _simple_callback_filter)
assert 1 == len(filtered_implementors)
assert [implementor_a] == filtered_implementors

0 comments on commit 8dc1de9

Please sign in to comment.