]> thyrsus.com Git - comparator.git/commitdiff
Improved filterator -- KeyboardInterrupt now handled gracefully, and
authorEric S. Raymond <esr@thyrsus.com>
Thu, 18 Sep 2003 08:48:06 +0000 (08:48 +0000)
committerEric S. Raymond <esr@thyrsus.com>
Thu, 18 Sep 2003 08:48:06 +0000 (08:48 +0000)
statistics are correct.

comparator.py
filterator

index cea57f49f47092c3140fab8061a03d7e6a63780b..4c0dddac521b9716e9a354b199c631073c6c9f0f 100644 (file)
@@ -52,7 +52,7 @@ class CommonReport:
             self.trees[tree] = {}
             for assignment in properties.split(", "):
                 (property, value) = assignment.split("=")
-                self.trees[tree][property] = int(value)
+                self.trees[tree][property.strip()] = int(value)
         # Now read the common-segment stuff
         self.cliques = []
         self.files = {}
@@ -99,9 +99,7 @@ class CommonReport:
                 pass
         if self.dir:
             os.chdir(olddir)
-        if not text:
-            print >>sys.stderr, "Failed to extract text from", clique
-        return text
+        return (file, start, end, text)
 
     def filter_by_size(self, minsize):
         "Throw out all common segments below a specified size."
@@ -111,6 +109,10 @@ class CommonReport:
                 if end - start + 1 >= minsize:
                     filtered.append(clique)
                     break
+            else:
+                properties = self.trees[file.split("/")[0]]
+                properties['matches'] -= 1
+                properties['matchlines'] -= end - start + 1
         self.cliques = filtered
 
 # Property flags
index d4f28c9a958a97d7cf01befbcb2e71cb40680bc9..2c4f67bf777a1c7f8e457293ba961ebed9f2e600 100755 (executable)
@@ -23,17 +23,18 @@ if __name__ == '__main__':
             sys.stdout.write(rep[:-1] + "\n")
         print 75 * '-'
         for clique in state.cliques:
-            for (file, start, end) in clique:
-                if start == 1 and end == state.files[file]:
-                    print "%% %s:%d-%d: entire (%d matches)" % \
-                          (file, start, end, len(clique))
-                    continue
-            print "%% %s:%d-%d: (%d matches)" % (file, start, end, len(clique))
-            sys.stdout.write(state.extract_text(clique))
+            (file, start, end, text) = state.extract_text(clique)
+            if start == 1 and end == state.files[file]:
+                print "%% %s:%d-%d: entire (%d matches)" % \
+                      (file, start, end, len(clique))
+            else:
+                print "%% %s:%d-%d: (%d matches)" % \
+                      (file, start, end, len(clique))
+            sys.stdout.write(text)
     # Main line begins here
     import getopt
     try:
-        (optlist, args) = getopt.getopt(sys.argv[1:], 'd:m')
+        (optlist, args) = getopt.getopt(sys.argv[1:], 'd:m:')
     except getopt.GetoptError:
         sys.stderr.write("usage: filterator [-d dir] [-m size]\n")
         sys.exit(2)
@@ -50,7 +51,10 @@ if __name__ == '__main__':
         state = comparator.CommonReport(sys.stdin, dir)
         if minsize:
             state.filter_by_size(minsize)
-        report_common(state)
+        try:
+            report_common(state)
+        except KeyboardInterrupt:
+            pass
     except comparator.ComparatorException, e:
         sys.stderr.write("filterator: " + e.message + "\n")
         sys.exit(1)