import os import threading from unidecode import unidecode from . import config filelist = None filelist_lock = threading.Lock() def filter_strings(q, lst, processor): q = unidecode(q).lower().split() ret = [] for entry in lst: processed_entry = unidecode(processor(entry)).lower() if all(word in processed_entry for word in q): ret.append(entry) return ret def search(q: str): with filelist_lock: global filelist if filelist is None: filelist = [] for root, dirs, files in os.walk(config.get('base_path'), topdown=True): # We want the root relative to the base path rel_root = root[len(config.get('base_path')):] # Don't search search hidden stuff [dirs.remove(x) for x in list(dirs) if x.startswith('.')] [files.remove(x) for x in list(files) if x.startswith('.')] filelist += map(lambda x: [rel_root, x], dirs + files) copy_filelist = filelist ret = filter_strings(q, copy_filelist, lambda x: x[1]) return ret return [x[0] for x in ret]