Skip to content

Commit 0f65477

Browse files
committed
Converted file_format script to python and added command line auto-completion of paths/ros packages using autocomplete.py
1 parent 8a4c5b8 commit 0f65477

File tree

7 files changed

+166
-147
lines changed

7 files changed

+166
-147
lines changed

cfg/pep8.cfg

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[pep8]
2+
in-place = true
3+
max-line-length = 120

src/autocomplete.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env python
2+
3+
4+
class AutoComplete(object): # Custom completer
5+
6+
def __init__(self, options=[]):
7+
self.options = sorted(options)
8+
9+
def complete(self, text, state):
10+
if state == 0: # on first trigger, build possible matches
11+
if text: # cache matches (entries that start with entered text)
12+
self.matches = [s for s in self.options
13+
if s and s.startswith(text)]
14+
else: # no text entered, all matches possible
15+
self.matches = self.options[:]
16+
17+
# return match indexed by state
18+
try:
19+
return self.matches[state]
20+
except IndexError:
21+
return None
22+
23+
def set_options(self, options):
24+
self.options = sorted(options)

src/cpp_format

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ echo
44
echo "This is an auto-formatting script for .h/.cpp files."
55
echo
66

7-
./file-format 'cpp'
7+
rosrun ipa_code_refactoring file_format.py 'cpp'

src/file_format

Lines changed: 0 additions & 131 deletions
This file was deleted.

src/file_format.py

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#!/usr/bin/env python
2+
3+
from operator import itemgetter
4+
from sets import Set
5+
import re
6+
import sys
7+
import os.path
8+
import os
9+
import subprocess
10+
import rospkg
11+
import fnmatch
12+
import yaml
13+
import readline
14+
import glob
15+
from autocomplete import AutoComplete
16+
17+
# Function for user input completion of the current path
18+
def complete_path(text, state):
19+
return (glob.glob(text + '*') + [None])[state]
20+
21+
22+
# Important global variables
23+
rospack = rospkg.RosPack()
24+
25+
try:
26+
this_pkg_path = rospack.get_path('ipa_code_refactoring')
27+
except Exception, e:
28+
print "Unable to find ROS Package with name 'ipa_code_refactoring'."
29+
sys.exit()
30+
31+
# set autocompletion to ros packages
32+
try:
33+
autocomplete = AutoComplete(rospack.list())
34+
except Exception, e:
35+
print "Unable to get ROS Package List."
36+
sys.exit()
37+
38+
readline.set_completer_delims(' \t\n;')
39+
readline.set_completer(complete_path)
40+
readline.parse_and_bind('tab: complete')
41+
42+
43+
exec_policy = {'cpp': ' -name "*.h" -or -name "*.hpp" -or -name "*.cpp" | xargs rosrun ipa_code_refactoring clang-format -i -style=file', # CPP
44+
'py': ' -name "*.py" | xargs autopep8 --global-config ' + this_pkg_path + '/cfg/pep8.cfg'} # Python
45+
46+
if len(sys.argv) <= 1:
47+
print 'You need to provide a file type. Program shutting down now.'
48+
sys.exit()
49+
50+
file_type = sys.argv[1]
51+
52+
53+
print
54+
print "1) Do you want to \n\t[a] Format all files within the 'src' Folder \n\t[b] Format all files in certain ROS packages \n\t[c] Format all files within a repository"
55+
56+
user_input = raw_input("Choice: ")
57+
58+
if user_input in ['a', 'A']:
59+
print "\n2) Please enter the full path to your 'src' folder:"
60+
input_src_path = raw_input("Path: ")
61+
os.system('find ' + input_src_path + exec_policy.get(file_type))
62+
print "Formatted all files in: '" + input_src_path + "'\n"
63+
64+
elif user_input in ['b', 'B']:
65+
readline.set_completer(autocomplete.complete)
66+
print "\n2) Specify the ROS packages you want to format (quit by entering [q/Q]):"
67+
input_ros_pkg = raw_input("ROS Package: ")
68+
69+
while input_ros_pkg not in ['q', 'Q']:
70+
# try to find ros package by name
71+
try:
72+
ros_pkg_path = rospack.get_path(input_ros_pkg)
73+
except Exception, e:
74+
print "Can't find ROS Package with name '" + input_ros_pkg + "', please try another one."
75+
continue
76+
77+
# format all files according to the selected file type
78+
os.system('find ' + ros_pkg_path + exec_policy.get(file_type))
79+
print "Formatted all files in: '" + ros_pkg_path + "'\n"
80+
81+
# read in new ros package
82+
input_ros_pkg = raw_input("ROS Package: ")
83+
84+
elif user_input in ['c', 'C']:
85+
print "\n2) Please enter the full path to your 'src' folder:"
86+
input_src_path = raw_input("Path: ")
87+
if not os.path.isdir(input_src_path):
88+
print "Path does not exist. Program will shut down now."
89+
sys.exit()
90+
91+
autocomplete.set_options(next(os.walk(input_src_path))[1])
92+
readline.set_completer(autocomplete.complete)
93+
print "3) Specify the repositories you want to format (quit by entering [q/Q]):"
94+
input_repo_name = raw_input("Repository: ")
95+
96+
while input_repo_name not in ['q', 'Q']:
97+
if not os.path.isdir(input_src_path + '/' + input_repo_name):
98+
print "Path does not exist, please try again."
99+
else:
100+
# format all files according to the selected file type
101+
repo_path = (input_src_path + '/' + input_repo_name).replace('//', '/')
102+
os.system('find ' + repo_path + exec_policy.get(file_type))
103+
print "Formatted all files in: '" + repo_path + "'\n"
104+
105+
# read new repo
106+
input_repo_name = raw_input("Repository: ")
107+
108+
else:
109+
print "Error! Wrong character, program is shutting down now."
110+
111+
print "\nProgram is shutting down now."

