Skip to content

base

Classes:

  • PropEnum

    Base class for enumerations representing frame or clip properties in VapourSynth.

PropEnum

PropEnum(_: Any, string: str | None = None, pretty_string: str | None = None)

Bases: CustomIntEnum

Base class for enumerations representing frame or clip properties in VapourSynth.

Methods:

  • apply

    Applies the property to the VideoNode.

  • ensure_presence

    Ensure the presence of the property in the VideoNode.

  • ensure_presences

    Ensure the presence of multiple PropEnums at once.

  • from_param

    Return the enum value from a parameter.

  • from_param_or_video

    Get the enum member from a value that can be casted to this prop value or grab it from frame properties.

  • from_param_with_fallback

    Attempts to obtain an enum member from a parameter value, returning a fallback if the value cannot be converted

  • from_res

    Get an enum member from the video resolution with heuristics.

  • from_string

    Get the enum member whose string attribute matches the given value.

  • from_video

    Get an enum member from the frame properties or optionally fall back to resolution when strict=False.

  • pretty_string

    Get a pretty, displayable string of the enum member.

  • prop_key

    The key used in props to store the enum.

  • string

    Get the string representation used in resize plugin/encoders.

  • value

Attributes:

Source code in vstools/enums/base.py
36
37
38
def __init__(self, _: Any, string: str | None = None, pretty_string: str | None = None) -> None:
    self._string = fallback(string, self._name_.lower())
    self._pretty_string = fallback(pretty_string, capwords(self._string.replace("_", " ")))

is_unspecified property

is_unspecified: bool

Whether the value is unspecified.

apply

apply(clip: VideoNode) -> VideoNode

Applies the property to the VideoNode.

Source code in vstools/enums/base.py
79
80
81
82
83
def apply(self, clip: vs.VideoNode) -> vs.VideoNode:
    """
    Applies the property to the VideoNode.
    """
    return clip.std.SetFrameProp(self.prop_key, self.value)

ensure_presence classmethod

ensure_presence(
    clip: VideoNode, value: Any, func: FuncExcept | None = None
) -> VideoNode

Ensure the presence of the property in the VideoNode.

Source code in vstools/enums/base.py
187
188
189
190
191
192
193
194
@classmethod
def ensure_presence(cls, clip: vs.VideoNode, value: Any, func: FuncExcept | None = None) -> vs.VideoNode:
    """
    Ensure the presence of the property in the VideoNode.
    """
    enum_value = cls.from_param_or_video(value, clip, True, func)

    return clip.std.SetFrameProp(enum_value.prop_key, enum_value.value)

ensure_presences staticmethod

ensure_presences(
    clip: VideoNode,
    prop_enums: Iterable[type[PropEnum] | PropEnum],
    func: FuncExcept | None = None,
) -> VideoNode

Ensure the presence of multiple PropEnums at once.

Source code in vstools/enums/base.py
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
@staticmethod
def ensure_presences(
    clip: vs.VideoNode, prop_enums: Iterable[type[PropEnum] | PropEnum], func: FuncExcept | None = None
) -> vs.VideoNode:
    """
    Ensure the presence of multiple PropEnums at once.
    """
    return clip.std.SetFrameProps(
        **{
            value.prop_key: value.value
            for value in (
                prop_enum if isinstance(prop_enum, PropEnum) else prop_enum.from_video(clip, True, func)
                for prop_enum in prop_enums
            )
        },
    )

from_param classmethod

from_param(value: Any, func_except: FuncExcept | None = None) -> Self

Return the enum value from a parameter.

Parameters:

  • value

    (Any) –

    Value to instantiate the enum class.

  • func_except

    (FuncExcept | None, default: None ) –

    Exception function.

Returns:

  • Self

    Enum value.

Raises:

Source code in jetpytools/enums/base.py
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
@classmethod
def from_param(cls, value: Any, func_except: FuncExcept | None = None) -> Self:
    """
    Return the enum value from a parameter.

    Args:
        value: Value to instantiate the enum class.
        func_except: Exception function.

    Returns:
        Enum value.

    Raises:
        NotFoundEnumValue: Variable not found in the given enum.
    """
    func_except = func_except or cls.from_param

    try:
        return cls(value)
    except (ValueError, TypeError):
        pass

    if isinstance(func_except, tuple):
        func_name, var_name = func_except
    else:
        func_name, var_name = func_except, repr(cls)

    raise NotFoundEnumValueError(
        'The given value for "{var_name}" argument must be a valid {enum_name}, not "{value}"!\n'
        "Valid values are: [{readable_enum}].",
        func_name,
        var_name=var_name,
        enum_name=cls,
        value=value,
        readable_enum=(f"{name} ({value!r})" for name, value in cls.__members__.items()),
        reason=value,
    ) from None

from_param_or_video classmethod

from_param_or_video(
    value: Any,
    src: VideoNode | VideoFrame | Mapping[str, Any],
    strict: bool = False,
    func_except: FuncExcept | None = None,
) -> Self

Get the enum member from a value that can be casted to this prop value or grab it from frame properties.

If strict=False, gather the heuristics using the clip's size or format.

Parameters:

  • value

    (Any) –

    Value to cast.

  • src

    (VideoNode | VideoFrame | Mapping[str, Any]) –

    Clip to get prop from.

  • strict

    (bool, default: False ) –

    Be strict about the frame properties. Default: False.

  • func_except

    (FuncExcept | None, default: None ) –

    Function returned for custom error handling.

