在看ctags的協助文檔時看到了ptags.py,專為python產生tags.具體目錄位於python原始碼的Tools/scripts/ptags.py。自己可以到Python官網下載
原始碼,解壓提取ptags.py放在path變數搜尋的目錄裡。Python2.x和Python3.x都有對應的ptags.py ,其實這裡面的原理是一樣的,根本不涉及到2.x和3.x之間有差異的文法,而且產生的tags檔案也主要是針對類和函數(class and def)所以,沒有什麼區別,完全可以在想聲稱python3.x的tags時,使用python2.x的pytags。
我的機器上放置的位置為:Debian Squeeze/Wheezy/Sid --> /usr/local/bin
Mandriva /openSUSE ------------->$HOME/bin
[wolf@localhost tmp]$ diff ptags.py ptags3.py<br />1c1<br />< #! /usr/bin/env python<br />---<br />> #! /usr/bin/env python3<br />##就只有那麼點差異而已,解譯器使用的不一樣而已。
ptags.py(for python2.x):
#! /usr/bin/env python<br /># ptags<br />#<br /># Create a tags file for Python programs, usable with vi.<br /># Tagged are:<br /># - functions (even inside other defs or classes)<br /># - classes<br /># - filenames<br /># Warns about files it cannot open.<br /># No warnings about duplicate tags.<br />import sys, re, os<br />tags = [] # Modified global variable!<br />def main():<br /> args = sys.argv[1:]<br /> for filename in args:<br /> treat_file(filename)<br /> if tags:<br /> fp = open('tags', 'w')<br /> tags.sort()<br /> for s in tags: fp.write(s)<br />expr = '^[ /t]*(def|class)[ /t]+([a-zA-Z0-9_]+)[ /t]*[:/(]'<br />matcher = re.compile(expr)<br />def treat_file(filename):<br /> try:<br /> fp = open(filename, 'r')<br /> except:<br /> sys.stderr.write('Cannot open %s/n' % filename)<br /> return<br /> base = os.path.basename(filename)<br /> if base[-3:] == '.py':<br /> base = base[:-3]<br /> s = base + '/t' + filename + '/t' + '1/n'<br /> tags.append(s)<br /> while 1:<br /> line = fp.readline()<br /> if not line:<br /> break<br /> m = matcher.match(line)<br /> if m:<br /> content = m.group(0)<br /> name = m.group(2)<br /> s = name + '/t' + filename + '/t/^' + content + '//n'<br /> tags.append(s)<br />if __name__ == '__main__':<br /> main()<br />
ptags.py(for python3.x)
#! /usr/bin/env python3<br /># ptags<br />#<br /># Create a tags file for Python programs, usable with vi.<br /># Tagged are:<br /># - functions (even inside other defs or classes)<br /># - classes<br /># - filenames<br /># Warns about files it cannot open.<br /># No warnings about duplicate tags.<br />import sys, re, os<br />tags = [] # Modified global variable!<br />def main():<br /> args = sys.argv[1:]<br /> for filename in args:<br /> treat_file(filename)<br /> if tags:<br /> fp = open('tags', 'w')<br /> tags.sort()<br /> for s in tags: fp.write(s)<br />expr = '^[ /t]*(def|class)[ /t]+([a-zA-Z0-9_]+)[ /t]*[:/(]'<br />matcher = re.compile(expr)<br />def treat_file(filename):<br /> try:<br /> fp = open(filename, 'r')<br /> except:<br /> sys.stderr.write('Cannot open %s/n' % filename)<br /> return<br /> base = os.path.basename(filename)<br /> if base[-3:] == '.py':<br /> base = base[:-3]<br /> s = base + '/t' + filename + '/t' + '1/n'<br /> tags.append(s)<br /> while 1:<br /> line = fp.readline()<br /> if not line:<br /> break<br /> m = matcher.match(line)<br /> if m:<br /> content = m.group(0)<br /> name = m.group(2)<br /> s = name + '/t' + filename + '/t/^' + content + '//n'<br /> tags.append(s)<br />if __name__ == '__main__':<br /> main()<br />