Skip to content

Commit

Permalink
Refactor filter decorators to use new index system
Browse files Browse the repository at this point in the history
  • Loading branch information
MaddyGuthridge committed Jul 28, 2023
1 parent a382365 commit 8c7d4e6
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 43 deletions.
2 changes: 0 additions & 2 deletions src/plugs/event_filters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"""
__all__ = [
'filterButtonLift',
'toSafeIndex',
'toWindowIndex',
'toPluginIndex',
'toGeneratorIndex',
Expand All @@ -20,7 +19,6 @@

from .filters import filterButtonLift
from .index import (
toSafeIndex,
toWindowIndex,
toPluginIndex,
toGeneratorIndex,
Expand Down
6 changes: 3 additions & 3 deletions src/plugs/event_filters/decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
more details.
"""
from typing import Any, Callable
from common.plug_indexes import UnsafeIndex
from common.plug_indexes import FlIndex
from control_surfaces import ControlShadowEvent
from devices.device_shadow import EventCallback

Expand Down Expand Up @@ -53,7 +53,7 @@ def wrapper_creator(func) -> EventCallback:
def wrapper_method(
self,
control: ControlShadowEvent,
index: UnsafeIndex,
index: FlIndex,
*args: Any,
**kwargs: Any
) -> bool:
Expand All @@ -73,7 +73,7 @@ def wrapper_method(
else:
def wrapper(
control: ControlShadowEvent,
index: UnsafeIndex,
index: FlIndex,
*args: Any,
**kwargs: Any
) -> bool:
Expand Down
32 changes: 15 additions & 17 deletions src/plugs/event_filters/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,45 +9,43 @@
This code is licensed under the GPL v3 license. Refer to the LICENSE file for
more details.
"""
from common.plug_indexes import UnsafeIndex
from common.plug_indexes import (
FlIndex,
PluginIndex,
GeneratorIndex,
EffectIndex,
WindowIndex,
)
from .decorator import do_filter


@do_filter
def toPluginIndex(control, index: UnsafeIndex, *args, **kwargs):
def toPluginIndex(control, index: FlIndex, *args, **kwargs):
"""
Filter out events when the index is not a plugin
"""
return isinstance(index, tuple)
return isinstance(index, PluginIndex)


@do_filter
def toGeneratorIndex(control, index: UnsafeIndex, *args, **kwargs):
def toGeneratorIndex(control, index: FlIndex, *args, **kwargs):
"""
Filter out events when the index is not a generator plugin
"""
return isinstance(index, tuple) and len(index) == 1
return isinstance(index, GeneratorIndex)


@do_filter
def toEffectIndex(control, index: UnsafeIndex, *args, **kwargs):
def toEffectIndex(control, index: FlIndex, *args, **kwargs):
"""
Filter out events when the index is not an effect plugin
"""
return isinstance(index, tuple) and len(index) == 2
return isinstance(index, EffectIndex)


@do_filter
def toWindowIndex(control, index: UnsafeIndex, *args, **kwargs):
def toWindowIndex(control, index: FlIndex, *args, **kwargs):
"""
Filter out events when the index is not a window
"""
return isinstance(index, int)


@do_filter
def toSafeIndex(control, index: UnsafeIndex, *args, **kwargs):
"""
Filter out events when the index is None
"""
return index is not None
return isinstance(index, WindowIndex)
8 changes: 4 additions & 4 deletions src/plugs/tick_filters/decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
"""
from typing import Callable
from typing_extensions import TypeAlias
from common.plug_indexes import UnsafeIndex
from common.plug_indexes import FlIndex

TickCallback: TypeAlias = Callable[[UnsafeIndex], bool]
TickCallback: TypeAlias = Callable[[FlIndex], bool]


def do_filter(callback: TickCallback):
Expand Down Expand Up @@ -52,7 +52,7 @@ def wrapper_creator(func) -> TickCallback:
if method:
def wrapper_method(
self,
index: UnsafeIndex,
index: FlIndex,
*args,
) -> bool:
if callback(index):
Expand All @@ -64,7 +64,7 @@ def wrapper_method(
return wrapper_method # type: ignore
else:
def wrapper(
index: UnsafeIndex,
index: FlIndex,
*args,
) -> bool:
if callback(index):
Expand Down
32 changes: 15 additions & 17 deletions src/plugs/tick_filters/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,45 +9,43 @@
This code is licensed under the GPL v3 license. Refer to the LICENSE file for
more details.
"""
from common.plug_indexes import UnsafeIndex
from common.plug_indexes import (
FlIndex,
PluginIndex,
GeneratorIndex,
EffectIndex,
WindowIndex,
)
from .decorator import do_filter


@do_filter
def toPluginIndex(index: UnsafeIndex):
def toPluginIndex(index: FlIndex):
"""
Filter out events when the index is not a plugin
"""
return isinstance(index, tuple)
return isinstance(index, PluginIndex)


@do_filter
def toGeneratorIndex(index: UnsafeIndex):
def toGeneratorIndex(index: FlIndex):
"""
Filter out events when the index is not a generator plugin
"""
return isinstance(index, tuple) and len(index) == 1
return isinstance(index, GeneratorIndex)


@do_filter
def toEffectIndex(index: UnsafeIndex):
def toEffectIndex(index: FlIndex):
"""
Filter out events when the index is not an effect plugin
"""
return isinstance(index, tuple) and len(index) == 2
return isinstance(index, EffectIndex)


@do_filter
def toWindowIndex(index: UnsafeIndex):
def toWindowIndex(index: FlIndex):
"""
Filter out events when the index is not a window
"""
return isinstance(index, int)


@do_filter
def toSafeIndex(index: UnsafeIndex):
"""
Filter out events when the index is None
"""
return index is not None
return isinstance(index, WindowIndex)

0 comments on commit 8c7d4e6

Please sign in to comment.