Source code in vstools/enums/base.py
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
@classmethod
def from_param_or_video(
    cls,
    value: Any,
    src: vs.VideoNode | vs.VideoFrame | Mapping[str, Any],
    strict: bool = False,
    func_except: FuncExcept | None = None,
) -> Self:
    """
    Get the enum member from a value that can be casted to this prop value or grab it from frame properties.

    If `strict=False`, gather the heuristics using the clip's size or format.

    Args:
        value: Value to cast.
        src: Clip to get prop from.
        strict: Be strict about the frame properties. Default: False.
        func_except: Function returned for custom error handling.
    """
    prop = cls.from_param_with_fallback(value, None)

    # Delay the `from_video` call here to avoid unnecessary frame rendering
    if prop is None:
        return cls.from_video(src, strict, func_except)

    return prop

from_param_with_fallback classmethod

from_param_with_fallback(value: Any, fallback: None = None) -> Self | None
from_param_with_fallback(value: Any, fallback: T) -> Self | T
from_param_with_fallback(
    value: Any, fallback: T | None = None
) -> Self | T | None

Attempts to obtain an enum member from a parameter value, returning a fallback if the value cannot be converted or represents an unspecified enum member.

Parameters:

  • value

    (Any) –

    The input value to convert into an enum member.

  • fallback

    (T | None, default: None ) –

    The value to return if the input cannot be converted. Defaults to None.

Returns:

  • Self | T | None

    The corresponding enum member if conversion succeeds, otherwise the provided fallback value.

Source code in vstools/enums/base.py
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
@classmethod
def from_param_with_fallback[T](cls, value: Any, fallback: T | None = None) -> Self | T | None:
    """
    Attempts to obtain an enum member from a parameter value, returning a fallback if the value cannot be converted
    or represents an unspecified enum member.

    Args:
        value: The input value to convert into an enum member.
        fallback: The value to return if the input cannot be converted. Defaults to `None`.

    Returns:
        The corresponding enum member if conversion succeeds, otherwise the provided `fallback` value.
    """
    with suppress(NotFoundEnumValueError, CustomValueError):
        # Will raise a NotFoundEnumValueError if the value can't be casted
        casted = cls.from_param(value)

        # If unspecified, fallbacks to `fallback` value
        if casted.is_unspecified:
            raise CustomValueError

        return casted

    return fallback

from_res abstractmethod classmethod

from_res(frame: VideoNode | VideoFrame) -> Self

Get an enum member from the video resolution with heuristics.

Source code in vstools/enums/base.py
85
86
87
88
89
90
@classmethod
@abstractmethod
def from_res(cls, frame: vs.VideoNode | vs.VideoFrame) -> Self:
    """
    Get an enum member from the video resolution with heuristics.
    """

from_string classmethod

from_string(string: str, func: FuncExcept | None = None) -> Self

Get the enum member whose string attribute matches the given value.

Parameters:

  • string

    (str) –

    The string to match against the enum members string attribute.

Raises:

Returns:

  • Self

    The matching enum member.

Source code in vstools/enums/base.py
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
@classmethod
def from_string(cls, string: str, func: FuncExcept | None = None) -> Self:
    """
    Get the enum member whose `string` attribute matches the given value.

    Args:
        string: The string to match against the enum members `string` attribute.

    Raises:
        NotFoundEnumValueError: If no enum member has a `string` value equal to the provided one.
        CustomOverflowError: If multiple enum members share the same `string` value, making the lookup ambiguous.

    Returns:
        The matching enum member.
    """

    matched_strings = [m for m in cls if m.string == string]

    if not matched_strings:
        raise NotFoundEnumValueError(f"No enum member found with string value {string!r}.", func)

    if len(matched_strings) > 1:
        raise CustomOverflowError(
            f"Multiple enum members found with string value {string!r}; expected exactly one.", func
        )

    return matched_strings.pop()

from_video abstractmethod classmethod

from_video(
    src: VideoNode | VideoFrame | Mapping[str, Any],
    strict: bool = False,
    func: FuncExcept | None = None,
) -> Self

Get an enum member from the frame properties or optionally fall back to resolution when strict=False.

Source code in vstools/enums/base.py
92
93
94
95
96
97
98
99
@classmethod
@abstractmethod
def from_video(
    cls, src: vs.VideoNode | vs.VideoFrame | Mapping[str, Any], strict: bool = False, func: FuncExcept | None = None
) -> Self:
    """
    Get an enum member from the frame properties or optionally fall back to resolution when strict=False.
    """

pretty_string

pretty_string() -> str

Get a pretty, displayable string of the enum member.

Source code in vstools/enums/base.py
57
58
59
60
61
62
@cachedproperty
def pretty_string(self) -> str:
    """
    Get a pretty, displayable string of the enum member.
    """
    return self._pretty_string

prop_key classmethod

prop_key() -> str

The key used in props to store the enum.

Source code in vstools/enums/base.py
71
72
73
74
75
76
77
@classproperty
@classmethod
def prop_key(cls) -> str:
    """
    The key used in props to store the enum.
    """
    return f"_{cls.__name__}"

string

string() -> str

Get the string representation used in resize plugin/encoders.

Source code in vstools/enums/base.py
64
65
66
67
68
69
@cachedproperty
def string(self) -> str:
    """
    Get the string representation used in resize plugin/encoders.
    """
    return self._string

value

value() -> int
Source code in jetpytools/enums/base.py
86
87
@enum_property
def value(self) -> int: ...