Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
athackst committed Feb 14, 2020
1 parent 7f77970 commit 00869ba
Showing 1 changed file with 36 additions and 25 deletions.
61 changes: 36 additions & 25 deletions ros1_bridge/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,10 @@ def is_field_mapping(self):
return self.fields_1_to_2 is not None

def __str__(self):
return 'MessageMappingRule(%s::%s <-> %s::%s)' % (self.ros1_package_name, self.ros1_message_name, self.ros2_package_name, self.ros2_message_name)
return 'MessageMappingRule(%s::%s <-> %s::%s)' % (self.ros1_package_name,
self.ros1_message_name,
self.ros2_package_name,
self.ros2_message_name)


class ServiceMappingRule(MappingRule):
Expand Down Expand Up @@ -458,7 +461,10 @@ def __init__(self, data, expected_package_name):
'Mapping for package %s contains unknown field(s)' % self.ros2_package_name)

def __str__(self):
return 'ServiceMappingRule(%s::%s <-> %s::%s)' % (self.ros1_package_name, self.ros1_service_name, self.ros2_package_name, self.ros2_service_name)
return 'ServiceMappingRule(%s::%s <-> %s::%s)' % (self.ros1_package_name,
self.ros1_service_name,
self.ros2_package_name,
self.ros2_service_name)


def determine_package_pairs(ros1_msgs, ros2_msgs, mapping_rules):
Expand Down Expand Up @@ -536,21 +542,23 @@ def determine_message_pairs(ros1_msgs, ros2_msgs, package_pairs, mapping_rules):

return pairs


def srv_field_mapping(ros_type, ros1_name, ros2_name):
return {
'basic': False if '/' in ros_type else True,
'array': True if '[]' in ros_type else False,
'ros1': {
'name': ros1_name,
'type': ros_type.rstrip('[]'),
'cpptype': ros_type.rstrip('[]').replace('/', '::')
},
'ros2': {
'name': ros2_name,
'type': ros_type.rstrip('[]'),
'cpptype': ros_type.rstrip('[]').replace('/', '::msg::')
}
'basic': False if '/' in ros_type else True,
'array': True if '[]' in ros_type else False,
'ros1': {
'name': ros1_name,
'type': ros_type.rstrip('[]'),
'cpptype': ros_type.rstrip('[]').replace('/', '::')
},
'ros2': {
'name': ros2_name,
'type': ros_type.rstrip('[]'),
'cpptype': ros_type.rstrip('[]').replace('/', '::msg::')
}
}


def determine_common_services(ros1_srvs, ros2_srvs, mapping_rules):
services = []
Expand All @@ -574,7 +582,7 @@ def determine_common_services(ros1_srvs, ros2_srvs, mapping_rules):
else:
print("No matching ros2 srv for %s" % str(ros1_srv), file=sys.stdout)
continue

# fill in missing fields if they match
for rule in mapping_rules:
ros1_srv = Message(rule.ros1_package_name, rule.ros1_service_name)
Expand All @@ -587,8 +595,8 @@ def determine_common_services(ros1_srvs, ros2_srvs, mapping_rules):
'response': ros1_spec.response.fields()
}
ros2_fields = {
'request': [(str(field.type), field.name) for field in ros2_spec.request.fields],
'response': [(str(field.type), field.name) for field in ros2_spec.response.fields]
'request': [(str(field.type), field.name) for field in ros2_spec.request.fields],
'response': [(str(field.type), field.name) for field in ros2_spec.response.fields]
}
rule_fields = {
'request': rule.request_fields_1_to_2,
Expand All @@ -599,29 +607,32 @@ def determine_common_services(ros1_srvs, ros2_srvs, mapping_rules):
'response': []
}
for direction in ['request', 'response']:
if(len(ros1_fields[direction])!= len(ros2_fields[direction])):
match=False
print("ros1 and ros2 %s fields must have matching length %s" % (direction, rule), file=sys.stderr)
if(len(ros1_fields[direction]) != len(ros2_fields[direction])):
match = False
print("ros1 and ros2 %s fields must have matching length %s" %
(direction, rule), file=sys.stderr)
continue
for (ros_type, ros1_name) in ros1_fields[direction]:
# if a rule exists for this item, add in the mapping
if (ros1_name in rule_fields[direction]):
if (rule_fields[direction] and ros1_name in rule_fields[direction]):
ros2_name = rule_fields[direction][ros1_name]

if ((ros_type, ros2_name) in ros2_fields[direction]):
output[direction].append(srv_field_mapping(ros_type, ros1_name, ros2_name))
else:
print("Invalid rule field pair for %s<-->%s in %s" % (ros1_name, ros2_name, rule), file=sys.stderr)
match=False
print("Invalid rule field pair for %s<-->%s in %s" %
(ros1_name, ros2_name, rule), file=sys.stderr)
match = False
continue
# A rule doesn't exist for this item, try to match by name
else:
ros2_name = ros1_name
if ((ros_type, ros2_name) in ros2_fields[direction]):
output[direction].append(srv_field_mapping(ros_type, ros1_name, ros2_name))
else:
print("Invalid matching field pair for %s<-->%s in %s" % (ros1_name, ros2_name, rule), file=sys.stderr)
match=False
print("Invalid matching field pair for %s<-->%s in %s" %
(ros1_name, ros2_name, rule), file=sys.stderr)
match = False
continue
if match:
services.append({
Expand Down

0 comments on commit 00869ba

Please sign in to comment.