Skip to content

Commit 583714b

Browse files
committed
Adding class validator
1 parent 8b6aabf commit 583714b

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

pluginloader/loader.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,17 @@ class PluginLoader(object):
66
def __init__(self):
77
self.plugins = {}
88

9-
def load_file(self, filename):
9+
def load_file(self, filename, onlyif=True):
1010
context = {}
1111
with open(filename) as fd:
1212
exec(fd.read(), context)
1313

1414
for name, clazz in context.iteritems():
15-
if isinstance(clazz, type):
15+
if (isinstance(clazz, type)
16+
and self._apply_condition(onlyif, name, clazz)):
1617
self.plugins[name] = clazz()
18+
19+
def _apply_condition(self, condition, *args, **kwargs):
20+
if callable(condition):
21+
return condition(*args, **kwargs)
22+
return condition

tests/acceptance/test_loader.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ def test_base_case(self):
3131
self.assertIsInstance(sut.plugins['Foo'], object)
3232
self.assertEquals('Foo', sut.plugins['Foo'].__class__.__name__)
3333

34+
def test_ignorable_classes(self):
35+
self.plugin_file.write('class Foo(object): pass')
36+
self.plugin_file.flush()
37+
sut = PluginLoader()
38+
39+
sut.load_file(self.plugin_file.name, onlyif=lambda x, y: False)
40+
41+
self.assertEquals({}, sut.plugins)
42+
43+
3444

3545
@unittest.skip('Not ready yet')
3646
class plugins_in_directory(unittest.TestCase):

0 commit comments

Comments
 (0)