-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
135 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from modelscan.middlewares.format_via_extension import FormatViaExtensionMiddleware |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from .middleware import MiddlewareBase | ||
from modelscan.model import Model | ||
from typing import Callable | ||
|
||
|
||
class FormatViaExtensionMiddleware(MiddlewareBase): | ||
def __call__(self, model: Model, call_next: Callable[[Model], None]) -> None: | ||
extension = model.get_source().suffix | ||
formats = [ | ||
format | ||
for format, extensions in self._settings["formats"].items() | ||
if extension in extensions | ||
] | ||
if len(formats) > 0: | ||
model.set_context("formats", model.get_context("formats") or [] + formats) | ||
|
||
call_next(model) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import abc | ||
from modelscan.model import Model | ||
from typing import Callable, Dict, Any, List | ||
import importlib | ||
|
||
|
||
class MiddlewareImportError(Exception): | ||
pass | ||
|
||
|
||
class MiddlewareBase(metaclass=abc.ABCMeta): | ||
_settings: Dict[str, Any] | ||
|
||
def __init__(self, settings: Dict[str, Any]): | ||
self._settings = settings | ||
|
||
@abc.abstractmethod | ||
def __call__( | ||
self, | ||
model: Model, | ||
call_next: Callable[[Model], None], | ||
) -> None: | ||
raise NotImplementedError | ||
|
||
|
||
class MiddlewarePipeline: | ||
_middlewares: List[MiddlewareBase] | ||
|
||
def __init__(self) -> None: | ||
self._middlewares = [] | ||
|
||
@staticmethod | ||
def from_settings(middleware_settings: Dict[str, Any]) -> "MiddlewarePipeline": | ||
pipeline = MiddlewarePipeline() | ||
|
||
for path, params in middleware_settings.items(): | ||
try: | ||
(modulename, classname) = path.rsplit(".", 1) | ||
imported_module = importlib.import_module( | ||
name=modulename, package=classname | ||
) | ||
|
||
middleware_class: MiddlewareBase = getattr(imported_module, classname) | ||
pipeline.add_middleware(middleware_class(params)) # type: ignore | ||
except Exception as e: | ||
raise MiddlewareImportError(f"Error importing middleware {path}: {e}") | ||
|
||
return pipeline | ||
|
||
def add_middleware(self, middleware: MiddlewareBase) -> "MiddlewarePipeline": | ||
self._middlewares.append(middleware) | ||
return self | ||
|
||
def run(self, model: Model) -> None: | ||
def runner(model: Model, index: int) -> None: | ||
if index < len(self._middlewares): | ||
self._middlewares[index](model, lambda model: runner(model, index + 1)) | ||
|
||
runner(model, 0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters