Skip to content

Commit b98f03a

Browse files
committed
Adding arguments for plugin consructor
1 parent 583714b commit b98f03a

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

pluginloader/loader.py

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

9-
def load_file(self, filename, onlyif=True):
9+
def load_file(self, filename, onlyif=True, args=None, kwargs=None):
10+
args = args or []
11+
kwargs = kwargs or {}
1012
context = {}
1113
with open(filename) as fd:
1214
exec(fd.read(), context)
1315

1416
for name, clazz in context.iteritems():
1517
if (isinstance(clazz, type)
1618
and self._apply_condition(onlyif, name, clazz)):
17-
self.plugins[name] = clazz()
19+
self.plugins[name] = clazz(*args, **kwargs)
1820

1921
def _apply_condition(self, condition, *args, **kwargs):
2022
if callable(condition):

tests/acceptance/test_loader.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,32 @@ def test_ignorable_classes(self):
4040

4141
self.assertEquals({}, sut.plugins)
4242

43+
def test_parameters_for_constructor(self):
44+
self.plugin_file.write(
45+
'class Foo(object):\n'
46+
' def __init__(self, a):\n'
47+
' self.a = a'
48+
)
49+
self.plugin_file.flush()
50+
sut = PluginLoader()
51+
52+
sut.load_file(self.plugin_file.name, args=[5])
53+
54+
self.assertEquals(5, sut.plugins['Foo'].a)
55+
56+
def test_namedparameters_for_constructor(self):
57+
self.plugin_file.write(
58+
'class Foo(object):\n'
59+
' def __init__(self, a):\n'
60+
' self.a = a'
61+
)
62+
self.plugin_file.flush()
63+
sut = PluginLoader()
64+
65+
sut.load_file(self.plugin_file.name, kwargs={'a': 5})
66+
67+
self.assertEquals(5, sut.plugins['Foo'].a)
68+
4369

4470

4571
@unittest.skip('Not ready yet')

0 commit comments

Comments
 (0)