Ejemplo n.º 1
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.º 2
0
    def updateParams(self, _event):
        old_verbose = self.verbose
        self.verbose = rospy.get_param('~verbose', True)
        # self.loginfo('verbose = %r' % self.verbose)
        if self.verbose != old_verbose:
            self.loginfo('Verbose is now %r' % self.verbose)

        self.image_size = rospy.get_param('~img_size')
        self.top_cutoff = rospy.get_param('~top_cutoff')

        if self.detector is None:
            c = rospy.get_param('~detector')
            assert isinstance(c, list) and len(c) == 2, c

            #         if str(self.detector_config) != str(c):
            self.loginfo('new detector config: %s' % str(c))

            self.detector = dtu.instantiate(c[0], c[1])


#             self.detector_config = c

        if self.verbose and self.pub_edge is None:
            self.pub_edge = rospy.Publisher("~edge", Image, queue_size=1)
            self.pub_colorSegment = rospy.Publisher("~colorSegment",
                                                    Image,
                                                    queue_size=1)
Ejemplo n.º 3
0
    def updateParams(self, event):
        if self.filter is None:
            c = rospy.get_param('~filter')
            assert isinstance(c, list) and len(c) == 2, c

            self.loginfo('new filter config: %s' % str(c))
            self.filter = dtu.instantiate(c[0], c[1])
Ejemplo n.º 4
0
def parse_reg_test():
    x = dtu.yaml_load(s)
    if isinstance(x['description'], unicode):
        msg = 'I do not expect Unicode'
        msg += '\n' + x.__repr__()
        raise ValueError(msg)
    _ = dtu.instantiate(x['constructor'], x['parameters'])
Ejemplo n.º 5
0
def parse_reg_fail():
    x = dtu.yaml_load(s_fail)
    print x.__repr__()
    try:
        _ = dtu.instantiate(x['constructor'], x['parameters'])
    except RTParseError:
        pass
    else:
        raise Exception('Expected failure')
Ejemplo n.º 6
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.º 7
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
Ejemplo n.º 8
0
 def LineDetectorClass():
     return dtu.instantiate(detector[0], detector[1])