Exemple #1
0
    def from_config(cls, configuration: ContractConfig) -> "Contract":
        """
        Load contract from configuration.

        :param configuration: the contract configuration.
        :return: the contract object.
        """
        assert (
            configuration.directory
            is not None), "Configuration must be associated with a directory."
        directory = configuration.directory
        load_aea_package(configuration)
        contract_module = load_module("contracts", directory / "contract.py")
        classes = inspect.getmembers(contract_module, inspect.isclass)
        contract_class_name = cast(str, configuration.class_name)
        contract_classes = list(
            filter(lambda x: re.match(contract_class_name, x[0]), classes))
        name_to_class = dict(contract_classes)
        logger.debug("Processing contract {}".format(contract_class_name))
        contract_class = name_to_class.get(contract_class_name, None)
        assert contract_class_name is not None, "Contract class '{}' not found.".format(
            contract_class_name)

        # path = Path(directory, configuration.path_to_contract_interface)
        # with open(path, "r") as interface_file:
        #     contract_interface = json.load(interface_file)

        return contract_class(configuration)
Exemple #2
0
    def from_config(cls, configuration: ConnectionConfig, identity: Identity,
                    crypto_store: CryptoStore, **kwargs) -> "Connection":
        """
        Load a connection from a configuration.

        :param configuration: the connection configuration.
        :param identity: the identity object.
        :param crypto_store: object to access the connection crypto objects.
        :return: an instance of the concrete connection class.
        """
        configuration = cast(ConnectionConfig, configuration)
        directory = cast(Path, configuration.directory)
        load_aea_package(configuration)
        connection_module_path = directory / "connection.py"
        assert (
            connection_module_path.exists()
            and connection_module_path.is_file()
        ), "Connection module '{}' not found.".format(connection_module_path)
        connection_module = load_module("connection_module",
                                        directory / "connection.py")
        classes = inspect.getmembers(connection_module, inspect.isclass)
        connection_class_name = cast(str, configuration.class_name)
        connection_classes = list(
            filter(lambda x: re.match(connection_class_name, x[0]), classes))
        name_to_class = dict(connection_classes)
        logger.debug("Processing connection {}".format(connection_class_name))
        connection_class = name_to_class.get(connection_class_name, None)
        assert connection_class is not None, "Connection class '{}' not found.".format(
            connection_class_name)
        return connection_class(configuration=configuration,
                                identity=identity,
                                crypto_store=crypto_store,
                                **kwargs)
Exemple #3
0
    def from_config(
        cls, configuration: SkillConfig, agent_context: AgentContext
    ) -> "Skill":
        """
        Load the skill from configuration.

        :param configuration: a skill configuration. Must be associated with a directory.
        :param agent_context: the agent context.
        :return: the skill.
        """
        assert (
            configuration.directory is not None
        ), "Configuration must be associated with a directory."

        # we put the initialization here because some skill components
        # might need some info from the skill
        # (e.g. see https://github.com/fetchai/agents-aea/issues/1095)
        skill_context = SkillContext()
        skill_context.set_agent_context(agent_context)
        logger_name = f"aea.packages.{configuration.author}.skills.{configuration.name}"
        logger = AgentLoggerAdapter(
            logging.getLogger(logger_name), agent_context.agent_name
        )
        skill_context.logger = logger

        skill = Skill(configuration, skill_context)

        directory = configuration.directory
        load_aea_package(configuration)
        handlers_by_id = dict(configuration.handlers.read_all())
        handlers = Handler.parse_module(
            str(directory / "handlers.py"), handlers_by_id, skill_context
        )

        behaviours_by_id = dict(configuration.behaviours.read_all())
        behaviours = Behaviour.parse_module(
            str(directory / "behaviours.py"), behaviours_by_id, skill_context,
        )

        models_by_id = dict(configuration.models.read_all())
        model_instances = Model.parse_module(
            str(directory), models_by_id, skill_context
        )

        skill.handlers.update(handlers)
        skill.behaviours.update(behaviours)
        skill.models.update(model_instances)

        return skill
Exemple #4
0
    def from_config(cls, configuration: ProtocolConfig,
                    **kwargs) -> "Protocol":
        """
        Load the protocol from configuration.

        :param configuration: the protocol configuration.
        :return: the protocol object.
        """
        assert (
            configuration.directory
            is not None), "Configuration must be associated with a directory."
        load_aea_package(configuration)
        class_module = importlib.import_module(
            configuration.prefix_import_path + ".message")
        classes = inspect.getmembers(class_module, inspect.isclass)
        name_camel_case = "".join(word.capitalize()
                                  for word in configuration.name.split("_"))
        message_classes = list(
            filter(
                lambda x: re.match("{}Message".format(name_camel_case), x[0]),
                classes))
        assert len(
            message_classes) == 1, "Not exactly one message class detected."
        message_class = message_classes[0][1]
        class_module = importlib.import_module(
            configuration.prefix_import_path + ".serialization")
        classes = inspect.getmembers(class_module, inspect.isclass)
        serializer_classes = list(
            filter(
                lambda x: re.match("{}Serializer".format(name_camel_case), x[0]
                                   ),
                classes,
            ))
        assert (len(serializer_classes) == 1
                ), "Not exactly one serializer class detected."
        serialize_class = serializer_classes[0][1]
        message_class.serializer = serialize_class

        return Protocol(configuration, message_class, **kwargs)
    def _load_and_add_components(
        self,
        component_type: ComponentType,
        resources: Resources,
        agent_name: str,
        **kwargs,
    ) -> None:
        """
        Load and add components added to the builder to a Resources instance.

        :param component_type: the component type for which
        :param resources: the resources object to populate.
        :param agent_name: the AEA name for logging purposes.
        :param kwargs: keyword argument to forward to the component loader.
        :return: None
        """
        for configuration in self._package_dependency_manager.get_components_by_type(
            component_type
        ).values():
            if configuration.is_abstract_component:
                load_aea_package(configuration)
                continue

            if configuration in self._component_instances[component_type].keys():
                component = self._component_instances[component_type][configuration]
                if configuration.component_type != ComponentType.SKILL:
                    component.logger = cast(
                        logging.Logger, make_logger(configuration, agent_name)
                    )
            else:
                configuration = deepcopy(configuration)
                _logger = make_logger(configuration, agent_name)
                component = load_component_from_config(
                    configuration, logger=_logger, **kwargs
                )

            resources.add_component(component)
Exemple #6
0
def test_load_aea_package():
    """Test aea package load."""
    config = ConnectionConfig("http_client", "fetchai", "0.5.0")
    config.directory = Path(ROOT_DIR) / "packages"
    load_aea_package(config)