-
Notifications
You must be signed in to change notification settings - Fork 17
/
main.py
85 lines (64 loc) · 2.95 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
"""
Ulauncher extension for opening recent projects on Jetbrains IDEs.
"""
import json
import logging
import os
# pylint: disable=import-error
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.ExtensionResultItem import ExtensionResultItem
from ulauncher.api.shared.action.RenderResultListAction import RenderResultListAction
from ulauncher.api.shared.action.HideWindowAction import HideWindowAction
from ulauncher.api.shared.action.RunScriptAction import RunScriptAction
from ulauncher.api.shared.action.CopyToClipboardAction import CopyToClipboardAction
from jetbrains import RecentProjectsParser
LOGGING = logging.getLogger(__name__)
class JetbrainsLauncherExtension(Extension):
""" Main Extension Class """
def __init__(self):
""" Initializes the extension """
super(JetbrainsLauncherExtension, self).__init__()
self.subscribe(KeywordQueryEvent, KeywordQueryEventListener())
def get_recent_projects_file_path(self, keyword):
if keyword in ['pstorm', 'webstorm', 'pycharm']:
return os.path.expanduser(self.preferences.get("%s_projects_file" % keyword))
raise AttributeError("Invalid keyword detected")
def get_icon(self, keyword):
""" Returns the application icon based on the keyword """
icon_path = os.path.join('images', "%s.png" % keyword)
return icon_path
def get_launcher_file(self, keyword):
""" Returns the launcher file from preferences"""
return os.path.expanduser(self.preferences.get("%s_projects_file" % keyword))
class KeywordQueryEventListener(EventListener):
""" Listener that handles the user input """
# pylint: disable=unused-argument,no-self-use
def on_event(self, event, extension):
""" Handles the event """
items = []
keyword = event.get_keyword()
query = event.get_argument() or ""
file_path = extension.get_recent_projects_file_path(keyword)
projects = RecentProjectsParser.parse(file_path, query)
if not projects:
return RenderResultListAction([
ExtensionResultItem(
icon=extension.get_icon(keyword),
name='No projects found',
on_enter=HideWindowAction()
)
])
for project in projects:
items.append(ExtensionResultItem(
icon=extension.get_icon(keyword),
name=project['name'],
description=project['path'],
on_enter=RunScriptAction('%s %s &' % (
extension.get_launcher_file(keyword), project['path']), []),
on_alt_enter=CopyToClipboardAction(project['path'])
))
return RenderResultListAction(items)
if __name__ == '__main__':
JetbrainsLauncherExtension().run()