OILS / devtools / completion.py View on Github | oilshell.org

81 lines, 54 significant
1#!/usr/bin/env python2
2from __future__ import print_function
3"""
4completion.py
5"""
6import re
7import sys
8
9
10# e.g. class FooTest(
11CLASS_RE = re.compile(r'\s*class (.*)\(')
12# e.g. def testFoo(self
13METHOD_RE = re.compile(r'\s*def (test.*)\(self')
14
15def ParsePythonTest(f):
16 current_test = None
17 for line in f:
18 match = CLASS_RE.match(line)
19 if match:
20 current_test = match.group(1)
21 continue
22
23 match = METHOD_RE.match(line)
24 if match and current_test is not None:
25 print('%s.%s' % (current_test, match.group(1)))
26
27
28# e.g. foo() {
29FUNC_RE = re.compile(r'^\s* (\S+) \(\) \s* \{', re.VERBOSE)
30
31def ParseShellFunctions(f):
32 actions = []
33 dispatch = False
34 for line in f:
35 match = FUNC_RE.match(line)
36 if match:
37 actions.append(match.group(1))
38 # some line starts with "$@"
39 line = line.lstrip()
40 if line.startswith('"$@"') or line.startswith('run-task "$@"'):
41 dispatch = True
42
43 if dispatch:
44 for action in actions:
45 print(action)
46
47
48def main(argv):
49 """Returns an exit code."""
50 try:
51 action = argv[1]
52 except IndexError:
53 raise RuntimeError('Action required')
54
55 try:
56 filename = argv[2]
57 except IndexError:
58 raise RuntimeError('Filename required')
59
60 try:
61 f = open(filename)
62 except IOError:
63 # Silent failure
64 return 1
65
66 if action == 'pyunit':
67 ParsePythonTest(f)
68
69 elif action == 'bash':
70 ParseShellFunctions(f)
71
72 f.close()
73 return 0
74
75
76if __name__ == '__main__':
77 try:
78 sys.exit(main(sys.argv))
79 except RuntimeError, e:
80 print(e.message, file=sys.stderr)
81 sys.exit(1)