Example #1
0
    def __setattr__(self, key, value):
        """A setattr that handles Modules being added specially.

        This is so that we can use the variable name for the Module as
        the name that all of the SPA system will use to access that module.
        """
        if not hasattr(self, "_initialized"):
            return super().__setattr__(key, value)

        if hasattr(self, key) and isinstance(getattr(self, key), Module):
            raise SpaModuleError("Cannot re-assign module-attribute %s to %s. "
                                 "SPA module-attributes can only be assigned "
                                 "once." % (key, value))
        super().__setattr__(key, value)
        if isinstance(value, Module):
            if value.label is None:
                value.label = key
            self._modules[key] = value
            for k, (obj, v) in value.inputs.items():
                if type(v) == int:
                    value.inputs[k] = (obj, self.get_default_vocab(v))
                self.config[obj].vocab = value.inputs[k][1]
            for k, (obj, v) in value.outputs.items():
                if type(v) == int:
                    value.outputs[k] = (obj, self.get_default_vocab(v))
                self.config[obj].vocab = value.outputs[k][1]

            value.on_add(self)
Example #2
0
 def get_module(self, name):
     """Return the module for the given name."""
     if name in self._modules:
         return self._modules[name]
     elif "_" in name:
         module, name = name.rsplit("_", 1)
         if module in self._modules:
             return self._modules[module]
     raise SpaModuleError("Could not find module %r" % name)
Example #3
0
    def __exit__(self, ex_type, ex_value, traceback):
        super().__exit__(ex_type, ex_value, traceback)
        if ex_type is not None:
            # re-raise the exception that triggered this __exit__
            return False

        module_list = frozenset(self._modules.values())
        for net in self.networks:
            # Since there are no attributes to distinguish what's been added
            # and what hasn't, we have to ask the network
            if isinstance(net, Module) and (net not in module_list):
                raise SpaModuleError(
                    "%s must be set as an attribute of a SPA network" % (net))
Example #4
0
    def get_module_output(self, name):
        """Return the object to connect into for the given name.

        The name will be either the same as a module, or of the form
        ``<module_name>_<output_name>``.
        """
        if name in self._modules:
            return self._modules[name].outputs["default"]
        elif "_" in name:
            module, name = name.rsplit("_", 1)
            if module in self._modules:
                m = self._modules[module]
                if name in m.outputs:
                    return m.outputs[name]
        raise SpaModuleError("Could not find module output %r" % name)
Example #5
0
    def get_module_input(self, name):
        """Return the object to connect into for the given name.

        The name will be either the same as a module, or of the form
        ``<module_name>_<input_name>``.
        """
        if name in self._modules and 'default' in self._modules[name].inputs:
            return self._modules[name].inputs['default']
        elif '_' in name:
            module, name = name.rsplit('_', 1)
            if module in self._modules:
                m = self._modules[module]
                if name in m.inputs:
                    return m.inputs[name]
        raise SpaModuleError("Could not find module input %r" % name)