From 6ed041c520b0d2d582777ae1106519ed5c2cd265 Mon Sep 17 00:00:00 2001 From: Chris Lalancette Date: Tue, 6 Aug 2024 06:50:35 -0400 Subject: [PATCH] Use an rclpy context manager. (#312) This makes the code cleaner and ensures we cleanup no matter how we exit. Signed-off-by: Chris Lalancette --- .../src/rqt_py_common/topic_completer.py | 100 +++++++++--------- rqt_py_common/src/rqt_py_common/topic_dict.py | 16 +-- 2 files changed, 60 insertions(+), 56 deletions(-) diff --git a/rqt_py_common/src/rqt_py_common/topic_completer.py b/rqt_py_common/src/rqt_py_common/topic_completer.py index d88dfa2f..3156a186 100644 --- a/rqt_py_common/src/rqt_py_common/topic_completer.py +++ b/rqt_py_common/src/rqt_py_common/topic_completer.py @@ -95,52 +95,54 @@ def update_topics(self, node): QTreeView, QVBoxLayout, QWidget import rclpy - rclpy.init() - topic_completer_node = rclpy.create_node() - - app = QApplication(sys.argv) - mw = QMainWindow() - widget = QWidget(mw) - layout = QVBoxLayout(widget) - - edit = QLineEdit() - edit_completer = TopicCompleter(edit) - edit_completer.update_topics(topic_completer_node) - # edit_completer.setCompletionMode(QCompleter.InlineCompletion) - edit.setCompleter(edit_completer) - - combo = QComboBox() - combo.setEditable(True) - combo_completer = TopicCompleter(combo) - combo_completer.update_topics(topic_completer_node) - - # combo_completer.setCompletionMode(QCompleter.InlineCompletion) - combo.lineEdit().setCompleter(combo_completer) - - model_tree = QTreeView() - model_tree.setModel(combo_completer.model()) - model_tree.expandAll() - for column in range(combo_completer.model().columnCount()): - model_tree.resizeColumnToContents(column) - - completion_tree = QTreeView() - completion_tree.setModel(combo_completer.completionModel()) - completion_tree.expandAll() - for column in range(combo_completer.completionModel().columnCount()): - completion_tree.resizeColumnToContents(column) - - layout.addWidget(model_tree) - layout.addWidget(completion_tree) - layout.addWidget(edit) - layout.addWidget(combo) - layout.setStretchFactor(model_tree, 1) - widget.setLayout(layout) - mw.setCentralWidget(widget) - - mw.move(300, 0) - mw.resize(800, 900) - mw.show() - app.exec_() - - topic_completer_node.destroy_node() - rclpy.shutdown() + from rclpy.executors import ExternalShutdownException + + try: + with rclpy.init(): + topic_completer_node = rclpy.create_node() + + app = QApplication(sys.argv) + mw = QMainWindow() + widget = QWidget(mw) + layout = QVBoxLayout(widget) + + edit = QLineEdit() + edit_completer = TopicCompleter(edit) + edit_completer.update_topics(topic_completer_node) + # edit_completer.setCompletionMode(QCompleter.InlineCompletion) + edit.setCompleter(edit_completer) + + combo = QComboBox() + combo.setEditable(True) + combo_completer = TopicCompleter(combo) + combo_completer.update_topics(topic_completer_node) + + # combo_completer.setCompletionMode(QCompleter.InlineCompletion) + combo.lineEdit().setCompleter(combo_completer) + + model_tree = QTreeView() + model_tree.setModel(combo_completer.model()) + model_tree.expandAll() + for column in range(combo_completer.model().columnCount()): + model_tree.resizeColumnToContents(column) + + completion_tree = QTreeView() + completion_tree.setModel(combo_completer.completionModel()) + completion_tree.expandAll() + for column in range(combo_completer.completionModel().columnCount()): + completion_tree.resizeColumnToContents(column) + + layout.addWidget(model_tree) + layout.addWidget(completion_tree) + layout.addWidget(edit) + layout.addWidget(combo) + layout.setStretchFactor(model_tree, 1) + widget.setLayout(layout) + mw.setCentralWidget(widget) + + mw.move(300, 0) + mw.resize(800, 900) + mw.show() + app.exec_() + except (KeyboardInterrupt, ExternalShutdownException): + pass diff --git a/rqt_py_common/src/rqt_py_common/topic_dict.py b/rqt_py_common/src/rqt_py_common/topic_dict.py index 765d10ad..859d6cf7 100644 --- a/rqt_py_common/src/rqt_py_common/topic_dict.py +++ b/rqt_py_common/src/rqt_py_common/topic_dict.py @@ -28,8 +28,6 @@ # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. -import rclpy - from rqt_py_common.message_helpers import get_message_class @@ -78,8 +76,12 @@ def _recursive_create_field_dict(self, field_name, field): if __name__ == '__main__': import pprint - rclpy.init() - topic_dict_node = rclpy.create_node('topic_dict') - pprint.pprint(TopicDict(node=topic_dict_node).get_topics()) - topic_dict_node.destroy_node() - rclpy.shutdown() + import rclpy + from rclpy.executors import ExternalShutdownException + + try: + with rclpy.init(): + topic_dict_node = rclpy.create_node('topic_dict') + pprint.pprint(TopicDict(node=topic_dict_node).get_topics()) + except (KeyboardInterrupt, ExternalShutdownException): + pass