-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path%{APPNAMELC}.py
executable file
·102 lines (83 loc) · 3.19 KB
/
%{APPNAMELC}.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/python3
"""A Plasma runner for the locate command."""
import re
import subprocess
from contextlib import suppress
from pathlib import Path
import dbus.service
# import q
from dbus.mainloop.glib import DBusGMainLoop
from gi.repository import GLib
DBusGMainLoop(set_as_default=True)
objpath = "/runner" # Default value for X-Plasma-DBusRunner-Path metadata property
iface = "org.kde.krunner1"
class Runner(dbus.service.Object):
def __init__(self):
dbus.service.Object.__init__(
self,
dbus.service.BusName("org.kde.%{APPNAMELC}", dbus.SessionBus()),
objpath,
)
@dbus.service.method(iface, in_signature="s", out_signature="a(sssida{sv})")
def Match(
self, query: str
) -> list[tuple[str, str, str, int, float, dict[str, str]]]:
"""This method is used to get the matches and it returns a list of tuples"""
# TODO: NoMatch = 0, CompletionMatch = 10, PossibleMatch = 30, InformationalMatch = 50, HelperMatch = 70, ExactMatch = 100
# q(query)
# Tried to use results as a dict itself but the {'subtext': line} portion is not hashable :/
results: list[tuple[str, str, str, int, float, dict[str, str]]] = []
if len(query) < 3:
return results
words = re.split(r"\s+", query)
self.locate = "/usr/bin/locate"
locate_config = Path("~/.config/locate-krunner").expanduser()
if locate_config.exists():
with locate_config.open() as conf:
for line in conf:
self.locate = str(Path(line.rstrip()).expanduser())
find_cmd = [self.locate, "-l", "100"]
find_cmd += re.split(r"\s+", query)
find_cmd_result = subprocess.run(find_cmd, capture_output=True, check=False)
for file in str.split(find_cmd_result.stdout.decode("UTF-8"), "\n"):
# q(file)
if file == "":
continue
fp = Path(file)
relevance = 1.0
if ".cache" in fp.as_posix():
relevance -= 0.01
for word in words:
if not word.startswith("-"):
if word not in fp.name:
relevance -= 0.02
else:
if word != fp.name:
relevance -= 0.01
results += [
(
file,
file,
"document-open",
100,
relevance,
{
"subtext": file.rsplit(".", 1)[-1] # extension
},
)
]
results.sort(key=lambda x: x[4], reverse=True)
return results[:10]
@dbus.service.method(iface, out_signature="a(sss)")
def Actions(self) -> list[tuple[str, str, str]]:
# pylint: enable=
# id, text, icon
return [("id", "Tooltip", "planetkde")]
@dbus.service.method(iface, in_signature="ss")
def Run(self, data: str, action_id: str) -> None:
with suppress(Exception):
_ = subprocess.Popen(["/usr/bin/xdg-open", data]).pid
# print(data, action_id)
runner = Runner()
loop = GLib.MainLoop()
loop.run()