By using this site, you agree to have cookies stored on your device, strictly for functional purposes, such as storing your session and preferences.

Dismiss

 __init__.py

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