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