Esempio n. 1
0
def test_utils():
    klass = import_string("sn_agent.network.test.TestNetwork")
    app = MockApp()
    network = klass(app)

    network.join_network()
    network.logon_network()
    network.logoff_network()
    network.leave_network()
Esempio n. 2
0
def setup_network(app):
    settings = NetworkSettings()
    klass = import_string(settings.CLASS)
    app['network'] = klass(app)
Esempio n. 3
0
def setup_service_manager(app):
    settings = ServiceAdapterSettings()
    config_file = settings.CONFIG_FILE
    ontology = app['ontology']

    log.debug("reading configuration file {0}".format(config_file))

    with open(config_file, 'r') as ymlfile:
        cfg = yaml.load(ymlfile)

    service_adapters = []
    for section, data in cfg.items():
        if section == 'opencogs':
            for opencog_data in data:
                ontology_node_id = opencog_data.get('ontology_node_id')
                if ontology_node_id is None:
                    raise RuntimeError(
                        'You must supply a ontology_node_id for each service adapter'
                    )

                required_ontology_node_ids = opencog_data.get(
                    'required_ontology_node_ids')

                host = opencog_data['host']
                port = opencog_data['port']
                service = ontology.get_service(ontology_node_id)
                service_adapter = OpenCogServiceAdapter(
                    app, service, required_ontology_node_ids, host, port)
                service_adapters.append(service_adapter)

        elif section == 'jsonrpcs':
            for jsonrpc_data in data:
                ontology_node_id = jsonrpc_data.get('ontology_node_id')
                if ontology_node_id is None:
                    raise RuntimeError(
                        'You must supply a ontology_node_id for each service adapter'
                    )

                required_ontology_node_ids = jsonrpc_data.get(
                    'required_ontology_node_ids')

                url = jsonrpc_data['url']
                service = ontology.get_service(ontology_node_id)
                service_adapter = JsonRpcServiceAdapter(
                    app, service, required_ontology_node_ids, url)
                service_adapters.append(service_adapter)

        elif section == 'modules':
            for module_data in data:
                ontology_node_id = module_data.get('ontology_node_id')
                if ontology_node_id is None:
                    raise RuntimeError(
                        'You must supply a ontology_node_id for each service adapter'
                    )

                required_ontology_node_ids = module_data.get(
                    'required_ontology_node_ids')
                if required_ontology_node_ids is None:
                    required_ontology_node_ids = []

                name = module_data['name']
                module_klass = import_string(name)
                service = ontology.get_service(ontology_node_id)
                service_adapter = module_klass(app, service,
                                               required_ontology_node_ids,
                                               name)
                service_adapters.append(service_adapter)
        else:
            raise RuntimeError('Unknown service adapter type specified: %s' %
                               section)

    service_manager = ServiceManager(service_adapters)
    service_manager.post_load_initialize()
    app['service_manager'] = service_manager
Esempio n. 4
0
def setup_service_manager(
        app,
        service_adapters: List[ServiceAdapterABC] = None) -> ServiceManager:
    settings = ServiceAdapterSettings()
    config_file = settings.CONFIG_FILE
    ontology = app['ontology']

    logger.debug("reading configuration file {0}".format(config_file))

    with open(config_file, 'r') as ymlfile:
        cfg = yaml.load(ymlfile)

    if service_adapters is None:
        service_adapters = []

    # Load service adapters from the config file.
    for section, data in cfg.items():
        if section == 'services':
            for service_object in data:

                service_id = service_object.get('service')
                if service_id is None:
                    raise RuntimeError(
                        'You must supply a service id for each service adapter'
                    )

                service = ontology.get_service(service_id)

                name = service_object['module']
                module_klass = import_string(name)

                required_services_object = service_object.get(
                    'required_services')

                required_services = []

                # Get the required / dependent service adapters corresponding to require service ids.
                if required_services_object is not None:
                    for required_service_object in required_services_object:

                        required_service_id = required_service_object.get('id')
                        if required_service_id is None:
                            raise RuntimeError(
                                'You must supply a service id for each required service'
                            )

                        required_service = ontology.get_service(
                            required_service_id)
                        required_services.append(required_service)

                        required_agents = required_service_object.get('agents')

                        if required_agents is not None:
                            external_service_adapter = ExternalServiceAdapter(
                                app, required_service, required_agents)
                            service_adapters.append(external_service_adapter)

                service.required_services = required_services

                service_adapter = module_klass(app, service, required_services)
                service_adapters.append(service_adapter)
        else:
            raise RuntimeError('Unknown service adapter type specified: %s' %
                               section)

    service_manager = ServiceManager(app, service_adapters)
    service_manager.post_load_initialize()
    app['service_manager'] = service_manager
    return service_manager
Esempio n. 5
0
def setup_network(app):
    settings = NetworkSettings()
    logger.debug('Loading network class: %s', settings.CLASS)
    klass = import_string(settings.CLASS)
    app['network'] = klass(app)
Esempio n. 6
0
def setup_agent(app):
    settings = AgentSettings()
    agent_id = settings.ID
    klass = import_string(settings.CLASS)
    app['agent'] = klass(app, agent_id)