-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinit.py
More file actions
61 lines (52 loc) · 1.34 KB
/
init.py
File metadata and controls
61 lines (52 loc) · 1.34 KB
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
# encoding: utf-8
import sys
import shell
from workflow import Workflow, ICON_ERROR
shells = {
"fish": {
"cmd": ["/usr/local/bin/fish", "-c", "set"],
"replace": {},
"separator": " "
},
"bash": {
"cmd": ["/bin/bash", "-ic", "export"],
"replace": {"declare -x": ""},
"separator": "="
},
"zsh": {
"cmd": ["/bin/zsh", "-ic", "export"],
"replace": {},
"separator": "="
},
}
def getVariables(shellName):
def _getVars(cmd):
variables = sh.execute(cmd)
return variables.split(b"\n")
def _split(item, symbol):
tmp = item.split(symbol)
return {"name": unicode(tmp[0].decode("utf-8")), "value": unicode(symbol.join(tmp[1:]).decode("utf-8"))}
def execute(shellInfos):
variables = _getVars(shellInfos["cmd"])
result = []
for variable in variables:
if len(variable):
for pattern in shellInfos["replace"]:
variable = variable.replace(pattern, shellInfos["replace"][pattern])
result.append(_split(variable, shellInfos["separator"]))
return result
sh = shell.Shell()
if shellName in shells:
return execute(shells[shellName])
return None
def main(wf):
try:
wf.settings["data"] = getVariables(wf.settings["shell"])
except:
wf.add_item(title="Missing shell",
subtitle="You should run sv shell",
icon=ICON_ERROR)
wf.send_feedback()
if __name__ == "__main__":
wf = Workflow()
sys.exit(wf.run(main))