Skip to content

Commit 945670f

Browse files
committed
Directory support
1 parent d9761db commit 945670f

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

pluginloader/loader.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,14 @@ def load_file(self, filename, onlyif=None):
2424
if (self._apply_condition(onlyif, name, clazz)):
2525
self.plugins[name] = PluginFactory(clazz)
2626

27-
def load_directory(self, path):
27+
def load_directory(self, path, onlyif=None, recursive=False):
2828
for filename in os.listdir(path):
2929
full_path = os.path.join(path, filename)
30-
self.load_file(full_path)
30+
if os.path.isfile(full_path):
31+
self.load_file(full_path, onlyif)
32+
elif os.path.isdir(full_path):
33+
if recursive:
34+
self.load_directory(full_path, onlyif, recursive)
3135

3236
def _apply_condition(self, condition, *args, **kwargs):
3337
if callable(condition):

tests/integration/test_directory_loader.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,16 @@ def setUp(self):
1313
def tearDown(self):
1414
shutil.rmtree(self.plugin_dir)
1515

16-
def _create(self, filename, content):
17-
with file(os.path.join(self.plugin_dir, filename), 'w+') as fd:
16+
def _create(self, filename, content, path=None):
17+
path = (self.plugin_dir
18+
if path is None
19+
else os.path.join(self.plugin_dir, path))
20+
with file(os.path.join(path, filename), 'w+') as fd:
1821
fd.write(content)
1922

23+
def _create_dir(self, path):
24+
os.makedirs(os.path.join(self.plugin_dir, path))
25+
2026
def test_empty_directory(self):
2127
sut = PluginLoader()
2228

@@ -31,3 +37,28 @@ def test_load_simple_file(self):
3137
sut.load_directory(self.plugin_dir)
3238

3339
self.assertEqual(['Foo'], sut.plugins.keys())
40+
41+
def test_ignorable_classes(self):
42+
self._create('foo.py', 'class Foo(object): pass')
43+
sut = PluginLoader()
44+
45+
sut.load_directory(self.plugin_dir, onlyif=lambda x, y: False)
46+
47+
self.assertEquals({}, sut.plugins)
48+
49+
def test_containing_directories(self):
50+
self._create_dir('foo')
51+
sut = PluginLoader()
52+
53+
sut.load_directory(self.plugin_dir)
54+
55+
self.assertEquals({}, sut.plugins)
56+
57+
def test_recursive_mode(self):
58+
self._create_dir('foo')
59+
self._create('bar.py', 'class Bazz(object): pass', 'foo')
60+
sut = PluginLoader()
61+
62+
sut.load_directory(self.plugin_dir, recursive=True)
63+
64+
self.assertEquals(['Bazz'], sut.plugins.keys())

0 commit comments

Comments
 (0)