diff --git a/src/quart_imp/imp.py b/src/quart_imp/imp.py index 0ca3960..21929b1 100644 --- a/src/quart_imp/imp.py +++ b/src/quart_imp/imp.py @@ -187,6 +187,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: """ @@ -206,6 +226,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] diff --git a/src/quart_imp/imp_blueprint.py b/src/quart_imp/imp_blueprint.py index 87ae14c..c72b2ba 100644 --- a/src/quart_imp/imp_blueprint.py +++ b/src/quart_imp/imp_blueprint.py @@ -100,6 +100,31 @@ class ImpBlueprint(Blueprint): raise ImportError( 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: """