diff --git a/ros2topic/ros2topic/eval/__init__.py b/ros2topic/ros2topic/eval/__init__.py index 47863c1ed..faebb2ae8 100644 --- a/ros2topic/ros2topic/eval/__init__.py +++ b/ros2topic/ros2topic/eval/__init__.py @@ -1,24 +1,22 @@ -# MIT License - -# Copyright (c) 2022 Yaroslav Polyakov - +# Copyright 2022 Yaroslav Polyakov +# # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: - -# The above copyright notice and this permission notice shall be included in all -# copies or substantial portions of the Software. - +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -# SOFTWARE. +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. """Safe user-supplied python expression evaluation.""" @@ -131,6 +129,7 @@ def generic_visit(self, node): class Expr(): + def __init__(self, expr, model=None, filename=None): self.expr = expr diff --git a/ros2topic/ros2topic/verb/hz.py b/ros2topic/ros2topic/verb/hz.py index 21fe5bb99..bf418480f 100644 --- a/ros2topic/ros2topic/verb/hz.py +++ b/ros2topic/ros2topic/verb/hz.py @@ -96,6 +96,7 @@ def _get_nested_messages(msg_class): all_attributes.extend(nested_messages) return all_attributes + def _setup_base_safe_eval(): safe_eval_model = base_eval_model.clone() @@ -117,6 +118,7 @@ def _setup_base_safe_eval(): safe_eval_model.allowed_functions.extend(safe_builtins) return safe_eval_model + def _setup_safe_eval(safe_eval_model, msg_class, topic): # allow-list topic builtins, msg attributes topic_builtins = [i for i in dir(topic) if not i.startswith('_')] @@ -129,27 +131,24 @@ def _setup_safe_eval(safe_eval_model, msg_class, topic): def main(args): with DirectNode(args) as node: - topics = args.topic_name + topic = args.topic_name filter_expr = None # set up custom safe eval model for filter expression if args.filter_expr: safe_eval_model = _setup_base_safe_eval() - for topic in topics: - msg_class = get_msg_class( - node, topic, blocking=True, include_hidden_topics=True) - if msg_class is None: - continue - + msg_class = get_msg_class( + node, topic, blocking=True, include_hidden_topics=True) + if msg_class is not None: safe_eval_model = _setup_safe_eval(safe_eval_model, msg_class, topic) - def expr_eval(expr): - def eval_fn(m): - safe_expression = Expr(expr, model=safe_eval_model) - return eval(safe_expression.code) - return eval_fn - filter_expr = expr_eval(args.filter_expr) + def expr_eval(expr): + def eval_fn(m): + safe_expression = Expr(expr, model=safe_eval_model) + return eval(safe_expression.code) + return eval_fn + filter_expr = expr_eval(args.filter_expr) - _rostopic_hz(node.node, topics, qos_args=args, window_size=args.window_size, + _rostopic_hz(node.node, topic, window_size=args.window_size, filter_expr=filter_expr, use_wtime=args.use_wtime)