Skip to content

Commit

Permalink
Merge pull request #66 from ament/more_operators
Browse files Browse the repository at this point in the history
support more operators in conditions
  • Loading branch information
dirk-thomas authored Dec 4, 2017
2 parents 18a1021 + 2d98c21 commit a51d9b7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
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

0 comments on commit a51d9b7

Please sign in to comment.