-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
113 lines (91 loc) · 4.17 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
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
103
104
105
106
107
108
109
110
111
112
113
""" Main Module """
import logging
import os
import subprocess
from ulauncher.api.client.Extension import Extension
from ulauncher.api.client.EventListener import EventListener
from ulauncher.api.shared.event import KeywordQueryEvent, ItemEnterEvent
from ulauncher.api.shared.item.ExtensionSmallResultItem import ExtensionSmallResultItem
from ulauncher.api.shared.action.RenderResultListAction import RenderResultListAction
from ulauncher.api.shared.action.ExtensionCustomAction import ExtensionCustomAction
from ulauncher.api.shared.action.HideWindowAction import HideWindowAction
from ulauncher.api.shared.action.OpenUrlAction import OpenUrlAction
from shortcuts.service import ShortcutsService
from shortcuts.loader import ShortcutsLoader
logger = logging.getLogger(__name__)
class ShortcutsExtension(Extension):
""" Main Extension Class """
def __init__(self):
""" Initializes the extension """
super(ShortcutsExtension, self).__init__()
self.subscribe(KeywordQueryEvent, KeywordQueryEventListener())
self.subscribe(ItemEnterEvent, ItemEnterEventListener())
self.shortcuts_service = ShortcutsService(ShortcutsLoader())
def show_applications(self, query):
""" shows the list of available applications known by the extension """
apps = self.shortcuts_service.get_applications(query)
if not apps:
return RenderResultListAction([
ExtensionSmallResultItem(
icon='images/icon.png',
name='No applications found matching your criteria',
highlightable=False,
on_enter=HideWindowAction())
])
items = []
for app in apps[:15]:
items.append(
ExtensionSmallResultItem(icon=app['icon'],
name=app['name'],
on_enter=ExtensionCustomAction({
"action":
"show",
"app":
app,
}),
on_alt_enter=OpenUrlAction(
app['reference_url'])))
return RenderResultListAction(items)
def show_shortcuts_window(self, app):
""" Shows the shortcuts window """
script_path = os.path.join(os.path.dirname(__file__), 'ui', 'main.py')
cmd = "python %s --file %s" % (script_path, app["path"])
result = subprocess.run(cmd,
shell=True,
stdin=None,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
close_fds=True)
print(result)
if result.returncode != 0:
logger.error("Error Opening shortcuts window: " +
str(result.stderr))
def refresh_shortcuts(self):
""" Refreshes the shortcuts """
return RenderResultListAction([
ExtensionSmallResultItem(
icon='images/icon.png',
name='Refresh Shortcuts',
description='Press enter to refresh shortcuts',
highlightable=False,
on_enter=ExtensionCustomAction({"action": "refresh"}))
])
class KeywordQueryEventListener(EventListener):
""" Listener that handles the user input """
def on_event(self, event, extension):
""" Handles the event """
query = event.get_argument() or ""
if query == "refresh":
return extension.refresh_shortcuts()
return extension.show_applications(query)
class ItemEnterEventListener(EventListener):
""" Listener that handles the click on an item """
def on_event(self, event, extension):
""" Handles the event """
data = event.get_data()
if data["action"] == "refresh":
extension.shortcuts_service.load()
return
extension.show_shortcuts_window(data["app"])
if __name__ == '__main__':
ShortcutsExtension().run()