-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathedpy
executable file
·52 lines (42 loc) · 1.51 KB
/
edpy
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
#!/usr/bin/env python
# Edit a Python module using its module name rather than filesystem path.
# Designed to work from an import line as well ("import X" or
# "from X import Y") for easier copy and paste.
# e.g. 'edpy tempfile'
# 'edpy calendar.Calendar' -- takes you directly to the class
# 'edpy calendar.Calendar.monthdayscalendar' -- takes you directly to the method
# 'edpy from calendar.Calendar import monthdayscalendar' -- same as above
# Author: David McClosky
# Homepage: http://zorglish.org
# Code: http://github.com/dmcc
# vi: syntax=python
import sys, inspect, os
args = sys.argv[1:]
editor_args = []
pieces = []
for piece in args:
if piece[0] == '-':
editor_args.append(piece)
else:
pieces.extend(piece.split('.'))
pieces = [p for p in pieces if p not in ('from', 'import', 'as')]
obj_pieces = []
while 1:
try:
modulename = '.'.join(pieces)
module = __import__(modulename, globals(), locals(), modulename)
break
except ImportError:
obj_pieces.insert(0, pieces.pop(-1))
obj = module
while obj_pieces:
obj = getattr(obj, obj_pieces.pop(0))
filename = inspect.getsourcefile(obj).replace('.pyc', '.py')
lines, lineno = inspect.findsource(obj)
editor = os.getenv('EDITOR', 'vi')
# seems vi and emacs support this
if ('vi' in editor or 'emacs' in editor) and lineno is not None:
editor_args.append('+%d' % (lineno + 1))
command = "{} {} {}".format(editor, ' '.join(editor_args), filename)
print("Running", repr(command))
os.system(command)