Skip to content

Commit

Permalink
🔖 v1.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
H2Sxxa committed Apr 25, 2024
1 parent b7afdd7 commit f5acb7e
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 35 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "saleyo"
version = "1.1.0"
version = "1.1.1"
description = "Saleyo is a lightwight scalable Python AOP framework, easy to use and integrate."
authors = [{ name = "H2Sxxa", email = "[email protected]" }]
dependencies = []
Expand Down
3 changes: 1 addition & 2 deletions src/saleyo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from . import operation as operation
from .mixin import Mixin as Mixin
from .operation import Accessor as Accessor
from .operation import FunctionAccessor as FunctionAccessor
from .operation import Processor as Processor
from .operation import Intercept as Intercept
from .operation import OverWrite as OverWrite
Expand All @@ -35,5 +36,3 @@
from .base.toolchain import CPyToolChain as CPyToolChain
from .base.toolchain import GCToolChain as GCToolChain
from .base.template import MixinOperation as MixinOperation

__version__ = (1, 0, 3)
12 changes: 6 additions & 6 deletions src/saleyo/base/toolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from gc import get_referents as _get_referents
from typing import Any, Callable, Dict, Generic, Optional

from ..base.typing import P, RT, NameSpace
from ..base.typing import P, T, NameSpace


@dataclass
Expand Down Expand Up @@ -101,26 +101,26 @@ def __str__(self) -> str:
return f"Arugument( positional: {self.args}, keyword: {self.kwargs} )"


class InvokeEvent(Generic[P, RT]):
class InvokeEvent(Generic[P, T]):
"""
A `InvokeEvent` includes the target function and the arguments to call this functions.
"""

target: Callable[P, RT]
target: Callable[P, T]
argument: Arguments[P]

def __init__(
self,
target: Callable[P, RT],
target: Callable[P, T],
*args: P.args,
**kwargs: P.kwargs,
) -> None:
super().__init__()
self.target = target
self.argument = Arguments(*args, **kwargs)

def invoke(self, target: Callable[P, RT]) -> RT:
def invoke(self, target: Callable[P, T]) -> T:
return target(*self.argument.args, **self.argument.kwargs)

def invoke_target(self) -> RT:
def invoke_target(self) -> T:
return self.target(*self.argument.args, **self.argument.kwargs)
1 change: 1 addition & 0 deletions src/saleyo/operation/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from .accessor import Accessor as Accessor
from .accessor import FunctionAccessor as FunctionAccessor
from .overwrite import OverWrite as OverWrite
from .processor import Processor as Processor
from .intercept import Intercept as Intercept
Expand Down
39 changes: 18 additions & 21 deletions src/saleyo/operation/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@

class Accessor(Generic[T], MixinOperation[str]):
"""
The Generic `T` is the type of target varible, if you want to visit a private function, try to use `FunctionAccessor`.
The Generic `T` is the type of target varible, if you want to visit a private function, try to use the subclass `FunctionAccessor`.
Notice: The value only available after invoking the `mixin` method.
If the `private` is `True`, will add target class name (like `_Foo`) to the prefix to argument, if the target is complex, you can set `private` to `False` and provide the true name by yourself.
Also a variable named `argument` will add to target classes when `private` is `True`.
If you use `@Mixin` and have more than one target classes, the `value` will always be the varible of latest target.
"""

Expand All @@ -29,11 +31,12 @@ def mixin(self, target: M, toolchain: ToolChain = ToolChain()) -> None:
target,
f"_{target.__name__}{self.argument}" if self._private else self.argument,
)
return toolchain.tool_setattr(
target,
self.argument,
self._inner,
)
if self._private:
toolchain.tool_setattr(
target,
self.argument,
self._inner,
)

@property
def value(self) -> T:
Expand All @@ -56,34 +59,28 @@ def __str__(self) -> str:
return f"Accessor {{ value: {self._inner} ({id(self._inner)}) }}"


class FunctionAccessor(Generic[P, T], MixinOperation[str]):
class FunctionAccessor(Generic[P, T], Accessor[Callable[P, T]]):
"""
`FunctionAccessor` can be call directly.
It's recommend to provide the Generic `P` and `T`, it can be useful in `__call__`.
If the `private` is `True`, will add target class name (like `_Foo`) to the prefix to argument, if the target is complex, you can set `private` to `False` and provide the true name by yourself.
If you just call in operation functions, you can just use a variable with `Callable[P, T]` type.
Also a variable named `argument` will add to target classes when `private` is `True`.s
If you just call in operation functions, you can just use a simple variable with `Callable[P, T]` type.
If you use `@Mixin` and have more than one target classes, the `value` will always be the varible of latest target.
```python
something: FunctionAccessor[[str], None] = FunctionAccessor("something")
```
"""

_inner: Optional[Callable[P, T]] = None
_private: bool

def __init__(self, argument: str, level=1, private=True) -> None:
super().__init__(argument, level)
self._private = private

def mixin(self, target: M, toolchain: ToolChain = ToolChain()) -> None:
self._inner = toolchain.tool_getattr(
target,
f"_{target.__name__}{self.argument}" if self._private else self.argument,
)

def __call__(self, *args: P.args, **kwargs: P.kwargs) -> T:
"""
Call the `argument` function in target, don't use until The `mixin` method call.
"""
assert self._inner
return self._inner(*args, **kwargs)
7 changes: 4 additions & 3 deletions src/saleyo/operation/ancestor.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from typing import Any, Type
from saleyo.base.template import MixinOperation
from saleyo.base.toolchain import ToolChain
from saleyo.base.typing import M

from ..base.template import MixinOperation
from ..base.toolchain import ToolChain
from ..base.typing import M


class Ancestor(MixinOperation[Type[Any]]):
Expand Down
4 changes: 2 additions & 2 deletions src/saleyo/operation/intercept.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from typing import Any, Callable, Generic, Optional, ParamSpec

from ..base.typing import M
from ..base.toolchain import InvokeEvent, ToolChain
from ..base.template import MixinOperation

from typing import Any, Callable, Generic, Optional, ParamSpec

_PA = ParamSpec("_PA")
_PB = ParamSpec("_PB")
_A = InvokeEvent[_PA, Any]
Expand Down

0 comments on commit f5acb7e

Please sign in to comment.