src/param_update.py

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env python
22

3+
from autocomplete import AutoComplete
34
from operator import itemgetter
45
from sets import Set
56
import re
@@ -10,6 +11,7 @@
1011
import rospkg
1112
import fnmatch
1213
import yaml
14+
import readline
1315

1416

1517
def print_params(input_list):
@@ -233,7 +235,6 @@ def find_file(in_root_path, in_file_name):
233235

234236
# read config-yaml file
235237
this_cfg = {}
236-
print this_cfg_path
237238
if os.path.exists(this_cfg_path):
238239
with open(this_cfg_path, 'r') as stream:
239240
try:
@@ -255,28 +256,36 @@ def find_file(in_root_path, in_file_name):
255256

256257
editor = this_cfg.get('editor', 'subl')
257258

259+
# add autocompletion of ros packages
260+
try:
261+
autocomplete = AutoComplete(rospack.list())
262+
except Exception, e:
263+
print "Unable to get ROS Package List."
264+
sys.exit()
265+
266+
readline.set_completer_delims(' \t\n;')
267+
readline.set_completer(autocomplete.complete)
268+
readline.parse_and_bind('tab: complete')
269+
258270
# check if additional filename was provided,
259271
# otherwise take the package name as filename
260-
if len(sys.argv) <= 1:
261-
print 'You need to provide at least a package name. Program shutting down now.'
262-
sys.exit()
272+
print "\n1) Specify the name of the ROS package for parameter updates:"
273+
input_ros_pkg = raw_input("ROS Package: ")
263274

264-
pkg_name = sys.argv[1]
265-
if len(sys.argv) > 2:
266-
file_name = sys.argv[2]
267-
else:
268-
file_name = pkg_name
275+
print "\n1) Specify the name of the node if it differs from the ROS package name (optional):"
276+
input_node_name = raw_input("Node name: ")
269277

270-
old_names = []
271-
new_names = []
272-
zip_names = zip(old_names, new_names)
278+
if len(input_node_name) > 0:
279+
file_name = input_node_name
280+
else:
281+
file_name = input_ros_pkg
273282

274283

275284
# find path to ros package
276285
try:
277-
pkg_path = rospack.get_path(pkg_name)
286+
pkg_path = rospack.get_path(input_ros_pkg)
278287
except Exception, e:
279-
print "Can't find ROS Package with name '" + pkg_name + "'"
288+
print "Can't find ROS Package with name '" + input_ros_pkg + "'"
280289
sys.exit()
281290

282291
# find cpp, cfg and yaml files in pkg_path
@@ -295,6 +304,9 @@ def find_file(in_root_path, in_file_name):
295304
# list of methods for constructing lines for missing parameters
296305
extract_values = [extract_values_common, extract_values_ros, extract_values_cfg, extract_values_yaml]
297306

307+
old_names = []
308+
new_names = []
309+
zip_names = zip(old_names, new_names)
298310

299311
### Regular Expressions ###
300312

src/py_format

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ echo
1212
echo "This is an auto-formatting script for .py files."
1313
echo
1414

15-
./file-format 'py'
15+
rosrun ipa_code_refactoring file_format.py 'py'

0 commit comments

Comments
 (0)