vendredi 21 février 2020

Load dynamically a file, and run a function inside

I have several launch files in my directory (e.g. "my_script.launch.py") and their content is pretty similar to this file:

import launch
import launch.actions
import launch.substitutions
import launch_ros.actions


def generate_launch_description():
    return launch.LaunchDescription([
        launch.actions.DeclareLaunchArgument(
            'node_prefix',
            default_value=[launch.substitutions.EnvironmentVariable('USER'), '_'],
            description='Prefix for node names'),
        launch_ros.actions.Node(
            package='demo_nodes_py', node_executable='talker', output='screen',
            node_name=[launch.substitutions.LaunchConfiguration('node_prefix'), 'talker']),
    ])

All of them have some imports at the top and a generate_launch_description() function, that returns an object which I want to inspect.

I want to implement this function

def get_generate_launch_description_per_path(path):
   pass

that executes the convenient function of the python file on that path and thus returns the LaunchDescription object so I can manipulate it afterwards. Any idea? Probably will need some kind of reflection or dynamic load.





Aucun commentaire:

Enregistrer un commentaire