aboutsummaryrefslogtreecommitdiff
path: root/packages
diff options
context:
space:
mode:
authorSilvio Rhatto <rhatto@riseup.net>2024-07-27 18:54:22 -0300
committerSilvio Rhatto <rhatto@riseup.net>2024-07-27 18:54:22 -0300
commit31cf445ff5b5c9161b782ac42742ba6c76bf1e49 (patch)
tree7b4a7fea4923cdde681e2a27e3bf49d80d62a4c3 /packages
parent7bf86d8aa2afbda36c347ed2e5d59635c902c376 (diff)
downloadfinder-31cf445ff5b5c9161b782ac42742ba6c76bf1e49.tar.gz
finder-31cf445ff5b5c9161b782ac42742ba6c76bf1e49.tar.bz2
feat: speedup FinderPluginFileNameSearcher by leveraging a filelist cache
Diffstat (limited to 'packages')
-rw-r--r--packages/finder/plugin/searcher/file/name.py46
1 files changed, 37 insertions, 9 deletions
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)