[Checkins] SVN: martian/branches/jw-philipp-using-ndir-directives/src/martian/util.py Extended the scan_for_classes helper to also accept an interface. It is now a generator.

Philipp von Weitershausen philikon at philikon.de
Sun May 4 07:02:32 EDT 2008


Log message for revision 86334:
  Extended the scan_for_classes helper to also accept an interface.  It is now a generator.

Changed:
  U   martian/branches/jw-philipp-using-ndir-directives/src/martian/util.py

-=-
Modified: martian/branches/jw-philipp-using-ndir-directives/src/martian/util.py
===================================================================
--- martian/branches/jw-philipp-using-ndir-directives/src/martian/util.py	2008-05-04 10:51:52 UTC (rev 86333)
+++ martian/branches/jw-philipp-using-ndir-directives/src/martian/util.py	2008-05-04 11:02:32 UTC (rev 86334)
@@ -104,21 +104,27 @@
                         "(use grok.provides to specify which one to use)."
                         % obj, obj)
 
-def scan_for_classes(module, classes):
+def scan_for_classes(module, classes=None, interface=None):
     """Given a module, scan for classes.
     """
-    result = set()
     for name in dir(module):
-        if name.startswith('__grok_'):
+        if '.' in name:
+            # This must be a module-level variable that couldn't have
+            # been set by the developer.  It must have been a
+            # module-level directive.
             continue
         obj = getattr(module, name)
-        if not defined_locally(obj, module.__name__):
+        if not defined_locally(obj, module.__name__) or not isclass(obj):
             continue
-        for class_ in classes:
-            if check_subclass(obj, class_):
-                result.add(obj)
-    return list(result)
 
+        if classes is not None:
+            for class_ in classes:
+                if check_subclass(obj, class_):
+                    yield obj
+
+        if interface is not None and interface.implementedBy(obj):
+            yield obj
+
 def methods_from_class(class_):
     # XXX Problem with zope.interface here that makes us special-case
     # __provides__.



More information about the Checkins mailing list