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