Skip to content

Commit 558a222

Browse files
committed
Basic working version. slowly making PEP8 compliant
1 parent a8ed9b5 commit 558a222

File tree

2 files changed

+103
-8
lines changed

2 files changed

+103
-8
lines changed

tools/bash_completion/generator.py

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
#!/usr/bin/env python
2+
# Michael Bartling ([email protected])
23

34
from collections import defaultdict
5+
import pystache
46
import re
57
import subprocess
6-
import pystache
7-
import pprint
88

9-
commandRegex = r"^\s+(?P<command>(--)?\w+)\s+(?P<helptxt>[a-zA-Z ]*)$" # This one extracts single commands and the help txt
9+
# Top level --version is a pain to deal with so ignoring for now
10+
# This one extracts single commands and the help txt
11+
commandRegex = r"^\s+(?P<command>\w+)\s+(?P<helptxt>[a-zA-Z ]*)$"
12+
1013
# Why the hell do spaces get regexed in command1 ?
11-
subcommandRegex = r"^\s+(?P<command1>-+[a-zA-Z_\-]+(?P<modifier1>\s+[A-Z_\-]+)?)(?P<command2>,\s+-+[a-zA-Z_-]+(?P<modifier2>\s+[A-Z_-]+)?)?\s+(?P<helptxt>.*)$" # Gets just about everything
14+
subcommandRegex = r"^\s+(?P<command1>-+[a-zA-Z_\-]+(?P<modifier1>\s+[A-Z_\-]+)?)"\
15+
r"(?P<command2>,\s+-+[a-zA-Z_-]+(?P<modifier2>\s+[A-Z_-]+)?)?"\
16+
r"\s+(?P<helptxt>.*)$"
1217

13-
pp = pprint.PrettyPrinter(indent=2)
1418

1519
def getHelpTxt(command=None):
1620
if command:
@@ -20,11 +24,12 @@ def getHelpTxt(command=None):
2024
out, err = p.communicate()
2125
return out
2226

27+
2328
def parseCommands():
2429
commands = defaultdict(defaultdict)
2530
commands["COMMAND"] = []
2631
helpTxt = getHelpTxt()
27-
#print helpTxt
32+
# print helpTxt
2833
for line in helpTxt.split('\n'):
2934
match = re.search(commandRegex, line)
3035
if match:
@@ -41,6 +46,10 @@ def parseCommands():
4146
commands["COMMAND"].append({"name": g["command"]})
4247

4348
for commandKey in commands:
49+
# Skip
50+
if commandKey == "COMMAND":
51+
continue
52+
4453
command = commands[commandKey]
4554
helpTxt = getHelpTxt(commandKey)
4655
for line in helpTxt.split('\n'):
@@ -87,18 +96,61 @@ def parseCommands():
8796

8897
return commands
8998

99+
90100
def generateMain(commands):
91101
tmplt = ""
92102

103+
txt = []
104+
93105
with open("templates/mbed.tmplt") as fp:
94106
tmplt = fp.read()
95107

96-
print pystache.render(tmplt, commands)
108+
txt.append(pystache.render(tmplt, commands))
109+
110+
return txt
111+
112+
113+
def generateCompleters(commands):
114+
tmplt = ""
115+
txt = []
116+
117+
with open("templates/command.tmplt") as fp:
118+
tmplt = fp.read()
119+
120+
for commandKey in commands:
121+
txt.append(pystache.render(tmplt, commands[commandKey]))
122+
123+
# if need to add hacks add them here
124+
125+
return txt
126+
127+
128+
def generateBoilerPlate(commands):
129+
tmplt = ""
130+
txt = []
131+
132+
with open("templates/boilerplate.tmplt") as fp:
133+
txt.append(fp.read())
134+
135+
return txt
136+
137+
138+
def generateScript(commands):
139+
txt = []
140+
141+
txt.extend(generateBoilerPlate(commands))
142+
txt.extend(generateCompleters(commands))
143+
txt.extend(generateMain(commands))
144+
145+
with open("mbed-completion", "w") as fp:
146+
for x in txt:
147+
fp.write("%s\n" % x)
148+
97149

98150
if __name__ == '__main__':
99151
commands = parseCommands()
100152

101-
generateMain(commands)
153+
generateScript(commands)
102154

103155
# At this point we have a list of all the commands and sub commands
104156
# for each command create a Bash function
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Note this file is generated
2+
# Based on mbed auto complete scripts
3+
4+
__mbedcomp_words_include() {
5+
local i=1
6+
while [[ "$i" -lt "$COMP_CWORD" ]]
7+
do
8+
if [[ "${COMP_WORDS[i]}" = "$1" ]]
9+
then
10+
return 0
11+
fi
12+
i="$((++i))"
13+
done
14+
return 1
15+
}
16+
17+
# Find the previous non-switch word
18+
__mbedcomp_prev() {
19+
local idx="$((COMP_CWORD - 1))"
20+
local prv="${COMP_WORDS[idx]}"
21+
while [[ "$prv" = -* ]]
22+
do
23+
idx="$((--idx))"
24+
prv="${COMP_WORDS[idx]}"
25+
done
26+
echo "$prv"
27+
}
28+
29+
__mbedcomp() {
30+
# break $1 on space, tab, and newline characters,
31+
# and turn it into a newline separated list of words
32+
local list s sep=$'\n' IFS=$' '$'\t'$'\n'
33+
local cur="${COMP_WORDS[COMP_CWORD]}"
34+
35+
for s in $1
36+
do
37+
__mbedcomp_words_include "$s" && continue
38+
list="$list$s$sep"
39+
done
40+
41+
IFS="$sep"
42+
COMPREPLY=($(compgen -W "$list" -- "$cur"))
43+
}

0 commit comments

Comments
 (0)