Exemple #1
0
    def from_config(cls, configuration: ProtocolConfig) -> "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."
        directory = configuration.directory
        package_modules = load_all_modules(
            directory,
            glob="__init__.py",
            prefix=configuration.prefix_import_path)
        add_modules_to_sys_modules(package_modules)
        serialization_module = load_module("serialization",
                                           Path(directory, "serialization.py"))
        classes = inspect.getmembers(serialization_module, inspect.isclass)
        serializer_classes = list(
            filter(lambda x: re.match("\\w+Serializer", x[0]), classes))
        assert len(
            serializer_classes) == 1, "Not exactly one serializer detected."
        serializer_class = serializer_classes[0][1]

        serializer = serializer_class()
        return Protocol(configuration, serializer)
Exemple #2
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
        package_modules = load_all_modules(
            directory, glob="__init__.py", prefix=configuration.prefix_import_path
        )
        add_modules_to_sys_modules(package_modules)
        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, contract_interface)
Exemple #3
0
    def from_config(cls,
                    configuration: SkillConfig,
                    skill_context: Optional[SkillContext] = None) -> "Skill":
        """
        Load the skill from configuration.

        :param configuration: a skill configuration. Must be associated with a directory.
        :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(
        ) if skill_context is None else skill_context
        skill = Skill(configuration, skill_context)

        directory = configuration.directory
        package_modules = load_all_modules(
            directory,
            glob="__init__.py",
            prefix=configuration.prefix_import_path)
        add_modules_to_sys_modules(package_modules)
        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 _load_connection(address: Address, configuration: ConnectionConfig) -> Connection:
    """
    Load a connection from a directory.

    :param address: the connection address.
    :param configuration: the connection configuration.
    :return: the connection.
    """
    try:
        directory = cast(Path, configuration.directory)
        package_modules = load_all_modules(
            directory, glob="__init__.py", prefix=configuration.prefix_import_path
        )
        add_modules_to_sys_modules(package_modules)
        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.from_config(
            address=address, configuration=configuration
        )
    except Exception as e:
        raise Exception(
            "An error occurred while loading connection {}: {}".format(
                configuration.public_id, str(e)
            )
        )
Exemple #5
0
    def from_config(cls,
                    configuration: SkillConfig,
                    skill_context: Optional[SkillContext] = None) -> "Skill":
        """
        Load the skill from configuration.

        :param configuration: a skill configuration. Must be associated with a directory.
        :return: the skill.
        """
        assert (
            configuration.directory
            is not None), "Configuration must be associated with a directory."
        directory = configuration.directory
        package_modules = load_all_modules(
            directory,
            glob="__init__.py",
            prefix=configuration.prefix_import_path)
        add_modules_to_sys_modules(package_modules)
        skill_context = SkillContext(
        ) if skill_context is None else skill_context
        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 = Skill(configuration, skill_context, handlers, behaviours,
                      model_instances)
        skill_context._skill = skill
        return skill