Example #1
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
 def decorator(klass: T, direct_call: bool = False) -> T:
     steps_back = 3 if direct_call else 2
     caller_filepath = get_caller_filepath(steps_back)
     if caller_filepath == InjectionContainer.LOADING_FILEPATH:
         InjectionContainer._register_injectable(
             klass, caller_filepath, qualifier, primary, namespace, group, singleton
         )
     return klass
Example #3
0
    def test__get_caller_filepath__with_1_step_back(self):
        # given
        expected = __file__

        # when
        filepath = get_caller_filepath(steps_back=1)

        # then
        assert filepath == expected
Example #4
0
def load_injection_container(
    search_path: str = None,
    *,
    default_namespace: str = DEFAULT_NAMESPACE,
    encoding: str = "utf-8",
):
    """
    Loads injectables under the search path to a shared injection container 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`.
    :param encoding: (optional) defines which encoding to use when reading project files
            to discover and register injectables. Defaults to ``utf-8``.

    Usage::

      >>> from injectable import load_injection_container
      >>> load_injection_container()

    .. note::

        This method will not scan any file already scanned by previous calls to it.
        Multiple invocations to different search paths will add found injectables into
        the injection container without clearing previously loaded ones but never
        loading a same injectable more than once.

    .. versionadded:: 3.4.0
    """
    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.abspath(os.path.join(caller_path, search_path))
    InjectionContainer.load_dependencies_from(search_path, default_namespace,
                                              encoding)
 def decorator(fn: Callable[..., T]) -> Callable[..., T]:
     caller_filepath = get_caller_filepath()
     if caller_filepath == InjectionContainer.LOADING_FILEPATH:
         InjectionContainer._register_factory(
             fn,
             caller_filepath,
             dependency,
             qualifier,
             primary,
             namespace,
             group,
             singleton,
         )
     return fn