-
Notifications
You must be signed in to change notification settings - Fork 5
/
Decompile to Editor.py
79 lines (59 loc) · 1.92 KB
/
Decompile to Editor.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
#!/usr/bin/python3
import json
import subprocess
import tempfile
from pathlib import Path
# ---------------------------------------------------------------------------
# Configuration
DEFAULT_CONFIG = {
'editor': 'Sublime Text',
'editors': {
'BBEdit': ['/usr/local/bin/bbedit'],
'MacVim': ['/Applications/MacVim.app/Contents/MacOS/Vim', '-g'],
'SubEthaEdit': ['/usr/local/bin/see'],
'Sublime Text': ['/usr/local/bin/subl'],
'TextMate': ['/usr/local/bin/mate'],
'Visual Studio Code': ['/usr/local/bin/code']
}
}
def get_config_path():
script = Path(__file__)
filename = script.stem + '.json'
filepath = script.parent.joinpath(filename)
return filepath
def read_config():
config_path = get_config_path()
if not config_path.is_file():
save_config(DEFAULT_CONFIG)
return json.loads(config_path.read_text())
def save_config(data):
config_path = get_config_path()
_ = config_path.write_text(json.dumps(data, indent=2))
def get_editor_command():
try:
config = read_config()
except:
config = DEFAULT_CONFIG
editor = config['editor']
command = config['editors'][editor]
return command
# ---------------------------------------------------------------------------
def main():
doc = Document.getCurrentDocument()
seg = doc.getCurrentSegment()
addr = doc.getCurrentAddress()
proc = seg.getProcedureAtAddress(addr)
if proc is None:
return
sig = proc.signatureString()
code = proc.decompile()
text = "%s {\n%s}" % (sig, code)
command = get_editor_command() # ['/usr/local/bin/subl']
with tempfile.NamedTemporaryFile('w', suffix='.m') as temp:
temp.write(text)
temp.flush()
command.append(temp.name)
_ = subprocess.call(command)
# ---------------------------------------------------------------------------
if __name__ == '__main__':
main()