Ejemplo n.º 1
0
def test_RosbinExecutable():
    from roslaunch.core import RosbinExecutable, PHASE_SETUP, PHASE_RUN
    e = RosbinExecutable('cmd', ('arg1', 'arg2'), PHASE_SETUP)
    assert e.command == 'cmd'
    assert e.args == ('arg1', 'arg2')
    assert e.phase == PHASE_SETUP
    assert 'cmd' in str(e)
    assert 'arg2' in str(e)
    assert 'cmd' in repr(e)
    assert 'arg2' in repr(e)
    assert 'ros/bin' in str(e)
    assert 'ros/bin' in repr(e)

    e = RosbinExecutable('cmd', ('arg1', 'arg2'))
    assert e.phase == PHASE_RUN
Ejemplo n.º 2
0
    def test_RosbinExecutable(self):
        from roslaunch.core import RosbinExecutable, PHASE_SETUP, PHASE_RUN, PHASE_TEARDOWN

        e = RosbinExecutable('ls', ['-alF'])
        self.assertEquals('ls', e.command)
        self.assertEquals(['-alF'], e.args)
        self.assertEquals(PHASE_RUN, e.phase)
        self.assertEquals('ros/bin/ls -alF', str(e))
        self.assertEquals('ros/bin/ls -alF', repr(e))        

        e = RosbinExecutable('ls', ['-alF', 'b*'], PHASE_TEARDOWN)
        self.assertEquals('ls', e.command)
        self.assertEquals(['-alF', 'b*'], e.args)
        self.assertEquals(PHASE_TEARDOWN, e.phase)
        self.assertEquals('ros/bin/ls -alF b*', str(e))
        self.assertEquals('ros/bin/ls -alF b*', repr(e))        
Ejemplo n.º 3
0
    def load_rosparam(self,
                      context,
                      ros_config,
                      cmd,
                      param,
                      file_,
                      text,
                      verbose=True):
        """
        Load rosparam setting
        
        @param context: Loader context
        @type  context: L{LoaderContext}
        @param ros_config: launch configuration
        @type  ros_config: L{ROSLaunchConfig}
        @param cmd: 'load', 'dump', or 'delete'
        @type  cmd: str
        @param file_: filename for rosparam to use or None
        @type  file_: str
        @param text: text for rosparam to load. Ignored if file_ is set.
        @type  text: str
        @raise ValueError: if parameters cannot be processed into valid rosparam setting
        """
        if not cmd in ('load', 'dump', 'delete'):
            raise ValueError("command must be 'load', 'dump', or 'delete'")
        if file_ is not None:
            if cmd == 'load' and not os.path.isfile(file_):
                raise ValueError("file does not exist [%s]" % file_)
            if cmd == 'delete':
                raise ValueError(
                    "'file' attribute is invalid with 'delete' command.")

        full_param = ns_join(context.ns, param) if param else context.ns

        if cmd == 'dump':
            ros_config.add_executable(
                RosbinExecutable('rosparam', (cmd, file_, full_param),
                                 PHASE_SETUP))
        elif cmd == 'delete':
            ros_config.add_executable(
                RosbinExecutable('rosparam', (cmd, full_param), PHASE_SETUP))
        elif cmd == 'load':
            # load YAML text
            if file_:
                with open(file_, 'r') as f:
                    text = f.read()

            # parse YAML text
            # - lazy import
            global yaml
            if yaml is None:
                import yaml
            # - lazy import: we have to import rosparam in oder to to configure the YAML constructors
            global rosparam
            if rosparam is None:
                import rosparam
            try:
                data = yaml.load(text)
                # #3162: if there is no YAML, load() will return an
                # empty string.  We want an empty dictionary instead
                # for our representation of empty.
                if data is None:
                    data = {}
            except yaml.MarkedYAMLError as e:
                if not file_:
                    raise ValueError(
                        "Error within YAML block:\n\t%s\n\nYAML is:\n%s" %
                        (str(e), text))
                else:
                    raise ValueError("file %s contains invalid YAML:\n%s" %
                                     (file_, str(e)))
            except Exception as e:
                if not file_:
                    raise ValueError("invalid YAML: %s\n\nYAML is:\n%s" %
                                     (str(e), text))
                else:
                    raise ValueError("file %s contains invalid YAML:\n%s" %
                                     (file_, str(e)))

            # 'param' attribute is required for non-dictionary types
            if not param and type(data) != dict:
                raise ValueError(
                    "'param' attribute must be set for non-dictionary values")

            self.add_param(ros_config, full_param, data, verbose=verbose)

        else:
            raise ValueError("unknown command %s" % cmd)
Ejemplo n.º 4
0
    def load_rosparam(self,
                      context,
                      ros_config,
                      cmd,
                      param,
                      file,
                      text,
                      verbose=True):
        """
        Load rosparam setting
        
        @param context: Loader context
        @type  context: L{LoaderContext}
        @param ros_config: launch configuration
        @type  ros_config: L{ROSLaunchConfig}
        @param cmd: 'load', 'dump', or 'delete'
        @type  cmd: str
        @param file: filename for rosparam to use or None
        @type  file: str
        @param text: text for rosparam to load. Ignored if file is set.
        @type  text: str
        @raise ValueError: if parameters cannot be processed into valid rosparam setting
        """
        if not cmd in ('load', 'dump', 'delete'):
            raise ValueError("command must be 'load', 'dump', or 'delete'")
        if file is not None:
            if cmd == 'load' and not os.path.isfile(file):
                raise ValueError("file does not exist [%s]" % file)
            if cmd == 'delete':
                raise ValueError(
                    "'file' attribute is invalid with 'delete' command.")

        full_param = ns_join(context.ns, param) if param else context.ns

        if cmd == 'dump':
            ros_config.add_executable(
                RosbinExecutable('rosparam', (cmd, file, full_param),
                                 PHASE_SETUP))
        elif cmd == 'delete':
            ros_config.add_executable(
                RosbinExecutable('rosparam', (cmd, full_param), PHASE_SETUP))
        elif cmd == 'load':
            # load YAML text
            if file:
                with open(file, 'r') as f:
                    text = f.read()

            if not text:
                if file:
                    raise ValueError("no YAML in file %s" % file)
                else:
                    raise ValueError("no YAML to load")

            # parse YAML text
            # - lazy import
            global yaml
            if yaml is None:
                import yaml
            try:
                data = yaml.load(text)
            except yaml.MarkedYAMLError, e:
                if not file:
                    raise ValueError(
                        "Error within YAML block:\n\t%s\n\nYAML is:\n%s" %
                        (str(e), text))
                else:
                    raise ValueError("file %s contains invalid YAML:\n%s" %
                                     (file, str(e)))
            except Exception, e:
                if not file:
                    raise ValueError("invalid YAML: %s\n\nYAML is:\n%s" %
                                     (str(e), text))
                else:
                    raise ValueError("file %s contains invalid YAML:\n%s" %
                                     (file, str(e)))