Skip to content

Commit 0e74ddd

Browse files
Refactor method names in PySpringApplication and ApplicationContext for consistency; update component and property container attributes in ContainerManager to improve clarity; adjust related tests to reflect these changes.
1 parent 9a6f774 commit 0e74ddd

File tree

6 files changed

+98
-102
lines changed

6 files changed

+98
-102
lines changed

py_spring_core/core/application/context/application_context.py

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -54,32 +54,32 @@ class ApplicationContextView(BaseModel):
5454
"""View model for application context state."""
5555

5656
config: ApplicationContextConfig
57-
component_cls_container: list[str]
58-
singleton_component_instance_container: list[str]
57+
component_classes: list[str]
58+
component_instances: list[str]
5959

6060

6161
class ContainerManager:
6262
"""Manages containers for different types of entities in the application context."""
6363

6464
def __init__(self):
65-
self.component_cls_container: dict[str, Type[Component]] = {}
66-
self.controller_cls_container: dict[str, Type[RestController]] = {}
67-
self.singleton_component_instance_container: dict[str, Component] = {}
65+
self.component_classes: dict[str, Type[Component]] = {}
66+
self.controller_classes: dict[str, Type[RestController]] = {}
67+
self.component_instances: dict[str, Component] = {}
6868

69-
self.bean_collection_cls_container: dict[str, Type[BeanCollection]] = {}
70-
self.singleton_bean_instance_container: dict[str, object] = {}
69+
self.bean_collection_classes: dict[str, Type[BeanCollection]] = {}
70+
self.bean_instances: dict[str, object] = {}
7171

72-
self.properties_cls_container: dict[str, Type[Properties]] = {}
73-
self.singleton_properties_instance_container: dict[str, Properties] = {}
72+
self.properties_classes: dict[str, Type[Properties]] = {}
73+
self.properties_instances: dict[str, Properties] = {}
7474

7575
def is_entity_in_container(self, entity_cls: Type[AppEntities]) -> bool:
7676
"""Check if an entity class is registered in any container."""
7777
cls_name = entity_cls.__name__
7878
return (
79-
cls_name in self.component_cls_container
80-
or cls_name in self.controller_cls_container
81-
or cls_name in self.bean_collection_cls_container
82-
or cls_name in self.properties_cls_container
79+
cls_name in self.component_classes
80+
or cls_name in self.controller_classes
81+
or cls_name in self.bean_collection_classes
82+
or cls_name in self.properties_classes
8383
)
8484

8585

@@ -241,7 +241,7 @@ def register_component(self, component_cls: Type[Component]) -> None:
241241
)
242242

243243
component_cls_name = component_cls.get_name()
244-
existing_component = self.container_manager.component_cls_container.get(
244+
existing_component = self.container_manager.component_classes.get(
245245
component_cls_name
246246
)
247247

@@ -253,7 +253,7 @@ def register_component(self, component_cls: Type[Component]) -> None:
253253
):
254254
return
255255

