From 50bbe4bc4c383c3f5370a90054b5043c02fc77b9 Mon Sep 17 00:00:00 2001 From: H2Sxxa Date: Thu, 21 Mar 2024 08:37:07 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20`Alias`=20Operation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/saleyo/__init__.py | 1 + src/saleyo/operation/__init__.py | 1 + src/saleyo/operation/modify.py | 15 +++++++++++++++ tests/modify_test.py | 7 ++++--- 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/saleyo/__init__.py b/src/saleyo/__init__.py index 9c5affa..8fb6825 100644 --- a/src/saleyo/__init__.py +++ b/src/saleyo/__init__.py @@ -26,6 +26,7 @@ from .operation import Post as Post from .operation import Del as Del from .operation import ReName as ReName +from .operation import Alias as Alias from .base.toolchain import ToolChain as ToolChain from .base.toolchain import Arguments as Arguments from .base.toolchain import InvokeEvent as InvokeEvent diff --git a/src/saleyo/operation/__init__.py b/src/saleyo/operation/__init__.py index d3a8edd..d33475c 100644 --- a/src/saleyo/operation/__init__.py +++ b/src/saleyo/operation/__init__.py @@ -4,5 +4,6 @@ from .intercept import Intercept as Intercept from .modify import ReName as ReName from .modify import Del as Del +from .modify import Alias as Alias from .hook import Pre as Pre from .hook import Post as Post diff --git a/src/saleyo/operation/modify.py b/src/saleyo/operation/modify.py index 88a4bc8..0773d4a 100644 --- a/src/saleyo/operation/modify.py +++ b/src/saleyo/operation/modify.py @@ -29,3 +29,18 @@ class Del(MixinOperation[str]): def mixin(self, target: Type[Any], toolchain: ToolChain = ToolChain()) -> None: return toolchain.tool_delattr(target, self.argument) + + +class Alias(MixinOperation[str]): + """will copy the `argument` to `alias`""" + + alias: str + + def __init__(self, argument: str, alias: str, level=1) -> None: + super().__init__(argument, level) + self.alias = alias + + def mixin(self, target: Type[Any], toolchain: ToolChain = ToolChain()) -> None: + return toolchain.tool_setattr( + target, self.alias, toolchain.tool_getattr(target, self.argument) + ) diff --git a/tests/modify_test.py b/tests/modify_test.py index e3e4315..4de6e2a 100644 --- a/tests/modify_test.py +++ b/tests/modify_test.py @@ -1,5 +1,6 @@ -from saleyo import ReName, GCToolChain +from saleyo import Alias, GCToolChain -ReName("upper", "lower").mixin(str, GCToolChain) +Alias("upper", "do_upper").mixin(str, GCToolChain) -print("Hello Saleyo!".lower()) +print("Hello Saleyo!".do_upper()) +print("Hello Saleyo!".upper())