[node-patches] Change in ovirt-node[node-3.0]: app: Move plugin loader into separate module

fabiand at fedoraproject.org fabiand at fedoraproject.org
Thu Oct 10 09:50:33 UTC 2013


Fabian Deutsch has uploaded a new change for review.

Change subject: app: Move plugin loader into separate module
......................................................................

app: Move plugin loader into separate module

Change-Id: I67d5efbfa544a536ac67b4e05ad6fcee014a2514
Signed-off-by: Fabian Deutsch <fabiand at fedoraproject.org>
---
M src/Makefile.am
M src/ovirt/node/app.py
M src/ovirt/node/installer/core/__init__.py
A src/ovirt/node/loader.py
M src/ovirt/node/plugins.py
M src/ovirt/node/setup/core/__init__.py
6 files changed, 102 insertions(+), 56 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-node refs/changes/70/20070/1

diff --git a/src/Makefile.am b/src/Makefile.am
index 194de0c..af5fd68 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -79,7 +79,8 @@
   ovirt/node/plugins.py \
   ovirt/node/valid.py \
   ovirt/node/app.py \
-  ovirt/node/base.py
+  ovirt/node/base.py \
+  ovirt/node/loader.py
 
 pyovirt_node_config_PYTHON = \
   ovirt/node/config/__init__.py \
diff --git a/src/ovirt/node/app.py b/src/ovirt/node/app.py
index cfedfb5..6aa3a4a 100644
--- a/src/ovirt/node/app.py
+++ b/src/ovirt/node/app.py
@@ -20,7 +20,7 @@
 # also available at http://www.gnu.org/copyleft/gpl.html.
 from StringIO import StringIO
 from optparse import OptionParser
-from ovirt.node import base, utils, plugins, ui
+from ovirt.node import base, utils, plugins, ui, loader
 from ovirt.node.config import defaults
 from ovirt.node.ui import urwid_builder
 from ovirt.node.utils import system, Timer, console
@@ -366,10 +366,12 @@
         the createPlugins function is responsible for creating the plugin
         """
         self.__plugins = {}
-        for group in plugins.load_plugin_groups(self.plugin_base):
-            if hasattr(group, "createPlugins"):
+        groups = loader.plugin_groups_iterator(self.plugin_base,
+                                               "createPlugins")
+        for group, createPlugins in groups:
+            if createPlugins:
                 self.logger.debug("Package has plugins: %s" % group)
-                group.createPlugins(self)
+                createPlugins(self)
             else:
                 self.logger.debug("Package has no plugins: %s" % group)
 
diff --git a/src/ovirt/node/installer/core/__init__.py b/src/ovirt/node/installer/core/__init__.py
index ce1f224..b460fbd 100644
--- a/src/ovirt/node/installer/core/__init__.py
+++ b/src/ovirt/node/installer/core/__init__.py
@@ -22,7 +22,7 @@
 """
 Core Installer Plugins
 """
-from ovirt.node import plugins
+from ovirt.node import plugins, loader
 
 
 #
@@ -33,5 +33,5 @@
     application.ui.with_menu = False
 
     # Lazy load all plugins in this package
-    for plugin in plugins.get_modules_in_package(__package__):
+    for plugin in loader.get_modules_in_package(__package__):
         plugin.Plugin(application)
diff --git a/src/ovirt/node/loader.py b/src/ovirt/node/loader.py
new file mode 100644
index 0000000..79469e1
--- /dev/null
+++ b/src/ovirt/node/loader.py
@@ -0,0 +1,85 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+#
+# plugins.py - Copyright (C) 2013 Red Hat, Inc.
+# Written by Fabian Deutsch <fabiand at redhat.com>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; version 2 of the License.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+# MA  02110-1301, USA.  A copy of the GNU General Public License is
+# also available at http://www.gnu.org/copyleft/gpl.html.
+import logging
+import pkgutil
+
+"""
+This contains much stuff related to plugins
+"""
+
+
+logger = logging.getLogger(__name__)
+
+
+def plugin_groups_iterator(basepackage, attrname):
+    """High-level function to retrieve a specific function from a package
+    """
+    for group in load_plugin_groups(basepackage):
+        attr = None
+        if hasattr(group, attrname):
+            attr = getattr(group, attrname)
+        yield group, attr
+
+
+def load_plugin_groups(basepackage):
+    """Load all plugin groups (which can the contain plugins)
+
+    Args:
+        basepackage: The package where to look for packages
+    """
+    modules = []
+    logger.debug("Loading plugin-groups from package: %s" % basepackage)
+    for groupmodule in get_packages_in_package(basepackage):
+        logger.debug("Found plugin-group package: %s" % groupmodule)
+        modules.append(groupmodule)
+    logger.debug("Loading loading plugin-group modules")
+    return modules
+
+
+def get_packages_in_package(basepackage):
+    """Find, import and yield all packages below basepackage
+
+    Args:
+        basepackage: Where to look for other packages
+    Yields:
+        Yields all packages found below basepackage
+    """
+    for importer, modname, ispkg in pkgutil.iter_modules(basepackage.__path__):
+        if ispkg:
+            fullmodpath = basepackage.__name__ + "." + modname
+            yield importer.find_module(modname).load_module(fullmodpath)
+
+
+def get_modules_in_package(package, filter_cb=lambda n: True):
+    """Get and load all modules in a package
+
+    Args:
+        package: Where to look for modules
+        filter_cb: (Optional) callback to filter out modules to be loaded
+                   the module name is passed to the cb, True indicates to load
+                   the module.
+    """
+    if type(package) in [str, unicode]:
+        package = pkgutil.get_loader(package).load_module(package)
+    for importer, modname, ispkg in pkgutil.iter_modules(package.__path__):
+        if filter_cb(modname):
+            fullmodpath = package.__name__ + "." + modname
+            yield importer.find_module(modname).load_module(fullmodpath)
diff --git a/src/ovirt/node/plugins.py b/src/ovirt/node/plugins.py
index c94b28c..5d0de52 100644
--- a/src/ovirt/node/plugins.py
+++ b/src/ovirt/node/plugins.py
@@ -20,7 +20,6 @@
 # also available at http://www.gnu.org/copyleft/gpl.html.
 from ovirt.node import base, exceptions, ui
 import logging
-import pkgutil
 
 """
 This contains much stuff related to plugins
