From 498bd4d12ad83255f18785483655bdc5b8e70169 Mon Sep 17 00:00:00 2001 From: Sebastian Andersson Date: Mon, 4 Mar 2024 17:54:01 +0100 Subject: [PATCH] Start to support more slicers --- README.md | 6 +- spoolman2superslicer.py => spoolman2slicer.py | 55 ++++++++++++++----- .../ABS+.template | 0 .../default.template | 0 4 files changed, 43 insertions(+), 18 deletions(-) rename spoolman2superslicer.py => spoolman2slicer.py (80%) rename {templates => templates-superslicer}/ABS+.template (100%) rename {templates => templates-superslicer}/default.template (100%) diff --git a/README.md b/README.md index 5fe310e..bb1b0b8 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Spoolman to Superslicer filament transfer +# Spoolman to slicer filament transfer Create [SuperSlicer](https://github.com/supermerill/SuperSlicer) filament configuration from the filaments in [Spoolman](https://github.com/Donkie/Spoolman). ## Prepare for running @@ -11,7 +11,7 @@ pip install -r requirements.txt ``` ## Config file templates -spoolman2superslicer uses [Jinja2](https://palletsprojects.com/p/jinja/) templates for the configuration files +spoolman2slicer uses [Jinja2](https://palletsprojects.com/p/jinja/) templates for the configuration files it creates. They are stored with the filaments' material's name in `templates/`. If the material's template isn't found, `default.template` is used. @@ -19,7 +19,7 @@ it creates. They are stored with the filaments' material's name in ## Run ```sh -./spoolman2superslicer.py -d ~/.config/SuperSlicer/filament/ +./spoolman2slicer.py -d ~/.config/SuperSlicer/filament/ ``` If `-D` is given, all ini files are removed in the directory before diff --git a/spoolman2superslicer.py b/spoolman2slicer.py similarity index 80% rename from spoolman2superslicer.py rename to spoolman2slicer.py index bb24452..b6044fd 100755 --- a/spoolman2superslicer.py +++ b/spoolman2slicer.py @@ -18,16 +18,34 @@ DEFAULT_TEMPLATE = "default.template" -id_to_filename = {} +ORCASLICER = "orcaslicer" +PRUSASLICER = "prusaslicer" +SLICER = "sl1cer" +SUPERSLICER = "superslicer" -loader = FileSystemLoader("templates") -env = Environment(loader=loader) +id_to_filename = {} parser = argparse.ArgumentParser( description="Fetches filaments from Spoolman and creates SuperSlicer filament configs.", ) parser.add_argument("--version", action="version", version="%(prog)s 0.0.1") +parser.add_argument( + "-d", + "--dir", + metavar="DIR", + required=True, + help="the filament config dir", +) + +parser.add_argument( + "-s", + "--slicer", + default=SUPERSLICER, + choices=[ORCASLICER, PRUSASLICER, SLICER, SUPERSLICER], + help="the slicer", +) + parser.add_argument( "-u", "--url", @@ -35,30 +53,37 @@ default="http://mainsailos.local:7912", help="URL for the Spoolman installation", ) -parser.add_argument( - "-d", - "--dir", - metavar="DIR", - required=True, - help="SuperSlicer's filament config dir", -) parser.add_argument( "-U", "--updates", action="store_true", - help="Keep running and update filament configs if they're updated in Spoolman", + help="keep running and update filament configs if they're updated in Spoolman", ) parser.add_argument( "-D", "--delete-all", action="store_true", - help="Delete all filaments before adding existing ones", + help="delete all filament configs before adding existing ones", ) args = parser.parse_args() +loader = FileSystemLoader("templates-" + args.slicer) +env = Environment(loader=loader) + + +def get_config_suffix(): + """Returns the slicer's config file prefix""" + if args.slicer == SUPERSLICER: + return "ini" + elif args.slicer == ORCASLICER: + return "json" + else: + # I don't know... + print("That slicer is not yet supported") + def load_filaments(url: str): """Load filaments json data from Spoolman""" @@ -68,7 +93,7 @@ def load_filaments(url: str): def get_filament_filename(filament): """Returns the filament's config filename""" - return f"{args.dir}/{filament['vendor']['name']}-{filament['name']}.ini" + return f"{args.dir}/{filament['vendor']['name']}-{filament['name']}.{get_config_suffix()}" def delete_filament(filament): @@ -80,9 +105,9 @@ def delete_filament(filament): def delete_all_filaments(): - """Delete all .ini files in the filament dir""" + """Delete all config files in the filament dir""" for filename in os.listdir(args.dir): - if filename.endswith(".ini"): + if filename.endswith("." + get_config_suffix()): filename = args.dir + "/" + filename print(f"Deleting: {filename}") os.remove(filename) diff --git a/templates/ABS+.template b/templates-superslicer/ABS+.template similarity index 100% rename from templates/ABS+.template rename to templates-superslicer/ABS+.template diff --git a/templates/default.template b/templates-superslicer/default.template similarity index 100% rename from templates/default.template rename to templates-superslicer/default.template