woo match-search

This commit is contained in:
q
2013-01-20 15:43:43 +02:00
parent 1a7fc1a18c
commit cc59b4266e

View File

@@ -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)