[PATCH 02/13] Make collect and part of UserInterface setup more generic

Martin Sivak msivak at redhat.com
Wed Aug 8 08:52:40 UTC 2012


---
 pyanaconda/ui/__init__.py     |   38 ++++++++++++++++++++++++++++++++++++++
 pyanaconda/ui/gui/__init__.py |   11 ++++++-----
 2 files changed, 44 insertions(+), 5 deletions(-)

diff --git a/pyanaconda/ui/__init__.py b/pyanaconda/ui/__init__.py
index 128f67c..4fafd49 100644
--- a/pyanaconda/ui/__init__.py
+++ b/pyanaconda/ui/__init__.py
@@ -19,6 +19,8 @@
 # Red Hat Author(s): Chris Lumens <clumens at redhat.com>
 #
 
+__all__ = ["UserInterface", "collect"]
+
 class UserInterface(object):
     """This is the base class for all kinds of install UIs.  It primarily
        defines what kinds of dialogs and entry widgets every interface must
@@ -92,3 +94,39 @@ class UserInterface(object):
            want to overwhelm the user with choices.
         """
         raise NotImplementedError
+
+    def getActionClasses(module_pattern, path, hubs, standalone_class):
+        standalones = collect(module_pattern, path, lambda obj: issubclass(obj, standalone_class) and \
+                              getattr(obj, "preForHub", False) or getattr(obj, "postForHub", False))
+
+        actionClasses = []
+        for hub in hubs:
+            actionClasses.extend(sorted(filter(lambda obj: getattr(obj, "preForHub", None) == hub, standalones),
+                                        key=lambda obj: obj.priority))
+            actionClasses.append(hub)
+            actionClasses.extend(sorted(filter(lambda obj: getattr(obj, "postForHub", None) == hub, standalones),
+                                        key=lambda obj: obj.priority))
+
+        return actionClasses
+
+def collect(module_pattern, path, pred):
+    """Traverse the directory (given by path) and find all classes that match
+       the given predicate.  This is then returned as a list of classes.
+
+       It is suggested you use collect_categories or collect_spokes instead of
+       this lower-level method.
+    """
+    retval = []
+    for module_file in os.listdir(path):
+        if not module_file.endswith(".py") or module_file == "__init__.py":
+            continue
+
+        mod_name = module_file[:-3]
+        module = importlib.import_module(module_pattern % mod_name))
+
+        p = lambda obj: inspect.isclass(obj) and pred(obj)
+
+        for (name, val) in inspect.getmembers(module, p):
+            retval.append(val)
+
+    return retval
diff --git a/pyanaconda/ui/gui/__init__.py b/pyanaconda/ui/gui/__init__.py
index 3d2fff9..8cdc168 100644
--- a/pyanaconda/ui/gui/__init__.py
+++ b/pyanaconda/ui/gui/__init__.py
@@ -53,8 +53,9 @@ class GraphicalUserInterface(UserInterface):
         self._hubs.extend([SummaryHub, ProgressHub])
 
         # First, grab a list of all the standalone spokes.
-        standalones = collect("spokes", lambda obj: issubclass(obj, StandaloneSpoke) and \
-                                                    getattr(obj, "preForHub", False) or getattr(obj, "postForHub", False))
+        path = os.path.join(os.path.dirname(__file__), "spokes")
+        standalones = collect("pyanaconda.ui.gui.spokes.%s", path, lambda obj: issubclass(obj, StandaloneSpoke) and \
+                              getattr(obj, "preForHub", False) or getattr(obj, "postForHub", False))
 
         actionClasses = []
         for hub in self._hubs:
@@ -396,7 +397,7 @@ class QuitDialog(UIObject):
         rc = self.window.run()
         return rc
 
-def collect(subpath, pred):
+def collect(module_pattern, path, pred):
     """Traverse the subdirectory (given by subpath) of this module's current
        directory and find all classes that math the given category.  This is
        then returned as a list of classes.  If category is None, this method
@@ -406,12 +407,12 @@ def collect(subpath, pred):
        this lower-level method.
     """
     retval = []
-    for module_file in os.listdir(os.path.dirname(__file__) + "/" + subpath):
+    for module_file in os.listdir( + "/" + subpath):
         if not module_file.endswith(".py") or module_file in [__file__, "__init__.py"]:
             continue
 
         mod_name = module_file[:-3]
-        module = importlib.import_module("pyanaconda.ui.gui.%s.%s" % (subpath, mod_name))
+        module = importlib.import_module(module_pattern % mod_name))
 
         p = lambda obj: inspect.isclass(obj) and pred(obj)
 
-- 
1.7.10.4



More information about the anaconda-patches mailing list