-
Notifications
You must be signed in to change notification settings - Fork 0
/
easylaunch.py
226 lines (182 loc) · 9.54 KB
/
easylaunch.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# EasyLaunch python module
# Written by James Usevitch
# Aerospace Department, University of Michigan
# Overall structure:
# 1. XML Version at top
# 2. launchFile class
# -- dictionary of args with default values
# -- dictionary of remaps
# -- array of include classes
# -- array of node classes
# 3. include class
# -- Attributes:
# > .file
# > .namespace
# -- array of args which use the default value
# -- dict of args which use custom values
# 4. node class
# -- Attributes:
# > .name
# > .package
# > .type
# > .launch-prefix
# > .output
# > .namespace
# -- array of parameters which use default arg values
# -- dict of parameters which use custom values
# -- dict of rosparam values which should be represented as <rosparam param="name"> text </rosparam>
# 5. Group class?
# !!! USE THE KEYWORDS USED IN THE XML FILES! (E.g. use pkg for package, ns for namespace, etc)
import copy
import xml.etree.ElementTree as ET
from xml.dom import minidom
class launchFile:
def __init__(self, arg={}, remap={}, include=[], node=[]):
self.arg = arg # Dictionary of {name: default} pairs
self.remap = remap
self.include = include
self.node = node
# Source for prettify function: https://bip.weizmann.ac.il/course/python/PyMOTW/PyMOTW/docs/xml/etree/ElementTree/create.html
# Link works as of 4/20/19
def prettify(self, element):
# Returns an XML string with 'pretty' formatting
# You can print the string to screen or to file
rough_string = ET.tostring(element, 'utf-8')
reparsed = minidom.parseString(rough_string)
return reparsed.toprettyxml(indent=" ")
# Creates a
def write(self, filename="./launchFile.launch", prettyprint=True, verbose=False):
launch = self.getLaunchElement(verbose)
if prettyprint:
print(self.prettify(launch), file=open(filename, "w"))
else:
print('<?xml version="1.0"?>', file=open(filename, "w"))
print(ET.tostring(launch).decode('UTF-8'), file=open(filename, "a"))
# Prints a preview of the launch file to screen
def print(self, verbose=False):
launch = self.getLaunchElement(verbose)
print(self.prettify(launch))
def getLaunchElement(self, verbose=False): # To do: put output text inside if statements controlled by verbose bool
launch = ET.Element("launch")
## Create arg elenents
if type(self.arg) is dict and len(self.arg) > 0:
arg_keys = list(self.arg.keys())
for i in range(len(arg_keys)):
ET.SubElement(launch, 'arg', {'name': arg_keys[i], 'default': self.arg[arg_keys[i]]})
else:
print('No default arg elements / Invalid arg elements.')
## Create include elements
if self.include is not None and len(self.include) > 0:
# Iterate through each include element
for i in range(len(self.include)):
# Set the file and ns attributes
temp_include = ET.SubElement(launch, 'include', {'file': self.include[i].file})
if self.include[i].ns is not None:
temp_include.set('ns', self.include[i].ns)
# Set the arg elements with default values
if self.include[i].defarg is not None and len(self.include[i].defarg) > 0:
for j in range(len(self.include[i].defarg)):
if self.include[i].defarg[j] in list(self.arg.keys()):
ET.SubElement(temp_include, 'arg', {'name': self.include[i].defarg[j], 'value': '$(arg ' + self.include[i].defarg[j] + ')'})
else:
print('Default arg ' + self.include[i].defarg[j] + ' not listed in default launchfile.arg dict')
else:
print('Default args are "None" or empty')
# Set the arg elements which are not default values
if self.include[i].arg is not None and len(self.include[i].arg) > 0:
temp_keys = list(self.include[i].arg.keys())
for k in range(len(temp_keys)):
ET.SubElement(temp_include, 'arg', {'name': temp_keys[k], 'value': self.include[i].arg[temp_keys[k]]})
else:
print('No include elements.')
## Create node elements
if len(self.node) > 0:
for i in range(len(self.node)):
temp_node = ET.SubElement(launch, 'node', {'name': self.node[i].name, 'pkg': self.node[i].pkg, 'type': self.node[i].type})
if self.node[i].args is not None:
temp_node.set('args', self.node[i].args)
if self.node[i].launch_prefix is not None:
# Note the difference between 'launch-prefix' (XML) and 'launch_prefix' (Python, which doesn't allow the dash in variable names)
temp_node.set('launch-prefix', self.node[i].launch_prefix)
if self.node[i].output is not None:
temp_node.set('output', self.node[i].output)
if self.node[i].ns is not None:
temp_node.set('ns', self.node[i].ns)
# Set the default param elements
if self.node[i].defparam is not None and len(self.node[i].defparam) > 0:
for j in range(len(self.node[i].defparam)):
if self.node[i].defparam[j] in list(self.arg.keys()):
ET.SubElement(temp_node, 'param', {'name': self.node[i].defparam[j], 'value': '$(arg ' + self.node[i].defparam[j] + ')'})
else:
print('Default param ' + self.node[i].defparam[j] + ' not listed in default launchfile.arg dict')
else:
print('Default params are "None" or empty')
# Set the param elements which are not default values
if self.node[i].param is not None and len(self.node[i].param) > 0:
temp_keys = list(self.node[i].param.keys())
for k in range(len(temp_keys)):
ET.SubElement(temp_node, 'param', {'name': temp_keys[k], 'value': self.node[i].param[temp_keys[k]]})
# Set the rosparam elements
if self.node[i].rosparam is not None and len(self.node[i].rosparam) > 0:
temp_keys = list(self.node[i].rosparam.keys())
for k in range(len(temp_keys)):
temp_element = ET.SubElement(temp_node, 'rosparam', {'param': temp_keys[k]})
temp_element.text = self.node[i].rosparam[temp_keys[k]]
else:
print('No (standalone) node elements. (Other nodes may be inside include files)')
return launch
# def print(self):
# Print to the screen as preview
class include:
def __init__(self, file=None, ns=None, defarg=[], arg={}):
self.file = file
self.ns = ns
self.defarg = defarg # Array of strings
self.arg = arg # Dict
def copy(self, number_copies=1, ns_array=None):
if self.ns is None:
include_list = [copy.deepcopy(self) for i in range(number_copies)]
return include_list
elif ns_array is None:
include_list = [copy.deepcopy(self) for i in range(number_copies)]
for i in range(number_copies):
include_list[i].ns = self.ns + str(i + 1)
return include_list
elif number_copies <= len(ns_array):
if number_copies < len(ns_array):
print('WARNING: number_copies < len(ns_array) for copy operation involving the include ' + self.file + '. Not all namespaces will be used.')
include_list = [copy.deepcopy(self) for i in range(number_copies)]
for i in range(number_copies):
include_list[i].ns = ns_array[i]
return include_list
elif number_copies > len(ns_array):
raise ValueError('Value of "number_copies" is greater than length of "ns_array". Aborting.')
else:
raise ValueError('Something really bad happened. Check your ns_array variable.')
class node:
def __init__(self, name, pkg, type, args=None, launch_prefix=None, output=None, ns=None, defparam=[], param={}, rosparam={}):
self.name = name
self.pkg = pkg
self.type = type
self.args = args
self.launch_prefix = launch_prefix
self.output = output
self.ns = ns
self.defparam = defparam # Array of strings
self.param = param # Dict
self.rosparam = rosparam # Dict
def copy(self, number_copies=1, name_array=None):
if name_array is None:
node_list = [copy.deepcopy(self) for i in range(number_copies)]
for j in range(number_copies):
node_list[j].name = self.name + str(j + 1)
return node_list
elif number_copies <= len(name_array):
if number_copies < len(name_array):
print('WARNING: number_copies < len(name_array) for copy operation involving the node ' + self.name + '. Not all names will be used.')
node_list = [copy.deepcopy(self) for i in range(number_copies)]
for i in range(number_copies):
node_list[i].name = name_array[i]
return node_list
else:
raise ValueError('Value of "number_copies" is greater than length of "name_array". Aborting.')