yield name.replace("_", "-"), module
def get_command(command_name):
+ """Retrieves the module for a user command
+
+ >>> get_command("asdf")
+ Traceback (most recent call last):
+ File "<stdin>", line 1, in ?
+ File "/home/abentley/be/libbe/cmdutil.py", line 60, in get_command
+ raise UserError("Unknown command %s" % command_name)
+ UserError: Unknown command asdf
+ >>> get_command("list")
+ <module 'becommands.list' from '/home/abentley/be/becommands/list.pyc'>
+ """
cmd = plugin.get_plugin("becommands", command_name.replace("-", "_"))
if cmd is None:
raise UserError("Unknown command %s" % command_name)
def help(cmd, args):
return get_command(cmd).help()
+
+def _test():
+ import doctest
+ import sys
+ doctest.testmod()
+
+if __name__ == "__main__":
+ _test()
return module
def iter_plugins(prefix):
- modfiles = os.listdir(os.path.join(sys.path[0], prefix))
+ """
+ >>> "list" in [n for n,m in iter_plugins("becommands")]
+ True
+ >>> "plugin" in [n for n,m in iter_plugins("libbe")]
+ True
+ """
+ modfiles = os.listdir(os.path.join(plugin_path, prefix))
modfiles.sort()
for modfile in modfiles:
if modfile.endswith(".py") and modfile != "__init__.py":
def get_plugin(prefix, name):
+ """
+ >>> get_plugin("becommands", "asdf") is None
+ True
+ >>> get_plugin("becommands", "list")
+ <module 'becommands.list' from '/home/abentley/be/becommands/list.pyc'>
+ """
dirprefix = '/'.join(prefix.split('.'))
- command_path = os.path.join(sys.path[0], dirprefix, name+".py")
+ command_path = os.path.join(plugin_path, dirprefix, name+".py")
if os.path.isfile(command_path):
return my_import(prefix + "." + name)
return None
-
+
+plugin_path = sys.path[0]
+while not os.path.isfile(os.path.join(plugin_path, "libbe/plugin.py")):
+ plugin_path = os.path.realpath(os.path.dirname(plugin_path))
+if plugin_path not in sys.path:
+ sys.path.append(plugin_path)
+def _test():
+ import doctest
+ doctest.testmod()
+
+if __name__ == "__main__":
+ _test()
--- /dev/null
+from libbe import plugin
+import doctest
+import sys
+if len(sys.argv) > 1:
+ match = False
+ mod = plugin.get_plugin("libbe", sys.argv[1])
+ if mod is not None:
+ doctest.testmod(mod)
+ match = True
+ mod = plugin.get_plugin("becommands", sys.argv[1])
+ if mod is not None:
+ doctest.testmod(mod)
+ match = True
+ if not match:
+ print "No modules match \"%s\"" % sys.argv[1]
+else:
+ for module in plugin.iter_plugins("libbe"):
+ doctest.testmod(module[1])
+ for module in plugin.iter_plugins("becommands"):
+ doctest.testmod(module[1])