--- a/src/pyplugin.py Sat Mar 23 04:09:30 2013 -0500
+++ b/src/pyplugin.py Sat Mar 23 15:51:06 2013 -0500
@@ -21,44 +21,45 @@
# we use a list since it is a mutable sequence type. Meaning we can add to it
- Adds a path to search for plugins under
+ Adds a module to search for plugins under - global PATHS # pylint:disable-msg=W0602
+ global MODULES # pylint:disable-msg=W0602
+ if not module in MODULES:
+def remove_module(module): - Removes a plugin search path
+ Removes a module to search for plugins under - global PATHS # pylint:disable-msg=W0602
+ global MODULES # pylint:disable-msg=W0602
- Returns the list of all plugin paths
+ Returns the list of all module to search under
- Clears all search paths
+ Clears all search modules
def find(cls, recurse=False):
@@ -71,8 +72,8 @@
- mod = importlib.import_module(path)
+ mod = importlib.import_module(module) if recurse and _is_package(mod):
@@ -121,13 +122,14 @@
filename = os.path.join(dirname, name)
if os.path.isdir(filename):
- add_path(make_module(base))
+ add_module(make_module(base)) elif os.path.isfile(filename):
if fnmatch.fnmatch(name, '*.py') or \
fnmatch.fnmatch(name, '*.py[co]'):
- add_path(make_module(base))
+ add_module(make_module(base)) -__all__ = [add_path, remove_path, get_paths, find]
+__all__ = [add_module, remove_module, get_modules, clear_modules, find] --- a/tests/tests.py Sat Mar 23 04:09:30 2013 -0500
+++ b/tests/tests.py Sat Mar 23 15:51:06 2013 -0500
@@ -23,15 +23,13 @@
-def test_path(test, path, count, recurse=False):
+def test_module(test, module, count, recurse=False): - Given a module path, test that we find count plugins
+ Given a module, test that we find count plugins - pyplugin.add_path(path)
+ pyplugin.add_module(module) gen = pyplugin.find(Plugin, recurse=recurse)
test.assertTrue(inspect.isgenerator(gen))
@@ -40,13 +38,13 @@
test.assertEqual(len(found), count, 'plugins found: {}'.format(found))
-def test_path_count(test, count):
+def test_module_count(test, count): Asserts if the number of paths doesn't match count
- paths = pyplugin.get_paths()
- test.assertEqual(len(paths), count, 'search paths {}'.format(paths))
+ modules = pyplugin.get_modules() + test.assertEqual(len(modules), count, 'search paths {}'.format(modules)) class Plugin(object): # pylint:disable-msg=R0903
@@ -57,18 +55,21 @@
-class TestPaths(unittest.TestCase): # pylint:disable-msg=R0904
+class TestModules(unittest.TestCase): # pylint:disable-msg=R0904 Unit tests for path manipulation
def setUp(self): # pylint:disable-msg=C0103
+ pyplugin.clear_modules() def tearDown(self): # pylint:disable-msg=C0103
- test_path_count(self, 0)
+ test_module_count(self, 0) + pyplugin.clear_modules() def test_add_remove(self):
@@ -76,9 +77,9 @@
Add and remove a single plugin path
- pyplugin.add_path(PATH)
- test_path_count(self, 1)
- pyplugin.remove_path(PATH)
+ pyplugin.add_module(TestModules.module) + test_module_count(self, 1) + pyplugin.remove_module(TestModules.module) def test_add_existing(self):
@@ -86,11 +87,11 @@
Add the same plugin path twice, and make sure it's only in there once
- pyplugin.add_path(PATH)
- pyplugin.add_path(PATH)
- test_path_count(self, 1)
+ pyplugin.add_module(TestModules.module) + pyplugin.add_module(TestModules.module) + test_module_count(self, 1) - pyplugin.remove_path(PATH)
+ pyplugin.remove_module(TestModules.module) def test_remove_nonexistant(self): # pylint:disable-msg=R0201
@@ -98,7 +99,7 @@
Try to remove a non-existant plugin path
- pyplugin.remove_path(PATH)
+ pyplugin.remove_module(TestModules.module) def test_add_multiple(self):
@@ -106,16 +107,16 @@
Add multiple paths to the search paths
- second = '{}.1'.format(PATH)
+ second = '{}.1'.format(TestModules.module) - pyplugin.add_path(PATH)
- test_path_count(self, 1)
+ pyplugin.add_module(TestModules.module) + test_module_count(self, 1) - pyplugin.add_path(second)
- test_path_count(self, 2)
+ pyplugin.add_module(second) + test_module_count(self, 2) - pyplugin.remove_path(PATH)
- pyplugin.remove_path(second)
+ pyplugin.remove_module(TestModules.module) + pyplugin.remove_module(second) class TestFind(unittest.TestCase): # pylint:disable-msg=R0904
@@ -177,49 +178,50 @@
""" Tests the discovery method of pyplugin """
def setUp(self): # pylint:disable-msg=C0103
+ pyplugin.clear_modules() def tearDown(self): # pylint:disable-msg=C0103
+ pyplugin.clear_modules() """ Test a module with a single plugin """
- test_path(self, 'modules.single', 1)
+ test_module(self, 'modules.single', 1) """ Test a module with multiple plugins """
- test_path(self, 'modules.multiple', 2)
+ test_module(self, 'modules.multiple', 2) """ Test a module with more than just plugins """
- test_path(self, 'modules.mixed', 2)
+ test_module(self, 'modules.mixed', 2) class TestPackage(unittest.TestCase): # pylint:disable-msg=R0904
""" Tests the find method of pyplugin on a package """
def setUp(self): # pylint:disable-msg=C0103
+ pyplugin.clear_modules() def tearDown(self): # pylint:disable-msg=C0103
+ pyplugin.clear_modules() def test_package_without_recurse(self):
""" Test a package without recursion """
- test_path(self, 'modules', 0)
+ test_module(self, 'modules', 0) def test_package_recursion(self):
""" Test a package with recursion """
- test_path(self, 'modules.deep', 2, recurse=True)
+ test_module(self, 'modules.deep', 2, recurse=True) def test_package_deep_recursion(self):
""" Test a package with recursion """
- test_path(self, 'modules', 7, recurse=True)
+ test_module(self, 'modules', 7, recurse=True)