예제 #1
0
    def resolve(self, service: Service, services2_classes: dict,
                aliases2_services: dict) -> ResolvedService:
        if service.uses_factory():
            if service.factory_service.service_name not in services2_classes:
                raise Exception(
                    f"Factory service {service.factory_service.service_name} not found"
                )

            factory_class = services2_classes[
                service.factory_service.service_name]
            inspected_arguments = self.__inspected_arguments_resolver.resolve_method(
                factory_class, service.factory_method)
        else:
            inspected_arguments = self.__inspected_arguments_resolver.resolve_constructor(
                service.class_)

        if service.has_named_arguments():
            resolved_arguments = self.__named_arguments_resolver.resolve(
                service.arguments, inspected_arguments, service.name)
        else:
            resolved_arguments = self.__argument_list_resolver.resolve(
                service.arguments, inspected_arguments, service.name)

        if not service.uses_factory():
            self.__arguments_validator.validate(service.name,
                                                resolved_arguments,
                                                services2_classes,
                                                aliases2_services)

        return ResolvedService(service, resolved_arguments)
예제 #2
0
    def test_basic(self):
        raw_services = {
            "injecta.mocks.Bar.Bar": {"arguments": ["Jiri Koutny"]},
            "injecta.mocks.Foo.Foo": {"arguments": ["@injecta.mocks.Bar.Bar"]},
            "injecta.mocks.Bar.BarAlias": "@injecta.mocks.Bar.Bar",
        }

        expected_service1 = Service(
            "injecta.mocks.Bar.Bar",
            DType("injecta.mocks.Bar", "Bar"),
            [
                PrimitiveArgument("Jiri Koutny"),
            ],
        )

        expected_service2 = Service(
            "injecta.mocks.Foo.Foo",
            DType("injecta.mocks.Foo", "Foo"),
            [
                ServiceArgument("injecta.mocks.Bar.Bar"),
            ],
        )

        expected_alias1 = ServiceAlias("injecta.mocks.Bar.BarAlias", "injecta.mocks.Bar.Bar")

        services, aliases = self.__services_preparer.prepare(raw_services)

        self.assertEqual(expected_service1, services[0])
        self.assertEqual(expected_service2, services[1])
        self.assertEqual(expected_alias1, aliases[0])
예제 #3
0
    def __create_container_build(self):
        tags2_services = {
            "my_service_tag": [
                Service("injecta.mocks.Bar", DType("injecta.mocks.Bar", "Bar")),
                Service("injecta.mocks.Bar.new", DType("injecta.mocks.Bar", "Bar")),
            ]
        }

        return ContainerBuild({}, [], {}, {}, tags2_services)
    def __createContainerBuild(self):
        tags2Services = {
            'myServiceTag': [
                Service('injecta.mocks.Bar', DType('injecta.mocks.Bar',
                                                   'Bar')),
                Service('injecta.mocks.Bar.new',
                        DType('injecta.mocks.Bar', 'Bar'))
            ]
        }

        return ContainerBuild({}, [], {}, {}, tags2Services)
    def test_multilineScript(self):
        d1 = Service('foo.Bar', DType('foo.Bar', 'Bar'),
                     [PrimitiveArgument('ahoj'),
                      PrimitiveArgument(52)], ['console.command'])
        d2 = Service('hello.World_service', DType('hello.World', 'World'), [],
                     ['my.tag'])
        d3 = Service('my.Name', DType('my.Name', 'Name'), [],
                     ['console.command'])

        result = self.__tag2ServicesPreparer.prepare([d1, d2, d3])

        expected = {'console.command': [d1, d3], 'my.tag': [d2]}

        self.assertEqual(expected, result)
예제 #6
0
    def test_multiline_script(self):
        d1 = Service("foo.Bar", DType("foo.Bar", "Bar"),
                     [PrimitiveArgument("ahoj"),
                      PrimitiveArgument(52)], ["console.command"])
        d2 = Service("hello.World_service", DType("hello.World", "World"), [],
                     ["my.tag"])
        d3 = Service("my.Name", DType("my.Name", "Name"), [],
                     ["console.command"])

        result = self.__tag2_services_preparer.prepare([d1, d2, d3])

        expected = {"console.command": [d1, d3], "my.tag": [d2]}

        self.assertEqual(expected, result)
