Skip to content

Commit

Permalink
add sl1s gcode extension
Browse files Browse the repository at this point in the history
  • Loading branch information
sarkafa committed Oct 23, 2023
1 parent 18a5bff commit 41f7b01
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
1 change: 1 addition & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 10 additions & 8 deletions gcode_metadata/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand All @@ -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

Expand Down Expand Up @@ -250,6 +251,7 @@ def save_cache(self):
<file_name>.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

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand All @@ -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]

Expand Down

0 comments on commit 41f7b01

Please sign in to comment.