Skip to content

Commit

Permalink
Make all config available in ContainerBuilder
Browse files Browse the repository at this point in the history
No longer allow a code_generator to be passed as a param.
Instead builder receives all options an creates the code generator
  • Loading branch information
GabrielCappelli committed Jul 30, 2023
1 parent d4c75dc commit 22afecf
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
22 changes: 19 additions & 3 deletions meta_di/builder.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import Dict, Generic, Optional, Type
from typing import Any, Dict, Generic, Optional, Set, Type

from meta_di.code_formatter import DEFAULT_CODE_FORMATTER, CodeFormatterProto
from meta_di.code_generator import CodeGenerator
from meta_di.container_proto import ContainerProto
from meta_di.exceptions import CannotInferProvider
Expand All @@ -16,13 +17,28 @@ class ContainerBuilder(Generic[ServiceId_T]):
def __init__(
self,
inspector: InspectorProto[ServiceId_T] = TypeHintInspector(),
code_generator: Optional[CodeGenerator] = None,
code_formatter: Optional[CodeFormatterProto] = DEFAULT_CODE_FORMATTER,
preload_singleton_instances: bool = True,
container_svc_ids: Optional[Set[Any]] = None,
) -> None:
"""
inspector: InspectorProto to use to inspect the dependencies of services. Defaults to TypeHintInspector
code_formatter: CodeFormatterProto to use to format the generated code. Defaults to black if installed
preload_singleton_instances: If true, singleton instances will be created when the container is instantiated. Defaults to True
container_svc_ids: Set of service identifiers that identify the container. Defaults to {ContainerProto, "di_container"}
"""
self._service_descriptors_map: Dict[
ServiceId_T, ServiceDescriptor[ServiceId_T]
] = {}

self._inspector = inspector
self._code_generator = code_generator or CodeGenerator()

self._code_generator = CodeGenerator(
inspector=inspector,
code_formatter=code_formatter,
preload_singleton_instances=preload_singleton_instances,
container_svc_ids=container_svc_ids,
)

def _add_service(
self,
Expand Down
15 changes: 7 additions & 8 deletions meta_di/code_generator.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from typing import Any, Mapping, Optional, Set, Type, Union

from meta_di.code_formatter import DEFAULT_CODE_FORMATTER, CodeFormatterProto
from meta_di.code_formatter import CodeFormatterProto
from meta_di.container_proto import ContainerProto
from meta_di.exceptions import MissingServiceError
from meta_di.inspector import InspectorProto, TypeHintInspector
from meta_di.inspector import InspectorProto
from meta_di.service_descriptor import ServiceDescriptor
from meta_di.typing import ServiceId_T

Expand All @@ -16,19 +16,18 @@ class CodeGenerator:
_singleton_instances_attr = "self._singleton_instances"
_scoped_instances_attr = "self._scoped_instances"
_service_getter_map_attr = "self._service_getter_map"
_default_container_svc_ids = set([ContainerProto, "di_container"])

def __init__(
self,
code_formatter: CodeFormatterProto = DEFAULT_CODE_FORMATTER,
inspector: InspectorProto[Any] = TypeHintInspector(),
preload_singleton_instances: bool = True,
container_svc_ids: Optional[Set[Any]] = None,
code_formatter: Optional[CodeFormatterProto],
inspector: InspectorProto[Any],
preload_singleton_instances: bool,
container_svc_ids: Optional[Set[Any]],
) -> None:
self._code_formatter = code_formatter
self._inspector = inspector
self._preload_singleton_instances = preload_singleton_instances
self._container_svc_ids = container_svc_ids or self._default_container_svc_ids
self._container_svc_ids = container_svc_ids or {ContainerProto, "di_container"}

def _get_getter_method_name(self, svc_desc: ServiceDescriptor[Any]) -> str:
"""
Expand Down

0 comments on commit 22afecf

Please sign in to comment.