Skip to content

Commit b909d92

Browse files
committed
add utils.to_ros_base_name() helper function, allow utils.anon(1)
1 parent 7f382c1 commit b909d92

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

src/roslaunch2/utils.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ def id_gen(size, chars=string.ascii_uppercase + string.ascii_lowercase + string.
3636
"""
3737
return ''.join(random.choice(chars) for _ in range(size))
3838

39-
if length <= 1:
40-
raise NameError("ROS names should be longer than 1 character.")
39+
#if length <= 1:
40+
# raise NameError("ROS names should be longer than 1 character.")
4141
return "{0}{1}".format(id_gen(1, chars=string.ascii_uppercase + string.ascii_lowercase), id_gen(length - 1))
4242

4343

@@ -119,6 +119,27 @@ def ros_join(left, right, force_global=False):
119119
return ROS_NAME_SEP + res if force_global and not res.startswith(ROS_NAME_SEP) else res
120120

121121

122+
def to_ros_base_name(data, replacement_begin=anon(1), replacement_other='_'):
123+
"""
124+
Converts the given data to a valid ROS base (!) name.
125+
:param data: string to be converted
126+
:param replacement_begin: Replacement string for the beginning (if a replacement is necessary)
127+
:param replacement_other: Replacement for subsequent invalid characters (if any)
128+
:return: Possibly modified valid ROS base name, given data as input
129+
"""
130+
result = ""
131+
if not data[0].isalpha():
132+
result = replacement_begin
133+
else:
134+
result = data[0]
135+
for i in range(1, len(data)):
136+
if not data[i].isalnum():
137+
result += replacement_other
138+
else:
139+
result += data[i]
140+
return result
141+
142+
122143
class Observable(object):
123144
"""
124145
Allows to create observable events (like the termination of roslaunch) to register custom actions in case such

0 commit comments

Comments
 (0)