예제 #7
0
    def modify_services(self, services: List[Service],
                        aliases: List[ServiceAlias], parameters: Box):
        if is_running_in_console():
            aliases.append(
                ServiceAlias("databricksbundle.logger",
                             "consolebundle.logger"))
        else:
            service = Service("databricksbundle.logger",
                              DType("logging", "Logger"))
            service.set_factory(
                ServiceArgument(NotebookLoggerFactory.__module__), "create")

            services.append(service)

        return services, aliases
예제 #8
0
    def test_service_with_no_args(self):
        result = self.__service_parser.parse("injecta.api.ApiClient.ApiClient",
                                             None)
        expected = Service("injecta.api.ApiClient.ApiClient",
                           DType("injecta.api.ApiClient", "ApiClient"))

        self.assertEqual(expected, result)
예제 #9
0
    def test_factory(self):
        result = self.__serviceParser.parse('injecta.api.ApiClient', {
            'factory': ['@injecta.api.ApiClientFactory.ApiClientFactory', 'create'],
            'arguments': [
                'Jirka',
            ]
        })
        expected = Service(
            'injecta.api.ApiClient',
            DType('injecta.api', 'ApiClient'),
            [
                PrimitiveArgument('Jirka'),
            ]
        )
        expected.setFactory(ServiceArgument('injecta.api.ApiClientFactory.ApiClientFactory'), 'create')

        self.assertEqual(expected, result)
예제 #10
0
    def test_serviceWithNoArgs(self):
        result = self.__serviceParser.parse('injecta.api.ApiClient.ApiClient', None)
        expected = Service(
            'injecta.api.ApiClient.ApiClient',
            DType('injecta.api.ApiClient', 'ApiClient')
        )

        self.assertEqual(expected, result)
예제 #11
0
    def parse(self, service_name: str, raw_service: dict = None):
        if raw_service is None:
            return Service(service_name,
                           self.__type_resolver.resolve(service_name))

        arguments = self.__parse_arguments(raw_service)
        tags = raw_service["tags"] if "tags" in raw_service else []
        class_ = self.__type_resolver.resolve(
            raw_service["class"] if "class" in raw_service else service_name)

        service = Service(service_name, class_, arguments, tags)

        if "autowire" in raw_service:
            service.set_autowire(raw_service["autowire"] is True)

        if "factory" in raw_service:
            if raw_service["factory"][0][0:1] != "@":
                raise Exception(
                    "Factory service name must be prefixed with @ (service {})"
                    .format(service_name))

            service.set_factory(ServiceArgument(raw_service["factory"][0][1:]),
                                raw_service["factory"][1])

        return service
예제 #12
0
    def parse(self, serviceName: str, rawService: dict = None):
        if rawService is None:
            return Service(serviceName,
                           self.__typeResolver.resolve(serviceName))

        arguments = self.__parseArguments(rawService)
        tags = rawService['tags'] if 'tags' in rawService else []
        class_ = self.__typeResolver.resolve(
            rawService['class'] if 'class' in rawService else
            serviceName)  # pylint: disable = invalid-name

        service = Service(serviceName, class_, arguments, tags)

        if 'autowire' in rawService:
            service.setAutowire(rawService['autowire'] is True)

        if 'factory' in rawService:
            if rawService['factory'][0][0:1] != '@':
                raise Exception(
                    'Factory service name must be prefixed with @ (service {})'
                    .format(serviceName))

            service.setFactory(ServiceArgument(rawService['factory'][0][1:]),
                               rawService['factory'][1])

        return service
