forked from bleader/ansible_snippet_generator
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsnippet_generator.py
executable file
·86 lines (73 loc) · 2.7 KB
/
snippet_generator.py
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python
import argparse
import os
from ansible.utils.plugin_docs import get_docstring
ultisnips_play = '''
snippet play
- hosts: ${1:group}
user: ${2:root}
tasks:
endsnippet
'''
snipmate_play = '''
snippet play
\t- hosts: ${1:group}
\t user: ${2:root}
\t tasks:
'''
def generate(path, args, snippets):
doc, examples, _, _ = get_docstring(path)
prefix = args.snipmate and '\t' or ''
l = []
if doc is not None:
l.append(("snippet %s" % doc['module']))
l.append(("%s- name: ${1:task_description}" % prefix))
l.append(("%s %s:" % (prefix, doc['module'])))
count = 1
if 'options' in doc:
for opt in doc['options']:
if 'required' in doc['options'][opt] and \
doc['options'][opt]['required']:
count += 1
if 'default' in doc['options'][opt]:
value = "${%d:%s}" % (count,
doc['options'][opt]['default'])
else:
value = "${%d}" % (count)
l.append(("%s %s: %s" % (prefix, opt, value)))
for opt in doc['options']:
if 'required' not in doc['options'][opt] or \
doc['options'][opt]['required'] is False:
count += 1
if 'default' in doc['options'][opt] and \
doc['options'][opt]['default']:
value = "${%d:%s}" % (count,
doc['options'][opt]['default'])
else:
value = "${%d}" % (count)
l.append(("%s #%s: %s" % (prefix, opt, value)))
if args.ultisnips:
l.append('endsnippet')
l.append('\n')
snippets.append('\n'.join(l))
def main():
parser = argparse.ArgumentParser(prog='snippet_generator.py')
parser.add_argument('--ultisnips', action='store_true',
help='Generate snippets for UltiSnips')
parser.add_argument('--snipmate', action='store_true',
help='Generate snippets for snipMate')
parser.add_argument('modpath', type=str, nargs=1,
help="Path to Ansible's modules")
args = parser.parse_args()
if args.ultisnips:
print (ultisnips_play)
else:
print (snipmate_play)
snippets = []
for root, _, files in os.walk(args.modpath[0]):
for name in files:
if name.endswith('.py'):
generate(os.path.join(root, name), args, snippets)
print ''.join(sorted(snippets)),
if __name__ == '__main__':
main()