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

support more operators in conditions #66

Merged
merged 1 commit into from
Dec 4, 2017
Merged
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
22 changes: 14 additions & 8 deletions ament_package/condition.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import operator
import pyparsing


Expand All @@ -34,7 +35,7 @@ def _get_condition_expression():
global _condition_expression
if not _condition_expression:
pp = pyparsing
operator = pp.Regex('==|!=').setName('operator')
operator = pp.Regex('==|!=|>=|>|<=|<').setName('operator')
identifier = pp.Word('$', pp.alphanums + '_', min=2)
value = pp.Word(pp.alphanums + '_-')
comparison_term = identifier | value
Expand Down Expand Up @@ -67,10 +68,15 @@ def _evaluate(parse_results, context):
_evaluate(parse_results[2], context)

# handle comparison operators
assert parse_results[1] in ('==', '!=')
if parse_results[1] == '==':
return _evaluate(parse_results[0], context) == \
_evaluate(parse_results[2], context)
if parse_results[1] == '!=':
return _evaluate(parse_results[0], context) != \
_evaluate(parse_results[2], context)
operators = {
'==': operator.eq,
'!=': operator.ne,
'<=': operator.le,
'<': operator.lt,
'>=': operator.ge,
'>': operator.gt,
}
assert parse_results[1] in operators.keys()
return operators[parse_results[1]](
_evaluate(parse_results[0], context),
_evaluate(parse_results[2], context))
6 changes: 6 additions & 0 deletions test/test_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ def test_init_dependency(self):
self.assertTrue(dep.evaluated_condition)
self.assertRaises(TypeError, Dependency, 'foo', unknownattribute=42)

dep = Dependency('foo', condition='foo > bar and bar < baz')
self.assertTrue(dep.evaluate_condition({}))

dep = Dependency('foo', condition='foo <= bar or bar >= baz')
self.assertFalse(dep.evaluate_condition({}))

def test_init_kwargs_string(self):
pack = Package(filename='foo',
name='bar',
Expand Down