Skip to content

Commit

Permalink
change: Don't update filament file without change
Browse files Browse the repository at this point in the history
  • Loading branch information
bofh69 committed Mar 15, 2024
1 parent 4a15a0d commit 8a48957
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 10 deletions.
31 changes: 26 additions & 5 deletions spoolman2slicer.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
templates = Environment(loader=loader)

filament_id_to_filename = {}
filament_id_to_content = {}

filename_usage = {}

Expand Down Expand Up @@ -123,7 +124,7 @@ def get_filament_filename(filament):
return args.dir + "/" + template.render(filament)


def delete_filament(filament, is_update = False):
def delete_filament(filament, is_update=False):
"""Delete the filament's file if no longer in use"""
filename = filament_id_to_filename[filament["id"]]

Expand All @@ -133,7 +134,12 @@ def delete_filament(filament, is_update = False):
if filename_usage[filename] > 0:
return

if not is_update:
new_filename = None
if is_update:
add_sm2s_to_filament(filament)
new_filename = get_filament_filename(filament)

if filename != new_filename:
print(f"Deleting: {filename}")
os.remove(filename)

Expand All @@ -157,7 +163,11 @@ def write_filament(filament):
else:
filename_usage[filename] = 1

filament_id_to_filename[filament["id"]] = filename
filament_id = filament["id"]

old_filename = filament_id_to_filename.get(filament_id)

filament_id_to_filename[filament_id] = filename

if "material" in filament:
template_name = f"{filament['material']}.template"
Expand All @@ -173,15 +183,25 @@ def write_filament(filament):
if args.verbose:
print("Using the default template")

print(f"Writing to: {filename}")

if args.verbose:
print(f"Rendering for filename: {filename}")
print("Fields for the template:")
print(filament)

filament_text = template.render(filament)
old_filament_text = filament_id_to_content.get(filament_id)

if old_filament_text == filament_text and old_filename == filename:
if args.verbose:
print("Same content, file not updated")
return

print(f"Writing to: {filename}")

with open(filename, "w", encoding="utf-8") as cfg_file:
print(filament_text, file=cfg_file)
filament_id_to_content[filament_id] = filament_text

if args.verbose:
print()

Expand All @@ -200,6 +220,7 @@ def handle_filament_update(filament):
delete_filament(filament, is_update=True)
write_filament(filament)


def handle_spool_update_msg(msg):
"""Handles spool update msgs received via WS"""

Expand Down
2 changes: 1 addition & 1 deletion templates-superslicer/ABS+.template
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# generated by {{sm2s.name}} {{sm2s.version}} on {{sm2s.now}}
# generated by {{sm2s.name}} {{sm2s.version}}
bed_temperature = {{settings_bed_temp|int}}
bridge_fan_speed = -1
bridge_internal_fan_speed = -1
Expand Down
2 changes: 1 addition & 1 deletion templates-superslicer/ASA.template
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# generated by {{sm2s.name}} {{sm2s.version}} on {{sm2s.now}}
# generated by {{sm2s.name}} {{sm2s.version}}
bed_temperature = {{settings_bed_temp|int}}
bridge_fan_speed = -1
bridge_internal_fan_speed = -1
Expand Down
2 changes: 1 addition & 1 deletion templates-superslicer/PLA.template
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# generated by {{sm2s.name}} {{sm2s.version}} on {{sm2s.now}}
# generated by {{sm2s.name}} {{sm2s.version}}
bed_temperature = {{settings_bed_temp|int}}
bridge_fan_speed = 100
bridge_internal_fan_speed = -1
Expand Down
2 changes: 1 addition & 1 deletion templates-superslicer/TPU.template
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# generated by {{sm2s.name}} {{sm2s.version}} on {{sm2s.now}}
# generated by {{sm2s.name}} {{sm2s.version}}
bed_temperature = {{settings_bed_temp|int}}
bridge_fan_speed = -1
bridge_internal_fan_speed = -1
Expand Down
2 changes: 1 addition & 1 deletion templates-superslicer/default.template
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# generated by {{sm2s.name}} {{sm2s.version}} on {{sm2s.now}}
# generated by {{sm2s.name}} {{sm2s.version}}
bed_temperature = {{settings_bed_temp|int}}
bridge_fan_speed = 100
bridge_internal_fan_speed = -1
Expand Down

0 comments on commit 8a48957

Please sign in to comment.