Parcourir la source

[trac810] Plugin loader

Michal 'vorner' Vaner il y a 14 ans
Parent
commit
e58e69f00b

+ 22 - 1
src/bin/cfgmgr/b10-cfgmgr.py.in

@@ -23,6 +23,8 @@ import isc.util.process
 import signal
 import os
 from optparse import OptionParser
+import glob
+import os.path
 
 isc.util.process.rename()
 
@@ -66,7 +68,26 @@ def signal_handler(signal, frame):
         cm.running = False
 
 def load_plugins(path, cm):
-    pass
+    """Load all python files in the given path and treat them as plugins."""
+    # Find the python files
+    plugins = glob.glob(path + os.sep + '*.py')
+    # Search this directory first, but leave the others there for the imports
+    # of the modules
+    sys.path.insert(0, path)
+    try:
+        for plugin in plugins:
+            # Generate the name of the plugin
+            filename = os.path.basename(plugin)
+            name = filename[:-3]
+            # Load it
+            module = __import__(name)
+            # Ask it to provide the spec and checking function
+            (spec, check_func) = module.load()
+            # And insert it into the manager
+            cm.set_virtual_module(spec, check_func)
+    finally:
+        # Restore the search path
+        sys.path = sys.path[1:]
 
 def main():
     options = parse_options()

+ 1 - 1
src/bin/cfgmgr/tests/testdata/plugins/testplugin.py

@@ -23,7 +23,7 @@ class MockSpec:
     def get_module_name(self):
         return "mock_config_plugin"
 
-def mock_check_config(config)
+def mock_check_config(config):
     """Mock function to check config. Does nothing, only returns
        an "error" string to indicate it's this one."""
     return "Mock config plugin"