Ejemplo n.º 1
0
    def test__register_injectable__with_qualifier_only(self):
        # given
        injectable = MagicMock(spec=Injectable)
        qualifier = "qualifier"
        namespace = Namespace()

        # when
        namespace.register_injectable(injectable, qualifier=qualifier)

        # then
        assert namespace.qualifier_registry[qualifier] == {injectable}
Ejemplo n.º 2
0
    def test__register_injectable__with_class_only(self):
        # given
        injectable = MagicMock(spec=Injectable)
        namespace = Namespace()
        klass = TestNamespace
        class_lookup_key = klass.__qualname__

        # when
        namespace.register_injectable(injectable, klass)

        # then
        assert namespace.class_registry[class_lookup_key] == {injectable}
Ejemplo n.º 3
0
    def test__register_injectable__with_class_and_qualifier(self):
        # given
        injectable = MagicMock(spec=Injectable)
        klass = TestNamespace
        class_lookup_key = klass.__qualname__
        qualifier = "qualifier"
        namespace = Namespace()

        # when
        namespace.register_injectable(injectable, klass, qualifier)

        # then
        assert namespace.class_registry[class_lookup_key] == {injectable}
        assert namespace.qualifier_registry[qualifier] == {injectable}
Ejemplo n.º 4
0
    def test__init__(self):
        # when
        namespace = Namespace()

        # then
        assert namespace.class_registry == {}
        assert namespace.qualifier_registry == {}
Ejemplo n.º 5
0
    def test__register_injectable__with_propagation_disabled(self):
        # given
        injectable = MagicMock(spec=Injectable)
        base_class = TestNamespace
        base_class_lookup_key = base_class.__qualname__

        class Child(base_class):
            ...

        child_class = Child
        child_class_lookup_key = child_class.__qualname__
        namespace = Namespace()

        # when
        namespace.register_injectable(injectable, child_class, propagate=False)

        # then
        assert namespace.class_registry[child_class_lookup_key] == {injectable}
        assert base_class_lookup_key not in namespace.class_registry
Ejemplo n.º 6
0
    def load(
        cls,
        search_path: str = None,
        *,
        default_namespace: str = DEFAULT_NAMESPACE,
    ):
        """
        Loads injectables under the search path to the :class:`InjectionContainer`
        under the designated namespaces.

        :param search_path: (optional) path under which to search for injectables. Can
                be either a relative or absolute path. Defaults to the caller's file
                directory.
        :param default_namespace: (optional) designated namespace for registering
                injectables which does not explicitly request to be addressed in a
                specific namespace. Defaults to
                :const:`injectable.constants.DEFAULT_NAMESPACE`.

        Usage::

          >>> from injectable import InjectionContainer
          >>> InjectionContainer.load()

        .. note::

            This method will not scan any file more than once regardless of being
            called successively. Multiple invocations to different search paths will
            add found injectables to the :class:`InjectionContainer` without clearing
            previously found ones.

        .. deprecated:: 3.4.0
            This method will be removed from the public API in the future. Use
            :func:`load_injection_container` instead.
        """
        warnings.warn(
            "Using 'load' directly from the 'InjectionContainer' is deprecated."
            " Use 'load_injection_container' instead. This class will be removed from"
            " the injectable's public API in the future.",
            DeprecationWarning,
            2,
        )
        cls.LOADING_DEFAULT_NAMESPACE = default_namespace
        if default_namespace not in cls.NAMESPACES:
            cls.NAMESPACES[default_namespace] = Namespace()
        if search_path is None:
            search_path = os.path.dirname(get_caller_filepath())
        elif not os.path.isabs(search_path):
            caller_path = os.path.dirname(get_caller_filepath())
            search_path = os.path.normpath(
                os.path.join(caller_path, search_path))
        cls._link_dependencies(search_path)
        cls.LOADING_DEFAULT_NAMESPACE = None
Ejemplo n.º 7
0
 def load_dependencies_from(cls,
                            absolute_search_path: str,
                            default_namespace: str,
                            encoding: str = "utf-8"):
     files = cls._collect_python_files(absolute_search_path)
     cls.LOADING_DEFAULT_NAMESPACE = default_namespace
     if default_namespace not in cls.NAMESPACES:
         cls.NAMESPACES[default_namespace] = Namespace()
     for file in files:
         if not cls._contains_injectables(file, encoding):
             continue
         if file.path in cls.LOADED_FILEPATHS:
             continue
         cls.LOADING_FILEPATH = file.path
         try:
             run_module(module_finder.find_module_name(file.path))
         except AttributeError:
             # This is needed for some corner cases involving pytest
             # See more at https://github.com/pytest-dev/pytest/issues/9007
             run_path(file.path)
         cls.LOADED_FILEPATHS.add(file.path)
         cls.LOADING_FILEPATH = None
     cls.LOADING_DEFAULT_NAMESPACE = None
Ejemplo n.º 8
0
 def _get_namespace_entry(cls, namespace: str) -> Namespace:
     if namespace not in cls.NAMESPACES:
         cls.NAMESPACES[namespace] = Namespace()
     return cls.NAMESPACES[namespace]