Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide support for yaml constructors !degrees and !radians [noetic] #257

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/xacro/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,30 @@ def __getattr__(self, item):

__getitem__ = __getattr__


def construct_angle_radians(loader, node):
"""utility function to construct radian values from yaml"""
value = loader.construct_scalar(node).strip()
try:
return float(eval(value, global_symbols, global_symbols))
except SyntaxError as e:
raise XacroException("invalid expression: %s" % value)


def construct_angle_degrees(loader, node):
"""utility function for converting degrees into radians from yaml"""
value = loader.construct_scalar(node)
try:
return math.radians(float(value))
except ValueError:
raise XacroException("invalid degree value: %s" % value)


def load_yaml(filename):
try:
import yaml
yaml.SafeLoader.add_constructor(u'!radians', construct_angle_radians)
yaml.SafeLoader.add_constructor(u'!degrees', construct_angle_degrees)
except Exception:
raise XacroException("yaml support not available; install python-yaml")

Expand Down
3 changes: 3 additions & 0 deletions test/constructors.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
a: !degrees 180
b: !radians 0.5*pi
c: 42
1 change: 1 addition & 0 deletions test/constructors_bad1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a: !radians os.system('clear')
1 change: 1 addition & 0 deletions test/constructors_bad2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a: !radians __import__('os').system('clear')
19 changes: 19 additions & 0 deletions test/test_xacro.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

import ast
from contextlib import contextmanager
import math
import os.path
import re
import shutil
Expand Down Expand Up @@ -1204,6 +1205,24 @@ def test_xacro_exist_optional(self):
res = '''<a></a>'''
self.assert_matches(self.quick_xacro(src), res)

def test_yaml_custom_constructors(self):
src = '''
<a xmlns:xacro="http://www.ros.org/wiki/xacro">
<xacro:property name="values" value="${load_yaml('constructors.yaml')}"/>
<values a="${values.a}" b="${values.b}" c="${values.c}"/>
</a>'''
res = '''<a><values a="{}" b="{}" c="42"/></a>'''.format(math.pi, 0.5*math.pi)
self.assert_matches(self.quick_xacro(src), res)

def test_yaml_custom_constructors_illegal_expr(self):
for file in ['constructors_bad1.yaml', 'constructors_bad2.yaml']:
src = '''
<a xmlns:xacro="http://www.ros.org/wiki/xacro">
<xacro:property name="values" value="${{load_yaml({file})}}"/>
<values a="${{values.a}}" />
</a>'''
self.assertRaises(xacro.XacroException, self.quick_xacro, src.format(file=file))

def test_macro_default_param_evaluation_order(self):
src = '''<a xmlns:xacro="http://www.ros.org/wiki/xacro">
<xacro:macro name="foo" params="arg:=${2*foo}">
Expand Down