예제 #13
0
    def test_basic(self):
        result = self.__service_parser.parse(
            "injecta.api.ApiClient_test",
            {
                "class": "injecta.api.ApiClient.ApiClient",
                "autowire": True,
                "arguments": ["Jirka", 15, False, "@injecta.api.Connector"]
            },
        )
        expected = Service(
            "injecta.api.ApiClient_test",
            DType("injecta.api.ApiClient", "ApiClient"),
            [
                PrimitiveArgument("Jirka"),
                PrimitiveArgument(15),
                PrimitiveArgument(False),
                ServiceArgument("injecta.api.Connector"),
            ],
        )
        expected.set_autowire(True)

        self.assertEqual(expected, result)
예제 #14
0
    def test_basic(self):
        rawServices = {
            'injecta.mocks.Bar.Bar': {
                'arguments': [
                    'Jiri Koutny'
                ]
            },
            'injecta.mocks.Foo.Foo': {
                'arguments': [
                    '@injecta.mocks.Bar.Bar'
                ]
            },
            'injecta.mocks.Bar.BarAlias': '@injecta.mocks.Bar.Bar',
        }

        expectedService1 = Service(
            'injecta.mocks.Bar.Bar',
            DType('injecta.mocks.Bar', 'Bar'),
            [
                PrimitiveArgument('Jiri Koutny'),
            ]
        )

        expectedService2 = Service(
            'injecta.mocks.Foo.Foo',
            DType('injecta.mocks.Foo', 'Foo'),
            [
                ServiceArgument('injecta.mocks.Bar.Bar'),
            ]
        )

        expectedAlias1 = ServiceAlias('injecta.mocks.Bar.BarAlias', 'injecta.mocks.Bar.Bar')

        services, aliases = self.__servicesPreparer.prepare(rawServices)

        self.assertEqual(expectedService1, services[0])
        self.assertEqual(expectedService2, services[1])
        self.assertEqual(expectedAlias1, aliases[0])
예제 #15
0
    def test_factory(self):
        result = self.__service_parser.parse(
            "injecta.api.ApiClient",
            {
                "factory":
                ["@injecta.api.ApiClientFactory.ApiClientFactory", "create"],
                "arguments": [
                    "Jirka",
                ],
            },
        )
        expected = Service(
            "injecta.api.ApiClient",
            DType("injecta.api", "ApiClient"),
            [
                PrimitiveArgument("Jirka"),
            ],
        )
        expected.set_factory(
            ServiceArgument("injecta.api.ApiClientFactory.ApiClientFactory"),
            "create")

        self.assertEqual(expected, result)
예제 #16
0
    def resolve(self, service: Service, services2Classes: dict,
                aliases2Services: dict) -> ResolvedService:
        if service.usesFactory():
            factoryClass = services2Classes[service.factoryService.serviceName]
            inspectedArguments = self.__inspectedArgumentsResolver.resolveMethod(
                factoryClass, service.factoryMethod)
        else:
            inspectedArguments = self.__inspectedArgumentsResolver.resolveConstructor(
                service.class_)

        if service.hasNamedArguments():
            resolvedArguments = self.__namedArgumentsResolver.resolve(
                service.arguments, inspectedArguments, service.name)
        else:
            resolvedArguments = self.__argumentListResolver.resolve(
                service.arguments, inspectedArguments, service.name)

        if not service.usesFactory():
            self.__argumentsValidator.validate(service.name, resolvedArguments,
                                               services2Classes,
                                               aliases2Services)

        return ResolvedService(service, resolvedArguments)
예제 #17
0
    def test_basic(self):
        result = self.__serviceParser.parse('injecta.api.ApiClient_test', {
            'class': 'injecta.api.ApiClient.ApiClient',
            'autowire': True,
            'arguments': [
                'Jirka',
                15,
                False,
                '@injecta.api.Connector'
            ]
        })
        expected = Service(
            'injecta.api.ApiClient_test',
            DType('injecta.api.ApiClient', 'ApiClient'),
            [
                PrimitiveArgument('Jirka'),
                PrimitiveArgument(15),
                PrimitiveArgument(False),
                ServiceArgument('injecta.api.Connector'),
            ]
        )
        expected.setAutowire(True)

        self.assertEqual(expected, result)