33
44import py_spring_core .core .utils as core_utils
55from loguru import logger
6- from py_spring_core import Component , EntityProvider
7- from py_spring_core .core .application .context .application_context import (
8- ApplicationContext ,
9- )
6+ from py_spring_core import Component , EntityProvider , ApplicationContextRequired
107from sqlalchemy import create_engine
118from sqlalchemy .exc import InvalidRequestError as SqlAlehemyInvalidRequestError
129from sqlmodel import SQLModel
2623class ApplicationContextNotSetError (Exception ): ...
2724
2825
29- class PySpringModelProvider (EntityProvider , Component ):
26+ class PySpringModelProvider (EntityProvider , Component , ApplicationContextRequired ):
3027 """
3128 The `PySpringModelProvider` class is responsible for initializing the PySpring model provider, which includes:
3229 - Grouping file paths into class files and model files
@@ -38,15 +35,22 @@ class PySpringModelProvider(EntityProvider, Component):
3835 This class is a key component in the PySpring model infrastructure, handling the setup and initialization of the model-related functionality.
3936 """
4037
41- props : PySpringModelProperties
38+ def _get_props (self ) -> PySpringModelProperties :
39+ app_context = self .get_application_context ()
40+ assert app_context is not None
41+ props = app_context .get_properties (PySpringModelProperties )
42+ assert props is not None
43+ return props
4244
4345 def _group_file_paths (self , files : Iterable [str ]) -> ApplicationFileGroups :
46+ props = self ._get_props ()
47+
4448 class_files : set [str ] = set ()
4549 model_files : set [str ] = set ()
4650
4751 for file in files :
4852 py_file_name = self ._get_file_base_name (file )
49- if py_file_name in self . props .model_file_postfix_patterns :
53+ if py_file_name in props .model_file_postfix_patterns :
5054 model_files .add (file )
5155 if file not in model_files :
5256 class_files .add (file )
@@ -76,6 +80,7 @@ def import_func_wrapper() -> set[type[object]]:
7680 self ._model_classes = self ._get_pyspring_model_inheritors ()
7781
7882 def _is_from_model_file (self , cls : Type [object ]) -> bool :
83+ props = self ._get_props ()
7984 try :
8085 source_file_name = inspect .getsourcefile (cls )
8186 except TypeError as error :
@@ -86,7 +91,7 @@ def _is_from_model_file(self, cls: Type[object]) -> bool:
8691 if source_file_name is None :
8792 return False
8893 py_file_name = self ._get_file_base_name (source_file_name ) # e.g., models.py
89- return py_file_name in self . props .model_file_postfix_patterns
94+ return py_file_name in props .model_file_postfix_patterns
9095
9196 def _get_file_base_name (self , file_path : str ) -> str :
9297 return file_path .split ("/" )[- 1 ]
@@ -126,34 +131,30 @@ def _create_all_tables(self) -> None:
126131 RepositoryBase .connection = self .sql_engine .connect ()
127132
128133 def provider_init (self ) -> None :
129- self .app_context : ApplicationContext
134+ props = self ._get_props ()
130135 logger .info (
131136 f"[PYSPRING MODEL PROVIDER INIT] Initialize PySpringModelProvider with app context: { self .app_context } "
132137 )
133- if self .app_context is None :
134- raise ApplicationContextNotSetError (
135- "AppContext is not set by the framework"
136- )
138+ app_context = self .get_application_context ()
137139
138- self .app_file_groups = self ._group_file_paths (self . app_context .all_file_paths )
140+ self .app_file_groups = self ._group_file_paths (app_context .all_file_paths )
139141 self .sql_engine = create_engine (
140- url = self . props .sqlalchemy_database_uri , echo = True
142+ url = props .sqlalchemy_database_uri , echo = True
141143 )
142- if self .app_context is None :
143- raise ApplicationContextNotSetError (
144- "AppContext is not set by the framework"
145- )
146144 self ._import_model_modules ()
147145 self ._create_all_tables ()
148146
149147
150148def provide_py_spring_model () -> EntityProvider :
151149 return PySpringModelProvider (
152- rest_controller_classes = [PySpringModelRestController ],
150+ rest_controller_classes = [
151+ # PySpringModelRestController
152+ ],
153153 component_classes = [
154- PySpringModelProvider ,
155- PySpringModelRestService ,
156- CrudRepositoryImplementationService ,
157- ], # injecting self for getting properties
158- properties_classes = [PySpringModelProperties ],
154+ # PySpringModelRestService,
155+ # CrudRepositoryImplementationService,
156+ ],
157+ properties_classes = [
158+ PySpringModelProperties
159+ ],
159160 )
0 commit comments