From cc59b4266e58fae203df64db97d52c20e2e3f3fe Mon Sep 17 00:00:00 2001 From: q Date: Sun, 20 Jan 2013 15:43:43 +0200 Subject: [PATCH] woo match-search --- file_list.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/file_list.py b/file_list.py index 52021c6..5d47f08 100755 --- a/file_list.py +++ b/file_list.py @@ -34,6 +34,8 @@ def setup_options(): help="SQL file name to use [%(default)s]") parser.add_argument("-s",type=str,dest="search",default=False, help="Search list based on path pattern") + parser.add_argument("--match",type=str,dest="match",default=False, + help="Search for closest match from basenames") parser.add_argument('startpath', action="store",default='.', nargs='?') options=parser.parse_args() @@ -171,11 +173,26 @@ def searchdb(sqlfile,needle): conn=sqlite3.connect(sqlfile) conn.text_factory=str db=conn.cursor() - dbh=conn.cursor() db.execute("SELECT file FROM list WHERE file LIKE ? ORDER BY file",('%'+needle+'%',)) for row in db: print(row[0]) +def matchdb(sqlfile,needle): + import difflib as dl + conn=sqlite3.connect(sqlfile) + conn.text_factory=str + db=conn.cursor() + db.execute("SELECT file FROM list") + ratio=0 + best_match="" + for row in db: + s=dl.SequenceMatcher(None, os.path.basename(row[0]), needle) + s_ratio=s.ratio() + if ratio < s_ratio: + ratio=s_ratio + best_match=row[0] + print(best_match) + def print_structure(files): for hash in files: #print(hash[0]) @@ -193,6 +210,9 @@ def main(): if options.search: searchdb(options.sqlfile,options.search) sys.exit(0) + if options.match: + matchdb(options.sqlfile,options.match) + sys.exit(0) if options.delete: print('Deleting entries...') delete_nonexisting(options.sqlfile,options)