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