feat: add model and extension autoloading
This commit is contained in:
parent
5e4fabb24c
commit
e8141ad8f7
@ -188,6 +188,26 @@ class Imp:
|
||||
if isinstance(potential_blueprint, Blueprint):
|
||||
self._quart_blueprint_registration(potential_blueprint)
|
||||
|
||||
def import_model(self, model: str) -> None:
|
||||
if Path(model).is_absolute():
|
||||
model_path = Path(model)
|
||||
else:
|
||||
model_path = Path(self.app_path / model)
|
||||
|
||||
if model_path.exists() and not model_path.name.startswith("__"):
|
||||
module = import_module(cast_to_import_str(self.app_name, model_path))
|
||||
|
||||
def import_extension(self, ext: str) -> None:
|
||||
if Path(model).is_absolute():
|
||||
ext_path = Path(ext)
|
||||
else:
|
||||
ext_path = Path(self.app_path / ext)
|
||||
|
||||
if model_path.exists() and not model_path.name.startswith("__"):
|
||||
module = import_module(cast_to_import_str(self.app_name, model_path))
|
||||
module.register_module(self.app)
|
||||
|
||||
|
||||
def import_blueprints(self, folder: str) -> None:
|
||||
"""
|
||||
Import all blueprints from the given folder.
|
||||
@ -207,6 +227,34 @@ class Imp:
|
||||
for potential_bp in folder_path.iterdir():
|
||||
self.import_blueprint(f"{potential_bp}")
|
||||
|
||||
def import_models(self, folder: str) -> None:
|
||||
"""
|
||||
Will import all models from the given folder.
|
||||
Given folder must be relative to the
|
||||
"""
|
||||
folder_path = Path(self.app_path / folder)
|
||||
if not folder_path.exists():
|
||||
raise ImportError(f"Cannot find models folder at {folder_path}")
|
||||
if not folder_path.is_dir():
|
||||
raise ImportError(f"Models must be a folder {folder_path}")
|
||||
|
||||
for potential_model in folder_path.iterdir():
|
||||
self.import_model(potential_model)
|
||||
|
||||
def import_extensions(self, folder: str) -> None:
|
||||
"""
|
||||
Will import all extensions from the given folder.
|
||||
Given folder must be relative to the
|
||||
"""
|
||||
folder_path = Path(self.app_path / folder)
|
||||
if not folder_path.exists():
|
||||
raise ImportError(f"Cannot find extensions folder at {folder_path}")
|
||||
if not folder_path.is_dir():
|
||||
raise ImportError(f"Extensions must be a folder {folder_path}")
|
||||
|
||||
for potential_ext in folder_path.iterdir():
|
||||
self.import_model(potential_ext)
|
||||
|
||||
async def _async_import_resource_module(
|
||||
self, module: Path, factories: t.List[str]
|
||||
) -> None:
|
||||
|
@ -101,6 +101,31 @@ class ImpBlueprint(Blueprint):
|
||||
f"Error when importing {self.package}.{resource}: {e}"
|
||||
)
|
||||
|
||||
def import_models(self, folder: str = "models") -> None:
|
||||
"""
|
||||
Will import all the models from the given folder.
|
||||
Given folder must be relative to the blueprint (in the same folder as the __init__.py file).
|
||||
|
||||
:param folder: Folder to look for models in. Must be relative.
|
||||
:return: None
|
||||
"""
|
||||
|
||||
if self._prevent_if_disabled():
|
||||
return
|
||||
|
||||
model_path = self.location / folder
|
||||
if not model_path.exists():
|
||||
raise NotADirectoryError(f"{model_path} is not a directory")
|
||||
|
||||
models = model_path.glob("*.py")
|
||||
for model in models:
|
||||
try:
|
||||
import_module(f"{self.package}.{folder}.{model.stem}")
|
||||
except ImportsError as e:
|
||||
raise ImportError(
|
||||
f"Error when importing {self.package}.{model}: {e}"
|
||||
)
|
||||
|
||||
def import_nested_blueprint(self, blueprint: t.Union[str, Path]) -> None:
|
||||
"""
|
||||
Imports the specified Quart-Imp Blueprint or a standard Quart Blueprint as a nested blueprint,
|
||||
|
Loading…
x
Reference in New Issue
Block a user