From 380ddf573ee84ab9844fa4018d2e885631538c91 Mon Sep 17 00:00:00 2001 From: "Eric S. Raymond" Date: Thu, 18 Sep 2003 08:48:06 +0000 Subject: [PATCH] Improved filterator -- KeyboardInterrupt now handled gracefully, and statistics are correct. --- comparator.py | 10 ++++++---- filterator | 22 +++++++++++++--------- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/comparator.py b/comparator.py index cea57f4..4c0ddda 100644 --- a/comparator.py +++ b/comparator.py @@ -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 diff --git a/filterator b/filterator index d4f28c9..2c4f67b 100755 --- a/filterator +++ b/filterator @@ -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) -- 2.43.0