71 lines
2.6 KiB
Python
71 lines
2.6 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import TYPE_CHECKING, Callable, List, Optional
|
|
|
|
if TYPE_CHECKING:
|
|
from octoprint_bambu_printer.printer.file_system.remote_sd_card_file_list import (
|
|
RemoteSDCardFileList,
|
|
)
|
|
|
|
from dataclasses import dataclass, field
|
|
from pathlib import Path
|
|
from octoprint_bambu_printer.printer.file_system.file_info import FileInfo
|
|
|
|
|
|
class CachedFileView:
|
|
def __init__(
|
|
self, file_system, on_update: Optional[Callable] = None, base_path: str = ""
|
|
):
|
|
self._filters = []
|
|
self._file_system = file_system
|
|
self._base_path = base_path
|
|
self._update_complete_callback = on_update
|
|
self._file_info_cache = []
|
|
|
|
def with_filter(self, path: str, extension: str):
|
|
self._filters.append({"path": path, "extension": extension})
|
|
return self
|
|
|
|
def update(self) -> None:
|
|
try:
|
|
file_info_list = self.list_all_views()
|
|
self._file_info_cache = file_info_list
|
|
|
|
# Rufe Callback auf, wenn vorhanden
|
|
if self._update_complete_callback is not None:
|
|
self._update_complete_callback()
|
|
except Exception as e:
|
|
import logging
|
|
logging.getLogger("octoprint.plugins.bambu_printer.BambuPrinter").error(
|
|
f"Error updating file list: {e}", exc_info=True
|
|
)
|
|
|
|
def list_all_views(self) -> List[FileInfo]:
|
|
# Verwende die Mock-Implementation von get_file_list statt FTPS
|
|
try:
|
|
return self._file_system.get_file_list(self._base_path)
|
|
except Exception as e:
|
|
import logging
|
|
logging.getLogger("octoprint.plugins.bambu_printer.BambuPrinter").error(
|
|
f"Error listing files: {e}", exc_info=True
|
|
)
|
|
return []
|
|
|
|
def get_all_cached_info(self) -> List[FileInfo]:
|
|
return self._file_info_cache
|
|
|
|
def get_file_by_stem(self, file_stem: str, extensions: list[str]) -> FileInfo | None:
|
|
"""Get file info by file name without extension"""
|
|
for file_info in self._file_info_cache:
|
|
for extension in extensions:
|
|
if file_info.file_name.lower().startswith(f"{file_stem.lower()}{extension}"):
|
|
return file_info
|
|
|
|
return None
|
|
|
|
def get_file_data(self, file_path: str) -> FileInfo | None:
|
|
for file_info in self._file_info_cache:
|
|
if file_info.path.lower() == file_path.lower() or file_info.file_name.lower() == file_path.lower():
|
|
return file_info
|
|
return None
|