-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
76 lines (59 loc) · 2.74 KB
/
main.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
from ulauncher.api.client.Extension import Extension
from ulauncher.api.client.EventListener import EventListener
from ulauncher.api.shared.action.SetUserQueryAction import SetUserQueryAction
from ulauncher.api.shared.event import KeywordQueryEvent, PreferencesUpdateEvent
from ulauncher.api.shared.item.ExtensionResultItem import ExtensionResultItem
from ulauncher.api.shared.action.RenderResultListAction import RenderResultListAction
import os
import json
import logging
EXT_DIR = os.environ["HOME"] + "/.local/share/ulauncher/extensions/"
log = logging.getLogger(__name__)
items = []
class ExtensionList(Extension):
def __init__(self):
super(ExtensionList, self).__init__()
self.subscribe(KeywordQueryEvent, KeywordQueryEventListener())
self.subscribe(PreferencesUpdateEvent, PreferencesUpdateListener)
class KeywordQueryEventListener(EventListener):
def on_event(self, event, extension):
if len(items) == 0:
for ext in self.get_extensions():
items.append(ExtensionResultItem(icon=ext.icon,
name='%s (%s)' % (ext.name, ext.def_value.rstrip().upper()),
description='%s' % ext.description,
on_enter=SetUserQueryAction(ext.def_value)
))
term = (event.get_argument() or "").lower()
sorted = self.sort(term)
return RenderResultListAction(sorted)
def sort(self, query):
return list(filter(lambda item: query in item.get_name().lower(), items))
def get_extensions(self):
extensions = []
for dir in os.listdir(EXT_DIR):
manifest_dir = EXT_DIR + dir + "/manifest.json"
with open(manifest_dir) as manifest:
data = json.load(manifest)
for pref in data["preferences"]:
if pref["type"] == "keyword" and pref["id"] != "exlist":
extensions.append(Extension(pref["name"],
data["description"],
pref["default_value"] + " ",
EXT_DIR + dir + "/" + data["icon"])
)
return extensions
class PreferencesUpdateListener(PreferencesUpdateEvent):
items = []
class Extension:
name = ""
description = ""
def_value = ""
icon = ""
def __init__(self, name, description, def_value, icon):
self.name = name
self.description = description
self.def_value = def_value
self.icon = icon
if __name__ == '__main__':
ExtensionList().run()