diff --git a/README.md b/README.md index e0d1aa5..f7505e2 100644 --- a/README.md +++ b/README.md @@ -31,9 +31,10 @@ pip install lib-not-dr ```python import subprocess -from lib_not_dr.nuitka import CompilerHelper +from pathlib import Path +from lib_not_dr.nuitka.compile import CompilerHelper -compiler = CompilerHelper("main.py") +compiler = CompilerHelper(src_file = Path("main.py")) print(compiler) subprocess.run(compiler.gen_subprocess_cmd()) @@ -45,9 +46,10 @@ subprocess.run(compiler.gen_subprocess_cmd()) ```python import sys import subprocess -from lib_not_dr.nuitka import CompilerHelper +from pathlib import Path +from lib_not_dr.nuitka.compile import CompilerHelper -compiler = CompilerHelper("main.py", run_after_build=True) +compiler = CompilerHelper(src_file = Path("main.py"), run_after_build=True) print(compiler) diff --git a/lib_not_dr/command/descriptor.py b/lib_not_dr/command/descriptor.py index 17c8cc6..5a479b7 100644 --- a/lib_not_dr/command/descriptor.py +++ b/lib_not_dr/command/descriptor.py @@ -1,7 +1,13 @@ class CallBackDescriptor: - def __set_name__(self, owner, name): + def __init__(self, name): self.callback_name = name def __set__(self, instance, value): - if + assert getattr(instance, self.callback_name) is None, f"Attribute '{self.callback_name}' has been set." + instance.__dict__[self.callback_name] = value + def __get__(self, instance, owner): + if instance is None: + return self + else: + return instance.__dict__[self.callback_name] diff --git a/lib_not_dr/command/nodes.py b/lib_not_dr/command/nodes.py index 943769a..0ed3a06 100644 --- a/lib_not_dr/command/nodes.py +++ b/lib_not_dr/command/nodes.py @@ -6,6 +6,9 @@ import re from typing import Callable, List, Optional, Union + +from .descriptor import CallBackDescriptor + try: from typing import Self except ImportError: @@ -26,6 +29,15 @@ EMPTY_WORDS = re.compile(r"\s", re.I) def check_once(cls, name) -> None: + """ + Check whether the attribute has been set. + if so, it will raise exception `AttributeError`. + 检查属性是否已经被设置。 + 如果已经被设置,将会抛出 `AttributeError` 异常。 + :param cls: class object + :param name: attribute name + :return: None + """ if hasattr(cls, name): if getattr(cls, name) is not None: raise AttributeError(f"Attribute '{name}' has been set.") @@ -65,6 +77,10 @@ class Flag: class Literal: + _tip = CallBackDescriptor("_tip") + _func = CallBackDescriptor("_func") + _err_callback = CallBackDescriptor("_err_callback") + def __init__(self, name: str): self.name: str = name self.sub: List[Self] = []