Skip to content

Commit

Permalink
Add Kernel.ensure_obj
Browse files Browse the repository at this point in the history
  • Loading branch information
Setsugennoao committed Oct 6, 2022
1 parent 5357ad5 commit 773fc35
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
28 changes: 26 additions & 2 deletions vskernels/kernels/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from typing import Any, Union, cast, overload

from vstools import (
FuncExceptT, GenericVSFunction, HoldsVideoFormatT, Matrix, MatrixT, VideoFormatT, core, get_video_format,
inject_self, vs
CustomTypeError, FuncExceptT, GenericVSFunction, HoldsVideoFormatT, Matrix, MatrixT, VideoFormatT, core,
get_video_format, inject_self, vs
)

from ..exceptions import UnknownKernelError
Expand Down Expand Up @@ -202,5 +202,29 @@ def from_param(cls: type[Kernel], kernel: KernelT, func_except: FuncExceptT | No

return kernel

@classmethod
def ensure_obj(
cls: type[Kernel], kernel: KernelT | None = None, func_except: FuncExceptT | None = None
) -> type[Kernel]:
from ..util import excluded_kernels

new_kernel: Kernel | None = None

if not isinstance(kernel, Kernel):
try:
new_kernel = Kernel.from_param(kernel, func_except)()
except Exception:
...
else:
new_kernel = kernel

if new_kernel is None:
new_kernel = cls()

if new_kernel.__class__ in excluded_kernels:
raise CustomTypeError('This kernel can\'t be instantiated to be used!')

return new_kernel


KernelT = Union[str, type[Kernel], Kernel]
1 change: 1 addition & 0 deletions vskernels/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from .kernels.docs import Example

__all__ = [
'excluded_kernels',
'get_all_kernels',
'NoShift'
]
Expand Down

0 comments on commit 773fc35

Please sign in to comment.