Debian Codegraph @

tools/edit.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3
4 ##--------------------------------------##
5 # WxGeometrie #
6 # Small facility for editing file #
7 # after Python bug repport #
8 ##--------------------------------------##
9 # WxGeometrie
10 # Dynamic geometry, graph plotter, and more for french mathematic teachers.
11 # Copyright (C) 2005-2013 Nicolas Pourcelot
12 #
13 # This program is free software; you can redistribute it and/or modify
14 # it under the terms of the GNU General Public License as published by
15 # the Free Software Foundation; either version 2 of the License, or
16 # (at your option) any later version.
17 #
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 # GNU General Public License for more details.
22 #
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26
27
28 import re
29 import sys
30 import subprocess
31
32 # ----- User config -----
33 DEFAULT_EDITOR = 'geany'
34 # XXX: move this outside the script
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 #print(repr(file_and_line))
44 try:
45 m = re.search('[Ff]ile "?([^",]+)"?, line ([0-9]+)', file_and_line)
46 if m is None:
47 # Format pyflakes
48 m = re.search('([^"]+[.]py):([0-9]+)', file_and_line)
49 if m is None:
50 # Fichier seul
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))