From 46161ba82ecf19b90c54bba1e61426e5a4b30a14 Mon Sep 17 00:00:00 2001 From: Leni Aniva Date: Thu, 4 Jul 2024 00:24:14 -0700 Subject: [PATCH] fix: Decorated target not directly callable --- nhf/build.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/nhf/build.py b/nhf/build.py index 5ac9bd2..f11b9c8 100644 --- a/nhf/build.py +++ b/nhf/build.py @@ -6,8 +6,9 @@ output objects with the `@target` decorator """ from pathlib import Path from typing import Union -import cadquery as Cq +from functools import wraps from colorama import Fore, Style +import cadquery as Cq class Target: @@ -16,6 +17,8 @@ class Target: name: str): self._method = method self.name = name + def __str__(self): + return f"" def __call__(self, obj, *args, **kwargs): return self._method(obj, *args, **kwargs) @@ -27,17 +30,21 @@ class Target: def g(): for name in dir(subject): method = getattr(subject, name) - if isinstance(method, Target): - yield name, method - return {name: method for name, method in g()} + if hasattr(method, 'target'): + yield method.target + return {method.name: method for method in g()} -def target(name, **kwargs): +def target(name, **deco_kwargs): """ Decorator for annotating a build output """ def f(method): - return Target(method, name=name, **kwargs) + @wraps(method) + def wrapper(self, *args, **kwargs): + return method(self, *args, **kwargs) + wrapper.target = Target(method, name, **deco_kwargs) + return wrapper return f def _to_shape(x: Union[Cq.Workplane, Cq.Shape, Cq.Compound, Cq.Assembly]) -> Cq.Shape: @@ -74,8 +81,7 @@ class Model: model = f(self) if verbose >= 1: - print(f"{Fore.BLUE}Building{Style.RESET_ALL} {output_file}", end="") + print(f"{Fore.BLUE}Building{Style.RESET_ALL} {output_file}") _to_shape(model).exportStl(str(output_file)) if verbose >= 1: - print("\r", end="") print(f"{Fore.GREEN}Built{Style.RESET_ALL} {output_file}")