-
Notifications
You must be signed in to change notification settings - Fork 0
/
workon.py
76 lines (58 loc) · 1.89 KB
/
workon.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
#!/usr/bin/python
# encoding: utf-8
#
# Copyright © 2015 Jonathan McAuliffe
#
# Created on 15/03/2015
#
"""
"""
from __future__ import unicode_literals, print_function
import sys
import os
import subprocess
from workflow import Workflow, ICON_NETWORK
VERSION = '1.0'
def search_key_for_venv(venv):
"""Generate a string search key for a venv"""
elements = []
elements.append(venv) # title of post
return u' '.join(elements)
def main(wf):
# Get query from Alfred
if len(wf.args):
query = wf.args[0]
else:
query = None
# -- this way works in the terminal but not with alfred... --
# p = subprocess.Popen("source $(which virtualenvwrapper.sh)
# && lsvirtualenv -b | sort -df | grep -v '^$'", shell=True,
# stdout=subprocess.PIPE)
workon_home = os.getenv('WORKON_HOME')
workon_home = b"{}".format(workon_home)
p = subprocess.check_output([b'ls',
os.path.join(os.getenv('HOME'),
'.virtualenvs')])
p = wf.decode(p)
# lst = (p.stdout.read()).split('\n')
lst = p.split('\n')
# venvs = [x for x in lst if x != '']
venvs = [x for x in lst if
os.path.exists(os.path.join(os.getenv('HOME'),
'.virtualenvs', x,
'bin/activate'))]
# If script was passed a query, use it to filter posts
if query:
venvs = wf.filter(query, venvs, key=search_key_for_venv)
# Loop through the returned venvs and add an item for each to
# the list of results for Alfred
for venv in venvs:
wf.add_item(title=venv,
arg=venv,
valid=True,
icon=ICON_NETWORK)
# Send the results to Alfred as XML
wf.send_feedback()
if __name__ == u"__main__":
wf = Workflow()
sys.exit(wf.run(main))