From 803cc90e3792c2f9a2b23f5efb10fb2aa536ee4b Mon Sep 17 00:00:00 2001 From: "sarka.faloutova" Date: Mon, 23 Oct 2023 15:17:19 +0200 Subject: [PATCH] add sl1s gcode extension --- ChangeLog | 1 + gcode_metadata/metadata.py | 18 ++++++++++-------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/ChangeLog b/ChangeLog index 9f26f9d..e5a8273 100644 --- a/ChangeLog +++ b/ChangeLog @@ -2,6 +2,7 @@ ChangeLog ========= 0.2.0dev * Improve automatic thumbnail and preview selection + * `sl1s` added as sla extension gcode type 0.1.0 * Initial release diff --git a/gcode_metadata/metadata.py b/gcode_metadata/metadata.py index 7ceadbb..ffbdd9c 100644 --- a/gcode_metadata/metadata.py +++ b/gcode_metadata/metadata.py @@ -11,6 +11,7 @@ from logging import getLogger GCODE_EXTENSIONS = (".gcode", ".gc", ".g", ".gco") +SLA_EXTENSIONS = ("sl1", "sl1s") CHARS_TO_REMOVE = ["/", "\\", "\"", "(", ")", "[", "]", "'"] log = getLogger("connect-printer") @@ -62,14 +63,14 @@ def dimension_badness(width, target_width): smaller than the target dimension""" size_difference = width - target_width if size_difference < 0: - return (abs(size_difference) + 2) ** 2 + return (abs(size_difference) + 2)**2 return size_difference def badness(self, target: "ImageInfo", aspect_ratio_weight=1): """Returns a badness score for this image compared to the target""" # This gives a value between 1 and infinity - ar_badness = (max(self.ratio, target.ratio) - / min(self.ratio, target.ratio)) + ar_badness = (max(self.ratio, target.ratio) / + min(self.ratio, target.ratio)) width_badness = self.dimension_badness(self.width, target.width) height_badness = self.dimension_badness(self.height, target.height) @@ -79,8 +80,8 @@ def badness(self, target: "ImageInfo", aspect_ratio_weight=1): # while using a square root on the size badness # As the aspect ratio minimum is 1, let's make the lowest value # 0.01 by subtracting 0.99 - weighted_ar_badness = ar_badness ** aspect_ratio_weight - 0.99 - weighted_size_badness = size_badness ** (1/aspect_ratio_weight) + weighted_ar_badness = ar_badness**aspect_ratio_weight - 0.99 + weighted_size_badness = size_badness**(1 / aspect_ratio_weight) return weighted_ar_badness * weighted_size_badness @@ -250,6 +251,7 @@ def save_cache(self): .cache file. Parse thumbnail from bytes to string format because of JSON serialization requirements""" + def get_cache_data(info): width, height = info.width, info.height @@ -388,6 +390,7 @@ def from_string(self, raw_value): class FDMMetaData(MetaData): """Class for extracting Metadata for FDM gcodes""" + # pylint: disable=too-many-instance-attributes def set_attr(self, name, value): @@ -817,7 +820,7 @@ def get_meta_class(path: str, filename: Optional[str] = None): meta_class: MetaData if fnl.lower().endswith(GCODE_EXTENSIONS): meta_class = FDMMetaData(path) - elif fnl.lower().endswith(".sl1"): + elif fnl.lower().endswith(SLA_EXTENSIONS): meta_class = SLMetaData(path) else: raise UnknownGcodeFileType(path) @@ -841,8 +844,7 @@ def get_closest_image(thumbnails: Dict[str, bytes], return None sorted_thumbnails: List[ImageInfo] = sorted( - valid_thumbnails, key=lambda x: x.badness(target, aspect_ratio_weight) - ) + valid_thumbnails, key=lambda x: x.badness(target, aspect_ratio_weight)) return sorted_thumbnails[0]