MCPcopy Create free account
hub / github.com/awesomebytes/rclcppyy / convert_python_msgs_to_cpp

Function convert_python_msgs_to_cpp

rclcppyy/node.py:483–529  ·  view source on GitHub ↗

Convert all Python ROS messages in globals() to their C++ equivalents with keyword support. Args: target_globals: The globals dict to modify. If None, uses caller's globals().

(target_globals=None)

Source from the content-addressed store, hash-verified

481
482 except Exception as e:
483 print(f"Failed to convert {name}: {e}")
484 # Keep the original Python version if conversion fails
485 continue
486
487def setup_automatic_cpp_conversion():
488 """
489 Set up automatic conversion of Python ROS messages to C++ equivalents.
490 This sets up an import hook that will automatically convert messages during import.
491 """
492 import sys
493 import importlib.util
494 from importlib.abc import Loader, MetaPathFinder
495 from importlib.machinery import ModuleSpec
496
497 class ROSMessageConverter(MetaPathFinder):
498 """Import hook to automatically convert ROS messages to C++ equivalents"""
499
500 def __init__(self):
501 # Keep track of modules we're currently importing to avoid recursion
502 self._importing = set()
503 # Store the original loaders we wrap
504 self._original_loaders = {}
505
506 def find_spec(self, fullname, path, target=None):
507 """Called by Python's import system for each import statement"""
508
509 # Only intercept ROS message type imports (e.g. std_msgs.msg._string)
510 # The actual message types are in the _<message> modules
511 if '.msg._' in fullname and fullname not in self._importing:
512 # Let the normal import happen first
513 self._importing.add(fullname)
514 try:
515 spec = importlib.util.find_spec(fullname)
516 finally:
517 self._importing.remove(fullname)
518
519 if spec is not None and spec.loader is not None:
520 # Remember the original loader
521 self._original_loaders[fullname] = spec.loader
522 # Return a new spec with our custom loader
523 return ModuleSpec(
524 fullname,
525 ROSMessageLoader(self._original_loaders[fullname]),
526 origin=spec.origin,
527 is_package=spec.parent == ''
528 )
529 return None
530
531 class ROSMessageLoader(Loader):
532 """Custom loader that converts ROS messages to C++ after normal loading"""

Callers

nothing calls this directly

Calls 3

_is_msg_pythonFunction · 0.90
_resolve_message_typeFunction · 0.90

Tested by

no test coverage detected