print: refactor file updates and moves, fixes #1303

This commit is contained in:
alfrix 2024-03-17 12:29:12 -03:00
parent ebf7d574b7
commit 865d38217e
2 changed files with 37 additions and 22 deletions

View File

@ -32,6 +32,8 @@ class KlippyFiles:
if params['filename'] not in self.files:
self.files[params['filename']] = {}
self.files[params['filename']][x] = result['result'][x]
if 'path' not in self.files[params['filename']]:
self.files[params['filename']]['path'] = params['filename']
if "thumbnails" in self.files[params['filename']]:
self.files[params['filename']]['thumbnails'].sort(key=lambda y: y['size'], reverse=True)
for thumbnail in self.files[params['filename']]['thumbnails']:
@ -50,6 +52,9 @@ class KlippyFiles:
thumbnail['relative_path']
)
self._screen.process_update("notify_metadata_update", params)
self.run_callbacks(
"modify_file", {'action': "modify_file", 'item': self.files[params['filename']]}
)
def add_file(self, item):
if 'path' not in item:
@ -72,9 +77,11 @@ class KlippyFiles:
logging.info(f"callback not found {callback}")
def process_update(self, data):
if 'item' in data and data['item']['root'] != 'gcodes':
if (
'item' in data and data['item']['root'] != 'gcodes'
or data['action'].endswith("file") and not self.is_gcode(data['item']['path'])
):
return
if data['action'] == "create_file":
self.add_file(data['item'])
elif data['action'] == "delete_file":
@ -82,11 +89,13 @@ class KlippyFiles:
elif data['action'] == "modify_file":
self.request_metadata(data['item']['path'])
elif data['action'] == "move_file":
self.remove_file(data['source_item']['path'])
self.add_file(data['item'])
self.run_callbacks(data['action'], data['item'])
self.files[data['item']['path']] = self.files.pop(data['source_item']['path'])
self.files[data['item']['path']].update(data['item'])
self.run_callbacks(data['action'], data)
return False
@staticmethod
def is_gcode(path):
return os.path.splitext(path)[1] in {'.gcode', '.gco', '.g'}
def file_metadata_exists(self, filename):
return filename in self.files and "slicer" in self.files[filename]
@ -105,8 +114,10 @@ class KlippyFiles:
return filename in self.files and "thumbnails" in self.files[filename]
def request_metadata(self, filename):
if os.path.splitext(filename)[1] in {'.gcode', '.gco', '.g'}:
if self.is_gcode(filename):
self._screen._ws.klippy.get_file_metadata(filename, self._callback)
else:
logging.info("Not a gcode")
def refresh_files(self):
self._screen._ws.klippy.get_file_list(self._callback)
@ -118,6 +129,7 @@ class KlippyFiles:
def get_file_info(self, path):
if path not in self.files:
logging.info(f"Metadata not found {path}")
self.request_metadata(path)
return {}
return self.files[path]

View File

@ -361,40 +361,44 @@ class Panel(ScreenPanel):
logging.info(f"Loaded in {(datetime.now() - start).total_seconds():.3f} seconds")
def delete_from_list(self, path):
logging.info(f"deleting {path}")
for item in self.flowbox.get_children():
if item.get_path() == path:
if item.get_path() in {path, f"gcodes/{path}"}:
logging.info("found removing")
self.flowbox.remove(item)
return True
def add_item_from_callback(self, action, item):
self.delete_from_list(item["path"])
def add_item_from_callback(self, action, data):
item = data['item']
if 'source_item' in data:
self.delete_from_list(data['source_item']['path'])
else:
self.delete_from_list(item['path'])
path = os.path.join("gcodes", item["path"])
if self.cur_directory != os.path.dirname(path):
return
if action == "create_dir":
if action in {"create_dir", "move_dir"}:
item.update({"path": path, "dirname": os.path.split(item["path"])[1]})
else:
item.update({"path": path, "filename": os.path.split(item["path"])[1]})
fbchild = self.create_item(item)
logging.info(item)
if fbchild:
self.flowbox.add(fbchild)
self.flowbox.invalidate_sort()
self.flowbox.show_all()
def _callback(self, action, item):
logging.info(f"{action}: {item}")
def _callback(self, action, data):
logging.info(f"{action}: {data}")
if action in {"create_dir", "create_file"}:
self.add_item_from_callback(action, item)
self.add_item_from_callback(action, data)
elif action == "delete_file":
self.delete_from_list(item["path"])
self.delete_from_list(data['item']["path"])
elif action == "delete_dir":
self.delete_from_list(os.path.join("gcodes", item["path"]))
elif action in {"modify_file", "move_file"}:
if "path" in item and item["path"].startswith("gcodes/"):
item["path"] = item["path"][7:]
self.add_item_from_callback(action, item)
self.delete_from_list(os.path.join("gcodes", data['item']["path"]))
elif action in {"modify_file", "move_file", "move_dir"}:
if "path" in data['item'] and data['item']["path"].startswith("gcodes/"):
data['item']["path"] = data['item']["path"][7:]
self.add_item_from_callback(action, data)
def _refresh_files(self, *args):
logging.info("Refreshing")
@ -464,4 +468,3 @@ class Panel(ScreenPanel):
params
)
self.back()
self._refresh_files()