| 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 |
import re |
| 29 |
import sys |
| 30 |
import subprocess |
| 31 |
|
| 32 |
|
| 33 |
DEFAULT_EDITOR = 'geany' |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
SUPPORTED_EDITORS = ('geany', 'gedit', 'nano', 'vim', 'emacs', 'kate') |
| 38 |
|
| 39 |
def edit(file_and_line, editor=DEFAULT_EDITOR): |
| 40 |
"Edit specified file at specified line, with editor." |
| 41 |
|
| 42 |
file_and_line = file_and_line.strip() |
| 43 |
|
| 44 |
try: |
| 45 |
m = re.search('[Ff]ile "?([^",]+)"?, line ([0-9]+)', file_and_line) |
| 46 |
if m is None: |
| 47 |
|
| 48 |
m = re.search('([^"]+[.]py):([0-9]+)', file_and_line) |
| 49 |
if m is None: |
| 50 |
|
| 51 |
m = re.search('[^"]+[.]py', file_and_line) |
| 52 |
file_name = m.group() |
| 53 |
line = '1' |
| 54 |
else: |
| 55 |
file_name = m.group(1) |
| 56 |
line = m.group(2) |
| 57 |
except AttributeError: |
| 58 |
print("Incorrect format (see help) : %s" % repr(file_and_line)) |
| 59 |
return |
| 60 |
if editor not in SUPPORTED_EDITORS: |
| 61 |
print('"%s" is currently not supported.' % editor) |
| 62 |
print('Supported editors : ' + ','.join(SUPPORTED_EDITORS)) |
| 63 |
return |
| 64 |
if editor in ('geany', 'kate'): |
| 65 |
command = '%s -l %s %s' % (editor, line, repr(file_name)) |
| 66 |
else: |
| 67 |
command = '%s +%s %s' % (editor, line, repr(file_name)) |
| 68 |
subprocess.call(command, shell=True) |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
def usage(): |
| 73 |
"Affiche l'aide." |
| 74 |
print("""\n === Usage ===\n |
| 75 |
- Éditer le fichier '~/wxgeometrie/filename.py' à la ligne 257 : |
| 76 |
$ ./tools/edit.py 'File "~/wxgeometrie/filename.py", line 257' |
| 77 |
""") |
| 78 |
exit() |
| 79 |
|
| 80 |
|
| 81 |
if __name__ == "__main__": |
| 82 |
args = sys.argv[1:] |
| 83 |
kw = {} |
| 84 |
if not args or args[0] in ('-h', '--help'): |
| 85 |
usage() |
| 86 |
else: |
| 87 |
edit(' '.join(args)) |