Ejemplo n.º 1
0
def check_validity_family_interface(f):
    # try to import interface
    symbol = f.interface
    try:
        dtu.import_name(symbol)
    except ValueError:
        #logger.error(e)
        error_if_invalid = 'Invalid symbol %r.' % symbol
        return f._replace(valid=False, error_if_invalid=error_if_invalid)
    return f
Ejemplo n.º 2
0
    def create_instance(self, family_name, instance_name_or_spec):
        dtu.check_isinstance(instance_name_or_spec, (dict, str))

        family = self.get_family(family_name)
        if not family.valid:
            msg = ('Cannot instantiate %r because its family %r is invalid.' %
                   (instance_name_or_spec, family_name))
            msg += '\n\n' + dtu.indent(family.error_if_invalid, "  > ")
            raise dtu.DTConfigException(msg)

        if isinstance(instance_name_or_spec, str):
            instance_name = instance_name_or_spec
            dtu.check_is_in('instance', instance_name, family.instances)
            instance = family.instances[instance_name]
            if not instance.valid:
                msg = ('Cannot instantiate %r because it is invalid:\n%s' %
                       (instance_name,
                        dtu.indent(instance.error_if_invalid, '> ')))
                raise dtu.DTConfigException(msg)
            res = dtu.instantiate(instance.constructor, instance.parameters)
        elif isinstance(instance_name_or_spec, dict):
            _name, spec = _parse_inline_spec(instance_name_or_spec)
            res = dtu.instantiate(spec.constructor, spec.parameters)
        else:
            assert False

        interface = dtu.import_name(family.interface)
        if not isinstance(res, interface):
            msg = ('I expected that %r would be a %s but it is a %s.' %
                   (instance_name, interface.__name__, type(res).__name__))
            raise dtu.DTConfigException(msg)

        return res
Ejemplo n.º 3
0
def check_validity_instance(f, i):
    if not f.valid:
        msg = 'Instance not valid because family not valid.'
        return i._replace(valid=False, error_if_invalid=msg)

    try:
        res = instantiate(i.constructor, i.parameters)
    except Exception as e:
        msg = str(e)
        return i._replace(valid=False, error_if_invalid=msg)

    interface = import_name(f.interface)
    #     print('interface: %s' % interface)
    if not isinstance(res, interface):
        msg = ('Expected a %s but it is a %s.' %
               (interface.__name__, type(res).__name__))
        return i._replace(valid=False, error_if_invalid=msg)
    return i
Ejemplo n.º 4
0
def message_class_from_string(s):
    if not '/' in s:
        msg = ''
        msg += 'Invalid message name "%s".\n' % s
        msg += 'I expected that the name of the message is in the format "PACKAGE/MSG".\n '
        msg += 'E.g. "sensor_msgs/Joy" or "duckietown_msgs/BoolStamped".'
        raise dtu.DTConfigException(msg)

    # e.g. "std_msgs/Header"
    i = s.index('/')
    package = s[:i]
    name = s[i + 1:]
    symbol = '%s.msg.%s' % (package, name)
    try:
        msgclass = dtu.import_name(symbol)
        return msgclass
    except ValueError as e:
        msg = 'Cannot import type for message "%s" (%s).' % (s, symbol)
        dtu.raise_wrapped(dtu.DTConfigException, e, msg, compact=True)
Ejemplo n.º 5
0
    def create_instance(self, family_name, instance_name):
        family = self.get_family(family_name)
        if not family.valid:
            msg = ('Cannot instantiate %r because its family %r is invalid.' %
                   (instance_name, family_name))
            raise DTConfigException(msg)

        check_is_in('instance', instance_name, family.instances)
        instance = family.instances[instance_name]

        if not instance.valid:
            msg = ('Cannot instantiate because it is invalid:\n%s' %
                   indent(instance.error_if_invalid, '> '))
            raise DTConfigException(msg)
        res = instantiate(instance.constructor, instance.parameters)

        interface = import_name(family.interface)
        if not isinstance(res, interface):
            msg = ('I expected that %r would be a %s but it is a %s.' %
                   (instance_name, interface.__name__, type(res).__name__))
            raise DTConfigException(msg)

        return res