forked from orf/alfred-pycharm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalfred-pycharm.py
77 lines (61 loc) · 2.24 KB
/
alfred-pycharm.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
#!/usr/bin/python
# encoding: utf-8
import sys
# Workflow3 supports Alfred 3's new features. The `Workflow` class
# is also compatible with Alfred 2.
from workflow import Workflow3, ICON_INFO
import xml.etree.ElementTree as ET
import os
LAUNCHER_DIR = '/usr/local/bin/charm'
RECENT_XPATH = ".//component[@name='RecentDirectoryProjectsManager']/option[@name='recentPaths']/list/option"
def parse_start_script(path=LAUNCHER_DIR):
run_path, config_path = None, None
for line in open(path, 'r'):
line = line.strip()
if line.startswith('CONFIG_PATH = '):
config_path = line.split('=')[1].strip().replace("u'", '').rstrip("'")
elif line.startswith('RUN_PATH = '):
run_path = line.split('=')[1].strip().replace("u'", '').rstrip("'")
return run_path, config_path
def main(wf):
if wf.update_available:
# Add a notification to top of Script Filter results
wf.add_item('New version available',
'Action this item to install the update',
autocomplete='workflow:update',
icon=ICON_INFO)
pycharm_path, config_path = parse_start_script()
home_dir = os.path.expanduser('~')
root = ET.parse(config_path + '/options/recentProjectDirectories.xml')
project_paths = (
el.attrib['value'].replace('$USER_HOME$', home_dir)
for el in root.findall(RECENT_XPATH)
)
paths_with_names = (
(path, os.path.basename(path))
for path in project_paths
)
query = None
if wf.args:
query = wf.args[0]
results = wf.filter(query, paths_with_names, lambda r: r[1], fold_diacritics=False)
for path, name in results:
wf.add_item(
title=name,
subtitle=path,
uid=path,
arg=path,
valid=True,
icontype='fileicon',
icon=pycharm_path
)
wf.send_feedback()
if __name__ == '__main__':
# Create a global `Workflow3` object
wf = Workflow3(update_settings={
'github_slug': 'orf/alfred-pycharm',
})
# Call your entry function via `Workflow3.run()` to enable its
# helper functions, like exception catching, ARGV normalization,
# magic arguments etc.
sys.exit(wf.run(main))