From 763888e83aeeae9ad79ed5dfc9d8a8b00fd04df7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Sat, 24 Feb 2018 22:30:12 +0100 Subject: [PATCH] Allow slicers to manage their profiles themselves --- src/octoprint/plugin/types.py | 41 +++++++++++++++++++++++++++++++ src/octoprint/slicing/__init__.py | 20 ++++----------- 2 files changed, 46 insertions(+), 15 deletions(-) diff --git a/src/octoprint/plugin/types.py b/src/octoprint/plugin/types.py index b39dcac91..9067ee596 100644 --- a/src/octoprint/plugin/types.py +++ b/src/octoprint/plugin/types.py @@ -1861,6 +1861,47 @@ class SlicerPlugin(OctoPrintPlugin): destination_extensions=["gco", "gcode", "g"] ) + def get_slicer_profiles(self, profile_path): + """ + Fetch all :class:`~octoprint.slicing.SlicingProfile`s stored for this slicer. + + For compatibility reasons with existing slicing plugins this method defaults to returning profiles parsed from + .profile files in the plugin's ``profile_path``, utilizing the :func:`SlicingPlugin.get_slicer_profile` method + of the plugin implementation. + + Arguments: + profile_path (str): The base folder where OctoPrint stores this slicer plugin's profiles + """ + + try: + from os import scandir + except ImportError: + from scandir import scandir + + import octoprint.util + + profiles = dict() + for entry in scandir(profile_path): + if not entry.name.endswith(".profile") or octoprint.util.is_hidden_path(entry.name): + # we are only interested in profiles and no hidden files + continue + + profile_name = entry.name[:-len(".profile")] + profiles[profile_name] = self.get_slicer_profile(entry.path) + return profiles + + # noinspection PyMethodMayBeStatic + def get_slicer_profiles_lastmodified(self, profile_path): + import os + try: + from os import scandir + except ImportError: + from scandir import scandir + + lms = [os.stat(profile_path).st_mtime] + lms += [os.stat(entry.path).st_mtime for entry in scandir(profile_path) if entry.name.endswith(".profile")] + return max(lms) + # noinspection PyMethodMayBeStatic def get_slicer_default_profile(self): """ diff --git a/src/octoprint/slicing/__init__.py b/src/octoprint/slicing/__init__.py index 9174cc52f..f14ecc199 100644 --- a/src/octoprint/slicing/__init__.py +++ b/src/octoprint/slicing/__init__.py @@ -377,9 +377,9 @@ class SlicingManager(object): If it's a :class:`dict`, a new :class:`SlicingProfile` instance will be created with the supplied meta data and the profile data as the :attr:`~SlicingProfile.data` attribute. - + .. note:: - + If the profile is the first profile to be saved for the slicer, it will automatically be marked as default. Arguments: @@ -432,7 +432,7 @@ class SlicingManager(object): profile=name) event = octoprint.events.Events.SLICING_PROFILE_MODIFIED if is_overwrite else octoprint.events.Events.SLICING_PROFILE_ADDED octoprint.events.eventManager().fire(event, payload) - + if first_profile: # enforce the first profile we add for this slicer is set as default self.set_default_profile(slicer, name) @@ -557,16 +557,8 @@ class SlicingManager(object): if require_configured and not slicer in self.configured_slicers: raise SlicerNotConfigured(slicer) - profiles = dict() slicer_profile_path = self.get_slicer_profile_path(slicer) - for entry in scandir(slicer_profile_path): - if not entry.name.endswith(".profile") or octoprint.util.is_hidden_path(entry.name): - # we are only interested in profiles and no hidden files - continue - - profile_name = entry.name[:-len(".profile")] - profiles[profile_name] = self._load_profile_from_path(slicer, entry.path, require_configured=require_configured) - return profiles + return self.get_slicer(slicer).get_slicer_profiles(slicer_profile_path) def profiles_last_modified(self, slicer): """ @@ -583,9 +575,7 @@ class SlicingManager(object): raise UnknownSlicer(slicer) slicer_profile_path = self.get_slicer_profile_path(slicer) - lms = [os.stat(slicer_profile_path).st_mtime] - lms += [os.stat(entry.path).st_mtime for entry in scandir(slicer_profile_path) if entry.name.endswith(".profile")] - return max(lms) + return self.get_slicer(slicer).get_slicer_profiles_lastmodified(slicer_profile_path) def get_slicer_profile_path(self, slicer): """