256-
self.container_manager.component_cls_container[component_cls_name] = (
256+
self.container_manager.component_classes[component_cls_name] = (
257257
component_cls
258258
)
259259

@@ -266,15 +266,15 @@ def get_component(
266266

267267
target_cls_name = self._determine_target_cls_name(component_cls, qualifier)
268268

269-
if target_cls_name not in self.container_manager.component_cls_container:
269+
if target_cls_name not in self.container_manager.component_classes:
270270
return None
271271

272272
scope = component_cls.get_scope()
273273
match scope:
274274
case ComponentScope.Singleton:
275275
return cast(
276276
T,
277-
self.container_manager.singleton_component_instance_container.get(
277+
self.container_manager.component_instances.get(
278278
target_cls_name
279279
),
280280
)
@@ -340,7 +340,7 @@ def _init_abstract_component_subclasses(self, component_cls: Type[ABC]) -> None:
340340
subclass_component_cls, subclass_component_cls.get_name()
341341
)
342342
if instance is not None:
343-
self.container_manager.singleton_component_instance_container[
343+
self.container_manager.component_instances[
344344
subclass_component_cls.get_name()
345345
] = instance
346346

@@ -349,7 +349,7 @@ def init_singleton_components(self) -> None:
349349
for (
350350
component_cls_name,
351351
component_cls,
352-
) in self.container_manager.component_cls_container.items():
352+
) in self.container_manager.component_classes.items():
353353
if component_cls.get_scope() != ComponentScope.Singleton:
354354
continue
355355

@@ -364,7 +364,7 @@ def init_singleton_components(self) -> None:
364364
component_cls, component_cls_name
365365
)
366366
if instance is not None:
367-
self.container_manager.singleton_component_instance_container[
367+
self.container_manager.component_instances[
368368
component_cls_name
369369
] = instance
370370

@@ -389,18 +389,18 @@ def register_bean_collection(self, bean_cls: Type[BeanCollection]) -> None:
389389
)
390390

391391
bean_name = bean_cls.get_name()
392-
self.container_manager.bean_collection_cls_container[bean_name] = bean_cls
392+
self.container_manager.bean_collection_classes[bean_name] = bean_cls
393393

394394
def get_bean(
395395
self, object_cls: Type[T], qualifier: Optional[str] = None
396396
) -> Optional[T]:
397397
"""Get a bean instance by class and optional qualifier."""
398398
bean_name = object_cls.__name__
399-
if bean_name not in self.container_manager.singleton_bean_instance_container:
399+
if bean_name not in self.container_manager.bean_instances:
400400
return None
401401

402402
return cast(
403-
T, self.container_manager.singleton_bean_instance_container.get(bean_name)
403+
T, self.container_manager.bean_instances.get(bean_name)
404404
)
405405

406406
def _inject_bean_collection_dependencies(
@@ -414,7 +414,7 @@ def _inject_bean_collection_dependencies(
414414

415415
def _validate_bean_view(self, view: BeanView, collection_name: str) -> None:
416416
"""Validate a bean view before adding it to the container."""
417-
if view.bean_name in self.container_manager.singleton_bean_instance_container:
417+
if view.bean_name in self.container_manager.bean_instances:
418418
raise BeanConflictError(
419419
f"[BEAN CONFLICTS] Bean: {view.bean_name} already exists under collection: {collection_name}"
420420
)
@@ -430,7 +430,7 @@ def init_singleton_beans(self) -> None:
430430
for (
431431
bean_collection_cls_name,
432432
bean_collection_cls,
433-
) in self.container_manager.bean_collection_cls_container.items():
433+
) in self.container_manager.bean_collection_classes.items():
434434
logger.debug(
435435
f"[INITIALIZING SINGLETON BEAN] Init singleton bean: {bean_collection_cls_name}"
436436
)
@@ -441,7 +441,7 @@ def init_singleton_beans(self) -> None:
441441
bean_views = collection.scan_beans()
442442
for view in bean_views:
443443
self._validate_bean_view(view, collection.get_name())
444-
self.container_manager.singleton_bean_instance_container[
444+
self.container_manager.bean_instances[
445445
view.bean_name
446446
] = view.bean
447447

@@ -464,19 +464,19 @@ def register_properties(self, properties_cls: Type[Properties]) -> None:
464464
)
465465

466466
properties_name = properties_cls.get_key()
467-
self.container_manager.properties_cls_container[properties_name] = (
467+
self.container_manager.properties_classes[properties_name] = (
468468
properties_cls
469469
)
470470

471471
def get_properties(self, properties_cls: Type[PT]) -> Optional[PT]:
472472
"""Get a properties instance by class."""
473473
properties_cls_name = properties_cls.get_key()
474-
if properties_cls_name not in self.container_manager.properties_cls_container:
474+
if properties_cls_name not in self.container_manager.properties_classes:
475475
return None
476476

477477
return cast(
478478
PT,
479-
self.container_manager.singleton_properties_instance_container.get(
479+
self.container_manager.properties_instances.get(
480480
properties_cls_name
481481
),
482482
)
@@ -485,7 +485,7 @@ def _create_properties_loader(self) -> _PropertiesLoader:
485485
"""Create a properties loader instance."""
486486
return _PropertiesLoader(
487487
self.config.properties_path,
488-
list(self.container_manager.properties_cls_container.values()),
488+
list(self.container_manager.properties_classes.values()),
489489
)
490490

491491
def load_properties(self) -> None:
@@ -496,10 +496,10 @@ def load_properties(self) -> None:
496496
for (
497497
properties_key,
498498
properties_cls,
499-
) in self.container_manager.properties_cls_container.items():
499+
) in self.container_manager.properties_classes.items():
500500
if (
501501
properties_key
502-
in self.container_manager.singleton_properties_instance_container
502+
in self.container_manager.properties_instances
503503
):
504504
continue
505505

@@ -515,13 +515,13 @@ def load_properties(self) -> None:
515515
f"with key: {properties_cls.get_key()}"
516516
)
517517

518-
self.container_manager.singleton_properties_instance_container[
518+
self.container_manager.properties_instances[
519519
properties_key
520520
] = optional_properties
521521

522522
# Update the global properties loader reference
523523
_PropertiesLoader.optional_loaded_properties = (
524-
self.container_manager.singleton_properties_instance_container
524+
self.container_manager.properties_instances
525525
)
526526

527527

@@ -565,11 +565,11 @@ def as_view(self) -> ApplicationContextView:
565565
"""Create a view model of the application context state."""
566566
return ApplicationContextView(
567567
config=self.config,
568-
component_cls_container=list(
569-
self.container_manager.component_cls_container.keys()
568+
component_classes=list(
569+
self.container_manager.component_classes.keys()
570570
),
571-
singleton_component_instance_container=list(
572-
self.container_manager.singleton_component_instance_container.keys()
571+
component_instances=list(
572+
self.container_manager.component_instances.keys()
573573
),
574574
)
575575

@@ -618,25 +618,25 @@ def register_controller(self, controller_cls: Type[RestController]) -> None:
618618
)
619619

620620
controller_cls_name = controller_cls.get_name()
621-
self.container_manager.controller_cls_container[controller_cls_name] = (
621+
self.container_manager.controller_classes[controller_cls_name] = (
622622
controller_cls
623623
)
624624

625625
def get_controller_instances(self) -> list[RestController]:
626626
"""Get all controller instances."""
627627
return [
628-
cls() for cls in self.container_manager.controller_cls_container.values()
628+
cls() for cls in self.container_manager.controller_classes.values()
629629
]
630630

631631
def get_singleton_component_instances(self) -> list[Component]:
632632
"""Get all singleton component instances."""
633633
return list(
634-
self.container_manager.singleton_component_instance_container.values()
634+
self.container_manager.component_instances.values()
635635
)
636636

637637
def get_singleton_bean_instances(self) -> list[object]:
638638
"""Get all singleton bean instances."""
639-
return list(self.container_manager.singleton_bean_instance_container.values())
639+
return list(self.container_manager.bean_instances.values())
640640

641641
def is_within_context(self, entity_cls: Type[AppEntities]) -> bool:
642642
"""Check if an entity class is registered in the application context."""
@@ -663,8 +663,8 @@ def inject_dependencies_for_external_object(self, object: Type[Any]) -> None:
663663
def inject_dependencies_for_app_entities(self) -> None:
664664
"""Inject dependencies for all registered app entities."""
665665
containers: list[Mapping[str, Type[AppEntities]]] = [
666-
self.container_manager.component_cls_container,
667-
self.container_manager.controller_cls_container,
666+
self.container_manager.component_classes,
667+
self.container_manager.controller_classes,
668668
]
669669

670670
for container in containers:

0 commit comments

Comments
 (0)