woo match-search
This commit is contained in:
22
file_list.py
22
file_list.py
@@ -34,6 +34,8 @@ def setup_options():
|
|||||||
help="SQL file name to use [%(default)s]")
|
help="SQL file name to use [%(default)s]")
|
||||||
parser.add_argument("-s",type=str,dest="search",default=False,
|
parser.add_argument("-s",type=str,dest="search",default=False,
|
||||||
help="Search list based on path pattern")
|
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='?')
|
parser.add_argument('startpath', action="store",default='.', nargs='?')
|
||||||
|
|
||||||
options=parser.parse_args()
|
options=parser.parse_args()
|
||||||
@@ -171,11 +173,26 @@ def searchdb(sqlfile,needle):
|
|||||||
conn=sqlite3.connect(sqlfile)
|
conn=sqlite3.connect(sqlfile)
|
||||||
conn.text_factory=str
|
conn.text_factory=str
|
||||||
db=conn.cursor()
|
db=conn.cursor()
|
||||||
dbh=conn.cursor()
|
|
||||||
db.execute("SELECT file FROM list WHERE file LIKE ? ORDER BY file",('%'+needle+'%',))
|
db.execute("SELECT file FROM list WHERE file LIKE ? ORDER BY file",('%'+needle+'%',))
|
||||||
for row in db:
|
for row in db:
|
||||||
print(row[0])
|
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):
|
def print_structure(files):
|
||||||
for hash in files:
|
for hash in files:
|
||||||
#print(hash[0])
|
#print(hash[0])
|
||||||
@@ -193,6 +210,9 @@ def main():
|
|||||||
if options.search:
|
if options.search:
|
||||||
searchdb(options.sqlfile,options.search)
|
searchdb(options.sqlfile,options.search)
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
if options.match:
|
||||||
|
matchdb(options.sqlfile,options.match)
|
||||||
|
sys.exit(0)
|
||||||
if options.delete:
|
if options.delete:
|
||||||
print('Deleting entries...')
|
print('Deleting entries...')
|
||||||
delete_nonexisting(options.sqlfile,options)
|
delete_nonexisting(options.sqlfile,options)
|
||||||
|
|||||||
Reference in New Issue
Block a user