-
Notifications
You must be signed in to change notification settings - Fork 1
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
18 changed files
with
218 additions
and
108 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
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 |
---|---|---|
@@ -1,7 +1,3 @@ | ||
from . import typing, template, toolchain | ||
|
||
__all__ = [ | ||
"typing", | ||
"template", | ||
"toolchain", | ||
] | ||
from . import typing as typing | ||
from . import template as template | ||
from . import toolchain as toolchain |
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
Empty file.
Empty file.
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,5 @@ | ||
from .accessor import Accessor as Accessor | ||
from .overwrite import OverWrite as OverWrite | ||
from .processor import Processor as Processor | ||
from .intercept import Intercept as Intercept, InvokeEvent as InvokeEvent | ||
from .hook import Pre as Pre, Post as Post |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
from typing import Callable, Optional, Type | ||
|
||
from ..base.typing import RT, T | ||
from ..base.toolchain import ToolChain | ||
from ..base.template import MixinOperation | ||
|
||
|
||
class Post(MixinOperation[Callable[[T], RT]]): | ||
target_name: Optional[str] | ||
|
||
def __init__( | ||
self, | ||
argument: Callable[[T], RT], | ||
target_name: Optional[str] = None, | ||
level: int = 1, | ||
) -> None: | ||
super().__init__(argument, level) | ||
self.target_name = target_name | ||
|
||
@staticmethod | ||
def configure( | ||
level: int = 1, | ||
target_name: Optional[str] = None, | ||
): | ||
return lambda argument: Post( | ||
argument=argument, | ||
target_name=target_name, | ||
level=level, | ||
) | ||
|
||
def mixin(self, target: Type, toolchain: ToolChain = ToolChain()) -> None: | ||
target_name = ( | ||
self.target_name if self.target_name is not None else self.argument.__name__ | ||
) | ||
native_function = toolchain.tool_getattr(target, target_name) | ||
|
||
def post(*args, **kwargs): | ||
result = native_function(*args, **kwargs) | ||
self.argument(result) | ||
return result | ||
|
||
return toolchain.tool_setattr(target, target_name, post) | ||
|
||
|
||
class Pre(MixinOperation[Callable[..., RT]]): | ||
target_name: Optional[str] | ||
|
||
def __init__( | ||
self, | ||
argument: Callable[[T], RT], | ||
target_name: Optional[str] = None, | ||
level: int = 1, | ||
) -> None: | ||
super().__init__(argument, level) | ||
self.target_name = target_name | ||
|
||
@staticmethod | ||
def configure( | ||
level: int = 1, | ||
target_name: Optional[str] = None, | ||
): | ||
return lambda argument: Post( | ||
argument=argument, | ||
target_name=target_name, | ||
level=level, | ||
) | ||
|
||
def mixin(self, target: Type, toolchain: ToolChain = ToolChain()) -> None: | ||
target_name = ( | ||
self.target_name if self.target_name is not None else self.argument.__name__ | ||
) | ||
native_function = toolchain.tool_getattr(target, target_name) | ||
|
||
def pre(*args, **kwargs): | ||
self.argument(*args, **kwargs) | ||
result = native_function(*args, **kwargs) | ||
return result | ||
|
||
return toolchain.tool_setattr(target, target_name, pre) |
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,61 @@ | ||
from typing import Any, Callable, Generic, Optional, Type | ||
|
||
from ..base.typing import RT, T | ||
from ..base.toolchain import InvokeEvent, ToolChain | ||
from ..base.template import MixinOperation | ||
|
||
|
||
class Intercept( | ||
Generic[T, RT], MixinOperation[Callable[[InvokeEvent[T]], InvokeEvent[RT]]] | ||
): | ||
""" | ||
The `Intercept` allow you to intercept the arguments before invoking target method. | ||
Then, you can handle these arguments in your own functions. | ||
""" | ||
|
||
target_name: Optional[str] | ||
|
||
def __init__( | ||
self, | ||
argument: Callable[[InvokeEvent[T]], InvokeEvent[RT]], | ||
level: int = 1, | ||
target_name: Optional[str] = None, | ||
) -> None: | ||
super().__init__(argument, level) | ||
self.target_name = target_name | ||
|
||
@staticmethod | ||
def configure( | ||
level: int = 1, | ||
target_name: Optional[str] = None, | ||
) -> Callable[[Callable[[InvokeEvent[T]], InvokeEvent[RT]]], "Intercept[T, RT]"]: | ||
return lambda argument: Intercept( | ||
argument=argument, | ||
level=level, | ||
target_name=target_name, | ||
) | ||
|
||
def mixin( | ||
self, | ||
target: Type, | ||
toolchain: ToolChain = ToolChain(), | ||
) -> None: | ||
target_name = ( | ||
self.target_name if self.target_name is not None else self.argument.__name__ | ||
) | ||
native_function = toolchain.tool_getattr( | ||
target, | ||
target_name, | ||
) | ||
|
||
def invoke(*args, **kwargs) -> Any: | ||
return self.argument( | ||
InvokeEvent.from_call(native_function, *args, **kwargs) | ||
).invoke() | ||
|
||
return toolchain.tool_setattr( | ||
target, | ||
target_name, | ||
invoke, | ||
) |
1 change: 1 addition & 0 deletions
1
src/saleyo/mixin/overwrite.py → src/saleyo/operation/overwrite.py
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1 @@ | ||
from saleyo.base.toolchain import InvokeEvent | ||
from saleyo.mixin import Mixin | ||
from saleyo.mixin.intercept import Intercept | ||
|
||
|
||
class Foo: | ||
def demo(self, hello, bad=0): | ||
print("goodbye~") | ||
|
||
|
||
@Mixin(target_class=Foo) | ||
class A: | ||
@Intercept | ||
@staticmethod | ||
def demo(_: InvokeEvent[None]): | ||
return InvokeEvent.from_call(lambda: print("hello world")) | ||
|
||
|
||
Foo().demo(1) | ||
from . import intercept as intercept |
Oops, something went wrong.