Skip to content

Commit

Permalink
Add in support for the version tag. (#52) (#54)
Browse files Browse the repository at this point in the history
* Add in support for the version tag.

Cherry-picked from the melodic-devel branch commit
c0a75a2

Signed-off-by: Chris Lalancette <[email protected]>
  • Loading branch information
clalancette authored Mar 10, 2020
1 parent 04be157 commit 8f8a28e
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 15 deletions.
20 changes: 19 additions & 1 deletion src/urdf_parser_py/urdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,23 @@ def get_root(self):
assert root is not None, "No roots detected, invalid URDF."
return root

def post_read_xml(self):
if self.version is None:
self.version = "1.0"

split = self.version.split(".")
if len(split) != 2:
raise ValueError("The version attribute should be in the form 'x.y'")

if split[0] == '' or split[1] == '':
raise ValueError("Empty major or minor number is not allowed")

if int(split[0]) < 0 or int(split[1]) < 0:
raise ValueError("Version number must be positive")

if self.version != "1.0":
raise ValueError("Invalid version; only 1.0 is supported")

@classmethod
def from_parameter_server(cls, key='robot_description'):
"""
Expand All @@ -547,7 +564,8 @@ def from_parameter_server(cls, key='robot_description'):


xmlr.reflect(Robot, tag='robot', params=[
xmlr.Attribute('name', str, False), # Is 'name' a required attribute?
xmlr.Attribute('name', str),
xmlr.Attribute('version', str, False),
xmlr.AggregateElement('link', Link),
xmlr.AggregateElement('joint', Joint),
xmlr.AggregateElement('gazebo', xmlr.RawType()),
Expand Down
99 changes: 85 additions & 14 deletions test/test_urdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def parse_and_compare(self, orig):

def test_new_transmission(self):
xml = '''<?xml version="1.0"?>
<robot name="test">
<robot name="test" version="1.0">
<transmission name="simple_trans">
<type>transmission_interface/SimpleTransmission</type>
<joint name="foo_joint">
Expand All @@ -48,7 +48,7 @@ def test_new_transmission(self):

def test_new_transmission_multiple_joints(self):
xml = '''<?xml version="1.0"?>
<robot name="test">
<robot name="test" version="1.0">
<transmission name="simple_trans">
<type>transmission_interface/SimpleTransmission</type>
<joint name="foo_joint">
Expand All @@ -67,7 +67,7 @@ def test_new_transmission_multiple_joints(self):

def test_new_transmission_multiple_actuators(self):
xml = '''<?xml version="1.0"?>
<robot name="test">
<robot name="test" version="1.0">
<transmission name="simple_trans">
<type>transmission_interface/SimpleTransmission</type>
<joint name="foo_joint">
Expand All @@ -83,28 +83,28 @@ def test_new_transmission_multiple_actuators(self):

def test_new_transmission_missing_joint(self):
xml = '''<?xml version="1.0"?>
<robot name="test">
<robot name="test" version="1.0">
<transmission name="simple_trans">
<type>transmission_interface/SimpleTransmission</type>
</transmission>
</robot>'''
self.assertRaises(Exception, self.parse, xml)
self.assertRaises(xmlr.core.ParseError, self.parse, xml)

def test_new_transmission_missing_actuator(self):
xml = '''<?xml version="1.0"?>
<robot name="test">
<robot name="test" version="1.0">
<transmission name="simple_trans">
<type>transmission_interface/SimpleTransmission</type>
<joint name="foo_joint">
<hardwareInterface>EffortJointInterface</hardwareInterface>
</joint>
</transmission>
</robot>'''
self.assertRaises(Exception, self.parse, xml)
self.assertRaises(xmlr.core.ParseError, self.parse, xml)

def test_old_transmission(self):
xml = '''<?xml version="1.0"?>
<robot name="test">
<robot name="test" version="1.0">
<transmission name="PR2_trans" type="SimpleTransmission">
<joint name="foo_joint"/>
<actuator name="foo_motor"/>
Expand All @@ -115,7 +115,7 @@ def test_old_transmission(self):

def test_link_material_missing_color_and_texture(self):
xml = '''<?xml version="1.0"?>
<robot name="test">
<robot name="test" version="1.0">
<link name="link">
<visual>
<geometry>
Expand All @@ -129,7 +129,7 @@ def test_link_material_missing_color_and_texture(self):

def test_robot_material(self):
xml = '''<?xml version="1.0"?>
<robot name="test">
<robot name="test" version="1.0">
<material name="mat">
<color rgba="0.0 0.0 0.0 1.0"/>
</material>
Expand All @@ -138,14 +138,14 @@ def test_robot_material(self):

def test_robot_material_missing_color_and_texture(self):
xml = '''<?xml version="1.0"?>
<robot name="test">
<robot name="test" version="1.0">
<material name="mat"/>
</robot>'''
self.assertRaises(ParseException, self.parse, xml)
self.assertRaises(xmlr.core.ParseError, self.parse, xml)

def test_link_multiple_visual(self):
xml = '''<?xml version="1.0"?>
<robot name="test">
<robot name="test" version="1.0">
<link name="link">
<visual>
<geometry>
Expand All @@ -165,7 +165,7 @@ def test_link_multiple_visual(self):

def test_link_multiple_collision(self):
xml = '''<?xml version="1.0"?>
<robot name="test">
<robot name="test" version="1.0">
<link name="link">
<collision>
<geometry>
Expand All @@ -181,6 +181,77 @@ def test_link_multiple_collision(self):
</robot>'''
self.parse_and_compare(xml)

def test_version_attribute_not_enough_dots(self):
xml = '''<?xml version="1.0"?>
<robot name="test" version="1">
</robot>'''
self.assertRaises(ValueError, self.parse, xml)

def test_version_attribute_too_many_dots(self):
xml = '''<?xml version="1.0"?>
<robot name="test" version="1.0.0">
</robot>'''
self.assertRaises(ValueError, self.parse, xml)

def test_version_attribute_not_enough_numbers(self):
xml = '''<?xml version="1.0"?>
<robot name="test" version="1.">
</robot>'''
self.assertRaises(ValueError, self.parse, xml)

def test_version_attribute_no_major_number(self):
xml = '''<?xml version="1.0"?>
<robot name="test" version=".0">
</robot>'''
self.assertRaises(ValueError, self.parse, xml)

def test_version_attribute_negative_major_number(self):
xml = '''<?xml version="1.0"?>
<robot name="test" version="-1.0">
</robot>'''
self.assertRaises(ValueError, self.parse, xml)

def test_version_attribute_negative_minor_number(self):
xml = '''<?xml version="1.0"?>
<robot name="test" version="1.-0">
</robot>'''
self.assertRaises(ValueError, self.parse, xml)

def test_version_attribute_dots_no_numbers(self):
xml = '''<?xml version="1.0"?>
<robot name="test" version="a.c">
</robot>'''
self.assertRaises(ValueError, self.parse, xml)

def test_version_attribute_dots_one_number(self):
xml = '''<?xml version="1.0"?>
<robot name="test" version="1.c">
</robot>'''
self.assertRaises(ValueError, self.parse, xml)

def test_version_attribute_trailing_junk(self):
xml = '''<?xml version="1.0"?>
<robot name="test" version="1.0~pre6">
</robot>'''
self.assertRaises(ValueError, self.parse, xml)

def test_version_attribute_correct(self):
xml = '''<?xml version="1.0"?>
<robot name="test" version="1.0">
</robot>'''
self.parse_and_compare(xml)

def test_version_attribute_invalid(self):
xml = '''<?xml version="1.0"?>
<robot name="test" version="foo">
</robot>'''
self.assertRaises(ValueError, self.parse, xml)

def test_version_attribute_invalid_version(self):
xml = '''<?xml version="1.0"?>
<robot name="test" version="2.0">
</robot>'''
self.assertRaises(ValueError, self.parse, xml)

class LinkOriginTestCase(unittest.TestCase):
@mock.patch('urdf_parser_py.xml_reflection.on_error',
Expand Down

0 comments on commit 8f8a28e

Please sign in to comment.