diff --git a/moonraker/components/file_manager/metadata.py b/moonraker/components/file_manager/metadata.py index 9655c6f..59b4c46 100644 --- a/moonraker/components/file_manager/metadata.py +++ b/moonraker/components/file_manager/metadata.py @@ -223,8 +223,8 @@ class BaseSlicer(metaclass=SlicerType): return slicercls(file_path, size, header + tail, name, ver) return UnknownSlicer(file_path, size, header + tail) - @staticmethod - def identify(data: str) -> Tuple[str, str] | None: + @classmethod + def identify(cls, data: str) -> Tuple[str, str] | None: return None def run_parsers(self) -> Dict[str, Any]: @@ -417,38 +417,46 @@ class UnknownSlicer(BaseSlicer): return regex_find_float(r"M191 S(%F)", self.header_data) class PrusaSlicer(BaseSlicer): - @staticmethod - def identify(data: str) -> Tuple[str, str] | None: - known_aliases = { - 'PrusaSlicer': r"PrusaSlicer\s(.*)\son", - 'SuperSlicer': r"SuperSlicer\s(.*)\son", - 'OrcaSlicer': r"OrcaSlicer\s(.*)\son", - 'MomentSlicer': r"MomentSlicer\s(.*)\son", - 'SliCR-3D': r"SliCR-3D\s(.*)\son", - 'A3dp-Slicer': r"A3dp-Slicer\s(.*)\son", - 'QIDISlicer': r"QIDISlicer\s(.*)\son", - } - excluded_aliases = ("slic3r, slic3r prusa edition") + @classmethod + def identify(cls, data: str) -> Tuple[str, str] | None: # Default base pattern for PrusaSlicer and its forks pattern = r"; generated by (.+?) on \d{4}-\d{2}-\d{2} at \d{2}:\d{2}:\d{2}" match = re.search(pattern, data) if match is None: - # Fall back to search by known alias - for name, expr in known_aliases.items(): - match = re.search(expr, data) - if match: - logging.info( - f"Detected PrusaSlicer fork via fallback: {name}" - ) - return name, match.group(1) + # Fall back to search by known alias (legacy regex search) + known_aliases = [ + "PrusaSlicer", "SuperSlicer", "OrcaSlicer", "MomentSlicer", + "SliCR-3D", "A3dp-Slicer", "QIDISlicer" + ] + pattern = r"({ids})\s(.*)\son".format(ids="|".join(known_aliases)) + match = re.search(pattern, data) + if match: + logging.info( + f"Detected PrusaSlicer fork via fallback: {match[1]}" + ) + return match.groups() # type: ignore return None name, _, version = match.group(1).rpartition(" ") - if name.lower() in excluded_aliases: + if name.lower() in cls.get_exclusions(): # Make sure we don't identify a deriviative implementation, # since the generic regex pattern will return a match. return None return name, version + @classmethod + def regex_id(cls) -> str: + return cls.__name__ + + @classmethod + def get_exclusions(cls) -> set[str]: + exclusions: set[str] = set() + subclasses = list(cls.__subclasses__()) + while subclasses: + sc = subclasses.pop() + subclasses.extend(list(sc.__subclasses__())) + exclusions.add(sc.regex_id().lower()) + return exclusions + @property def config_data(self) -> str: return self.footer_data @@ -614,22 +622,26 @@ class PrusaSlicer(BaseSlicer): return regex_find_int(r"; total filament change = (%D)", self.config_data) class Slic3rPE(PrusaSlicer): - @staticmethod - def identify(data: str) -> Tuple[str, str] | None: + @classmethod + def identify(cls, data: str) -> Tuple[str, str] | None: match = re.search(r"Slic3r\sPrusa\sEdition\s(.*)\son", data) if match: return "Slic3r PE", match.group(1) return None + @classmethod + def regex_id(cls) -> str: + return "Slic3r Prusa Edition" + def parse_filament_total(self) -> Optional[float]: return regex_find_float(r"filament\sused\s=\s(%F)mm", self.footer_data) def parse_thumbnails(self) -> Optional[List[Dict[str, Any]]]: return None -class Slic3r(Slic3rPE): - @staticmethod - def identify(data: str) -> Tuple[str, str] | None: +class Slic3r(PrusaSlicer): + @classmethod + def identify(cls, data: str) -> Tuple[str, str] | None: match = re.search(r"Slic3r\s(\d.*)\son", data) if match: return "Slic3r", match.group(1) @@ -649,9 +661,12 @@ class Slic3r(Slic3rPE): def parse_estimated_time(self) -> Optional[float]: return None + def parse_thumbnails(self) -> Optional[List[Dict[str, Any]]]: + return None + class BambuStudio(PrusaSlicer): - @staticmethod - def identify(data: str) -> Tuple[str, str] | None: + @classmethod + def identify(cls, data: str) -> Tuple[str, str] | None: match = re.search(r"; BambuStudio[^ ]* ([^\n]+)", data) if match is not None: return "BambuStudio", match.group(1) @@ -701,8 +716,8 @@ class BambuStudio(PrusaSlicer): return regex_find_int(r"; total layer number: (%D)", self.config_data) class Cura(BaseSlicer): - @staticmethod - def identify(data: str) -> Tuple[str, str] | None: + @classmethod + def identify(cls, data: str) -> Tuple[str, str] | None: match = re.search(r"Cura_SteamEngine\s(.*)", data) if match: return "Cura", match.group(1) @@ -810,8 +825,8 @@ class Cura(BaseSlicer): return thumbs class Simplify3D(BaseSlicer): - @staticmethod - def identify(data: str) -> Tuple[str, str] | None: + @classmethod + def identify(cls, data: str) -> Tuple[str, str] | None: match = re.search(r"Simplify3D\(R\)\sVersion\s(.*)", data) if match: return "Simplify3D", match.group(1) @@ -920,8 +935,8 @@ class Simplify3D(BaseSlicer): ) class KISSlicer(BaseSlicer): - @staticmethod - def identify(data: str) -> Tuple[str, str] | None: + @classmethod + def identify(cls, data: str) -> Tuple[str, str] | None: match = re.search(r";\sKISSlicer", data) if match: version: str = "?" @@ -971,8 +986,8 @@ class KISSlicer(BaseSlicer): class IdeaMaker(BaseSlicer): - @staticmethod - def identify(data: str) -> Tuple[str, str] | None: + @classmethod + def identify(cls, data: str) -> Tuple[str, str] | None: match = re.search(r"\sideaMaker\s(.*),", data) if match: return "IdeaMaker", match.group(1) @@ -1046,8 +1061,8 @@ class IdeaMaker(BaseSlicer): r";Dimension:(?:\s\d+\.\d+){3}\s(%F)", self.header_data) class IceSL(BaseSlicer): - @staticmethod - def identify(data: str) -> Tuple[str, str] | None: + @classmethod + def identify(cls, data: str) -> Tuple[str, str] | None: match = re.search(r"", data) if match: version = match.group(1) if match.group(1)[0].isdigit() else "-" @@ -1110,8 +1125,8 @@ class IceSL(BaseSlicer): r";\snozzle_diameter_mm_0\s:\s+(%F)", self.header_data) class KiriMoto(BaseSlicer): - @staticmethod - def identify(data: str) -> Tuple[str, str] | None: + @classmethod + def identify(cls, data: str) -> Tuple[str, str] | None: variants: Dict[str, str] = { "Kiri:Moto": r"; Generated by Kiri:Moto (\d.+)", "SimplyPrint": r"; Generated by Kiri:Moto \(SimplyPrint\) (.+)"