@@ -28,52 +27,6 @@
 
 
 logger = logging.getLogger(__name__)
-
-
-def load_plugin_groups(basepackage):
-    """Load all plugin groups (which can the contain plugins)
-
-    Args:
-        basepackage: The package where to look for packages
-    """
-    modules = []
-    logger.debug("Loading plugin-groups from package: %s" % basepackage)
-    for groupmodule in get_packages_in_package(basepackage):
-        logger.debug("Found plugin-group package: %s" % groupmodule)
-        modules.append(groupmodule)
-    logger.debug("Loading loading plugin-group modules")
-    return modules
-
-
-def get_packages_in_package(basepackage):
-    """Find, import and yield all packages below basepackage
-
-    Args:
-        basepackage: Where to look for other packages
-    Yields:
-        Yields all packages found below basepackage
-    """
-    for importer, modname, ispkg in pkgutil.iter_modules(basepackage.__path__):
-        if ispkg:
-            fullmodpath = basepackage.__name__ + "." + modname
-            yield importer.find_module(modname).load_module(fullmodpath)
-
-
-def get_modules_in_package(package, filter_cb=lambda n: True):
-    """Get and load all modules in a package
-
-    Args:
-        package: Where to look for modules
-        filter_cb: (Optional) callback to filter out modules to be loaded
-                   the module name is passed to the cb, True indicates to load
-                   the module.
-    """
-    if type(package) in [str, unicode]:
-        package = pkgutil.get_loader(package).load_module(package)
-    for importer, modname, ispkg in pkgutil.iter_modules(package.__path__):
-        if filter_cb(modname):
-            fullmodpath = package.__name__ + "." + modname
-            yield importer.find_module(modname).load_module(fullmodpath)
 
 
 class NodePlugin(base.Base):
diff --git a/src/ovirt/node/setup/core/__init__.py b/src/ovirt/node/setup/core/__init__.py
index a3be97f..cb3b024 100644
--- a/src/ovirt/node/setup/core/__init__.py
+++ b/src/ovirt/node/setup/core/__init__.py
@@ -22,13 +22,18 @@
 """
 Core Setup Plugins
 """
-from ovirt.node import plugins
+from ovirt.node import loader
 
 
 #
 # Magic function to register all plugins to be used
 #
+def all_modules():
+    for plugin in loader.get_modules_in_package(__package__):
+        yield plugin
+
+
 def createPlugins(application):
     # Lazy load all plugins in this package
-    for plugin in plugins.get_modules_in_package(__package__):
+    for plugin in all_modules():
         plugin.Plugin(application)


-- 
To view, visit http://gerrit.ovirt.org/20070
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I67d5efbfa544a536ac67b4e05ad6fcee014a2514
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-node
Gerrit-Branch: node-3.0
Gerrit-Owner: Fabian Deutsch <fabiand at fedoraproject.org>



More information about the node-patches mailing list