[Zope3-checkins] SVN: Zope3/trunk/src/zope/importtool/ - add a different cycling import checker that simply lists detected cycles

Fred L. Drake, Jr. fred at zope.com
Thu Jun 3 10:05:08 EDT 2004


Log message for revision 25227:
- add a different cycling import checker that simply lists detected cycles
  at the end; use that for --cyclic-imports
- use the report formatting helper for the FirstImportReporter



-=-
Modified: Zope3/trunk/src/zope/importtool/app.py
===================================================================
--- Zope3/trunk/src/zope/importtool/app.py	2004-06-03 12:54:33 UTC (rev 25226)
+++ Zope3/trunk/src/zope/importtool/app.py	2004-06-03 14:05:08 UTC (rev 25227)
@@ -17,6 +17,7 @@
 import os
 import sys
 
+from zope.importtool import format
 from zope.importtool import hook
 from zope.importtool import reporter
 
@@ -74,7 +75,7 @@
         if self.reporter is not None:
             raise SystemExit(2)
         from zope.importtool import cycle
-        self.reporter = cycle.CycleReporter()
+        self.reporter = cycle.CycleCollector()
 
 
 class FirstImportReporter(reporter.Reporter):
@@ -96,11 +97,4 @@
             print "---------------------"
             print "No imports to report."
         else:
-            left_width = right_width = 0
-            for i, imported, importer in L:
-                left_width = max(left_width, len(imported))
-                right_width = max(right_width, len(importer))
-            width = left_width + 1 + right_width
-            print width * "-"
-            for i, imported, importer in L:
-                print imported.ljust(left_width), importer
+            format.two_column_report([entry[1:] for entry in L])

Modified: Zope3/trunk/src/zope/importtool/cycle.py
===================================================================
--- Zope3/trunk/src/zope/importtool/cycle.py	2004-06-03 12:54:33 UTC (rev 25226)
+++ Zope3/trunk/src/zope/importtool/cycle.py	2004-06-03 14:05:08 UTC (rev 25227)
@@ -17,6 +17,7 @@
 
 $Id$
 """
+from zope.importtool import format
 from zope.importtool import reporter
 
 
@@ -78,3 +79,24 @@
 
     def display_report(self):
         pass
+
+
+class CycleCollector(CycleReporter):
+
+    def __init__(self):
+        CycleReporter.__init__(self)
+        self.cycles = {}
+
+    def report_cycle(self, stack):
+        t = tuple(stack)
+        self.cycles[t] = self.cycles.get(t, 0) + 1
+
+    def display_report(self):
+        if self.cycles:
+            cycles = self.cycles.keys()
+            items = [(cycle[-1], ", ".join(cycle[:-1])) for cycle in cycles]
+            items.sort()
+            format.two_column_report(items)
+        else:
+            print "--------------------"
+            print "No cycles to report."




More information about the Zope3-Checkins mailing list