From 31cf445ff5b5c9161b782ac42742ba6c76bf1e49 Mon Sep 17 00:00:00 2001 From: Silvio Rhatto Date: Sat, 27 Jul 2024 18:54:22 -0300 Subject: feat: speedup FinderPluginFileNameSearcher by leveraging a filelist cache --- packages/finder/plugin/searcher/file/name.py | 46 ++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 9 deletions(-) (limited to 'packages') diff --git a/packages/finder/plugin/searcher/file/name.py b/packages/finder/plugin/searcher/file/name.py index 3fce4ac..caf2a63 100644 --- a/packages/finder/plugin/searcher/file/name.py +++ b/packages/finder/plugin/searcher/file/name.py @@ -20,6 +20,10 @@ import os import re +import json + +from threading import Thread +from platformdirs import user_cache_dir class FinderPluginFileNameSearcher(): def __init__(self, path): @@ -34,9 +38,41 @@ class FinderPluginFileNameSearcher(): elif 'path' in dir(self) and self.path != path: self.items = [] - self.path = path + cache_dir = user_cache_dir('finder', 'rhatto') + self.path = path + self.cache = os.path.join(cache_dir, 'plugin', 'searcher', 'file', os.path.abspath(self.path).replace(os.sep, '_') + '.json') async def finder(self): + # Check existing cache + if os.path.exists(self.cache) and os.path.isfile(self.cache): + with open(self.cache, 'r') as f: + self.items = json.load(f) + + # Then update the folder list in the background + walker = Thread(target=self.walker, name='Walker for ' + self.path) + + return self.items + + # Update the folder list + self.walker() + + # Update the cache + os.makedirs(os.path.dirname(self.cache), exist_ok=True) + with open(self.cache, 'w') as f: + json.dump(self.items, f, sort_keys=True) + + return self.items + + def filter(self, text = ''): + if text == '' or text is None: + return self.items + + return filter(lambda s: re.search(re.escape(text), s, flags=re.IGNORECASE), self.items) + + def has_cache(self): + pass + + def walker(self): ignore_folders = [ '.git', '__pycache__' ] ignore_files = [ '.gitignore', '.gitattributes', '.gitmodules' ] @@ -66,11 +102,3 @@ class FinderPluginFileNameSearcher(): continue self.items.append(os.path.join(parent, file)) - - return self.items - - def filter(self, text = ''): - if text == '' or text is None: - return self.items - - return filter(lambda s: re.search(re.escape(text), s, flags=re.IGNORECASE), self.items) -- cgit v1.2.3