diff options
author | Silvio Rhatto <rhatto@riseup.net> | 2024-07-27 18:54:22 -0300 |
---|---|---|
committer | Silvio Rhatto <rhatto@riseup.net> | 2024-07-27 18:54:22 -0300 |
commit | 31cf445ff5b5c9161b782ac42742ba6c76bf1e49 (patch) | |
tree | 7b4a7fea4923cdde681e2a27e3bf49d80d62a4c3 | |
parent | 7bf86d8aa2afbda36c347ed2e5d59635c902c376 (diff) | |
download | finder-31cf445ff5b5c9161b782ac42742ba6c76bf1e49.tar.gz finder-31cf445ff5b5c9161b782ac42742ba6c76bf1e49.tar.bz2 |
feat: speedup FinderPluginFileNameSearcher by leveraging a filelist cache
-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) |