diff options
-rw-r--r-- | ChangeLog.md | 2 | ||||
-rw-r--r-- | README.md | 5 | ||||
-rw-r--r-- | TODO.md | 1 | ||||
-rw-r--r-- | packages/finder/plugin/searcher/file/name.py | 46 |
4 files changed, 43 insertions, 11 deletions
diff --git a/ChangeLog.md b/ChangeLog.md index 8bb0f77..cdb2418 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -6,3 +6,5 @@ * [x] Results using ~~a select box instead of a text ~~ read-only buffer. Advantages: easier to select things. + +* [x] Speedup `FinderPluginFileNameSearcher` by leveraging a filelist cache. @@ -18,8 +18,11 @@ browser other contents such as file lists, spreadsheets and remote queries. * [Python][] 3+. * [Prompt Toolkit][] ([python3-prompt-toolkit][] on [Debian][]). +* [platformdirs][] ([python3-platformdirs][] on [Debian][]). [Python]: https://python.org [Prompt Toolkit]: https://python-prompt-toolkit.readthedocs.io/en/master/index.html [Debian]: https://www.debian.org -[python3-prompt-toolkit]: https://packages.debian.org/bookworm/python3-prompt-toolkit +[python3-prompt-toolkit]: https://tracker.debian.org/python3-prompt-toolkit +[platformdirs]: https://pypi.org/project/platformdirs/ +[python3-platformdirs]: https://tracker.debian.org/platformdirs @@ -3,7 +3,6 @@ ## Improvements * [ ] Program version. -* [ ] Speedup `FinderPluginFileNameSearcher` by leveraging a filelist cache. * [ ] Abstract/generalize plugin load logic. * [ ] `FinderPluginFileNameSearcher.set_path`: check if path exists. * [ ] `FinderPluginFileXdgOpener.open`: check if file actually exists (and if 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) |