feat: Name in target

This commit is contained in:
Leni Aniva 2024-07-04 10:02:58 -07:00
parent d69cf014a1
commit 89c6a39c2f
Signed by: aniva
GPG Key ID: 4D9B1C8D10EA4C50
2 changed files with 27 additions and 11 deletions

View File

@ -42,14 +42,20 @@ class Target:
self.kind = kind self.kind = kind
self.kwargs = kwargs self.kwargs = kwargs
def __str__(self): def __str__(self):
return f"<target {self.name}>" return f"<target {self.name}.{self.kind.ext} {self._method}>"
def __call__(self, obj, *args, **kwargs): def __call__(self, obj, *args, **kwargs):
""" """
Raw call function which passes arguments directly to `_method` Raw call function which passes arguments directly to `_method`
""" """
return self._method(obj, *args, **kwargs) return self._method(obj, *args, **kwargs)
def file_name(self, file_name):
return f"{file_name}.{self.kind.ext}" @property
def file_name(self):
"""
Output file name
"""
return f"{self.name}.{self.kind.ext}"
def write_to(self, obj, path: str): def write_to(self, obj, path: str):
x = self._method(obj) x = self._method(obj)
if self.kind == TargetKind.STL: if self.kind == TargetKind.STL:
@ -98,6 +104,8 @@ class Model:
""" """
Base class for a parametric assembly Base class for a parametric assembly
""" """
def __init__(self, name: str):
self.name = name
@property @property
def target_names(self) -> list[str]: def target_names(self) -> list[str]:
@ -107,19 +115,22 @@ class Model:
return list(Target.methods(self).keys()) return list(Target.methods(self).keys())
def check_all(self) -> int: def check_all(self) -> int:
"""
Builds all targets but do not output them
"""
total = 0 total = 0
for k, f in Target.methods(self).items(): for t in Target.methods(self).values():
f(self) t(self)
total += 1 total += 1
return total return total
def build_all(self, output_dir: Union[Path, str] = "build", verbose=1): def build_all(self, output_dir: Union[Path, str] = "build", verbose=1):
""" """
Build all targets in this model Build all targets in this model and write the results to file
""" """
output_dir = Path(output_dir) output_dir = Path(output_dir)
for k, target in Target.methods(self).items(): for t in Target.methods(self).values():
output_file = output_dir / target.file_name(k) output_file = output_dir / self.name / t.file_name
if output_file.is_file(): if output_file.is_file():
if verbose >= 1: if verbose >= 1:
print(f"{Fore.GREEN}Skipping{Style.RESET_ALL} {output_file}") print(f"{Fore.GREEN}Skipping{Style.RESET_ALL} {output_file}")
@ -128,6 +139,10 @@ class Model:
if verbose >= 1: if verbose >= 1:
print(f"{Fore.BLUE}Building{Style.RESET_ALL} {output_file}") print(f"{Fore.BLUE}Building{Style.RESET_ALL} {output_file}")
target.write_to(self, str(output_file))
if verbose >= 1: try:
print(f"{Fore.GREEN}Built{Style.RESET_ALL} {output_file}") t.write_to(self, str(output_file))
if verbose >= 1:
print(f"{Fore.GREEN}Built{Style.RESET_ALL} {output_file}")
except Exception as e:
print(f"{Fore.RED}Failed to build{Style.RESET_ALL} {output_file}: {e}")

View File

@ -109,6 +109,7 @@ class Parameters(Model):
)) ))
def __post_init__(self): def __post_init__(self):
super().__init__(name="houjuu-nue")
assert self.wing_root_radius > self.hs_hirth_joint.radius,\ assert self.wing_root_radius > self.hs_hirth_joint.radius,\
"Wing root must be large enough to accomodate joint" "Wing root must be large enough to accomodate joint"