From db6f35bbe8117fced19dd2385ac9dcb9f2bf9230 Mon Sep 17 00:00:00 2001 From: Peter Odding Date: Mon, 2 Sep 2013 22:18:04 +0200 Subject: [PATCH] Improvements to search-notes.py - Bug fix: Output encoding of listed keywords - Bug fix: Don't print empty line when there's no matches (related to issue #47 but not a complete solution ... yet) - Move index version to variable See also issue #47 on GitHub: https://github.com/xolox/vim-notes/issues/47 --- misc/notes/search-notes.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/misc/notes/search-notes.py b/misc/notes/search-notes.py index d477de7..2bbc0bc 100755 --- a/misc/notes/search-notes.py +++ b/misc/notes/search-notes.py @@ -3,7 +3,7 @@ # Python script for fast text file searching using keyword index on disk. # # Author: Peter Odding -# Last Change: May 12, 2013 +# Last Change: September 2, 2013 # URL: http://peterodding.com/code/vim/notes/ # License: MIT # @@ -56,12 +56,18 @@ except ImportError: import pickle +# Try to import the Levenshtein module, don't error out if it's not installed. try: import Levenshtein levenshtein_supported = True except ImportError: levenshtein_supported = False +# The version of the index format that's supported by this revision of the +# `search-notes.py' script; if an existing index file is found with an +# unsupported version, the script knows that it should rebuild the index. +INDEX_VERSION = 2 + class NotesIndex: def __init__(self): @@ -79,7 +85,8 @@ def __init__(self): self.logger.debug("Finished listing keywords in %s", global_timer) else: matches = self.search_index(keywords) - print '\n'.join(sorted(matches)) + if matches: + print '\n'.join(sorted(matches)) self.logger.debug("Finished searching index in %s", global_timer) def init_logging(self): @@ -147,7 +154,7 @@ def load_index(self): with open(self.database_file) as handle: self.index = pickle.load(handle) self.logger.debug("Format version of index loaded from disk: %i", self.index['version']) - assert self.index['version'] == 2, "Incompatible index format detected!" + assert self.index['version'] == INDEX_VERSION, "Incompatible index format detected!" self.first_use = False self.dirty = False self.logger.debug("Loaded %i notes from index in %s", len(self.index['files']), load_timer) @@ -155,7 +162,7 @@ def load_index(self): self.logger.warn("Failed to load index from file: %s", e) self.first_use = True self.dirty = True - self.index = {'keywords': {}, 'files': {}, 'version': 2} + self.index = {'keywords': {}, 'files': {}, 'version': INDEX_VERSION} def save_index(self): ''' Save the keyword index to disk. ''' @@ -252,7 +259,7 @@ def list_keywords(self, substring, limit=25): decorated.append((-len(filenames), kw)) decorated.sort() selection = [d[-1] for d in decorated[:limit]] - print u'\n'.join(selection) + print self.encode(u'\n'.join(selection)) def tokenize(self, text): ''' Tokenize a string into a list of normalized, unique keywords. '''