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