__init__.py
Python script, ASCII text executable
1import asyncio 2import subprocess 3import logging 4from pathlib import Path 5import gettext 6 7_ = gettext.gettext 8 9 10logging.basicConfig(level=logging.DEBUG) 11 12config_template = { 13"path": "~/", 14"min_chars": 3, 15"case_insensitive": True, 16"limit": 16, 17"enable_search": True, 18"enable_path": True 19} 20 21class Provider: 22def __init__(self, config: dict): 23self.config = config 24self.name = _("Files") 25self.icon = "system-file-manager" 26self.description = _("Search for files and directories on your device.") 27 28async def search(self, query: str): 29if len(query) < self.config["min_chars"]: 30return 31 32if self.config["enable_path"] and query.startswith("/") or query.startswith("~/"): 33# Provide an entry to open the entered path 34def execute(): 35subprocess.Popen(["xdg-open", str(Path(query).expanduser().resolve())]) 36 37yield { 38"name": _("Open in file manager"), 39"description": str(Path(query).expanduser().resolve()), 40"image": ("logo", "system-file-manager"), 41"execute": execute 42} 43 44process = await asyncio.create_subprocess_exec( 45"locate", query, 46"-r", str(Path(self.config["path"]).expanduser().resolve()) + "/*", 47"-i" if self.config["case_insensitive"] else "", 48*("-l", str(self.config["limit"])) if self.config["limit"] > 0 else (), 49stdout=subprocess.PIPE, 50stderr=subprocess.PIPE 51) 52 53stdout, stderr = await process.communicate() 54 55if process.returncode != 0: 56logging.error(f"Cannot locate files: {stderr.decode().strip()}") 57return 58 59for line in stdout.decode().splitlines(): 60file_path = Path(line.strip()) 61 62if not file_path.exists() or not (file_path.is_file() or file_path.is_dir()): 63continue 64 65def execute(file_path=file_path): 66subprocess.Popen(["xdg-open", str(file_path)]) 67 68yield { 69"name": file_path.name, 70"description": str(file_path), 71"image": ("logo", "system-file-manager"), 72"execute": execute 73} 74