From 7557482948543886cd5bebc23d1801bd9836d89c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Wed, 6 Dec 2023 16:19:06 +0100 Subject: [PATCH 1/3] =?UTF-8?q?=E2=9C=85=20Let's=20personalize=20a=20unit?= =?UTF-8?q?=20test=20a=20bit=20;)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/filemanager/test_localstorage.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/filemanager/test_localstorage.py b/tests/filemanager/test_localstorage.py index 97d226628..c6394a092 100644 --- a/tests/filemanager/test_localstorage.py +++ b/tests/filemanager/test_localstorage.py @@ -722,14 +722,14 @@ class LocalStorageTest(unittest.TestCase): "some_file with (parentheses) and ümläuts and digits 123.gco", False, ), - ("pengüino pequeño.stl", "pengüino pequeño.stl", False), + ("there is no b in häußge.gco", "there is no b in häußge.gco", False), ("some file.gco", "some_file.gco", True), ( "some_file with (parentheses) and ümläuts and digits 123.gco", "some_file_with_(parentheses)_and_umlauts_and_digits_123.gco", True, ), - ("pengüino pequeño.stl", "penguino_pequeno.stl", True), + ("there is no b in häußge.gco", "there_is_no_b_in_haussge.gco", True), ) @unpack def test_sanitize_name(self, input, expected, really_universal): From 5229564196a37dd04f238b51578e15fbecf53f98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Wed, 6 Dec 2023 17:36:55 +0100 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=90=9B=20Normalize=20paths=20returned?= =?UTF-8?q?=20by=20split=5Fpath/join=5Fpath?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit See #4719 --- src/octoprint/filemanager/storage.py | 9 +++++---- tests/filemanager/test_localstorage.py | 25 +++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/octoprint/filemanager/storage.py b/src/octoprint/filemanager/storage.py index 0d84c8b4d..b9478c4de 100644 --- a/src/octoprint/filemanager/storage.py +++ b/src/octoprint/filemanager/storage.py @@ -1111,13 +1111,14 @@ class LocalFileStorage(StorageInterface): def split_path(self, path): path = to_unicode(path) split = path.split("/") + if len(split) == 1: return "", split[0] - else: - return self.join_path(*split[:-1]), split[-1] + + return self.path_in_storage(self.join_path(*split[:-1])), split[-1] def join_path(self, *path): - return "/".join(map(to_unicode, path)) + return self.path_in_storage("/".join(map(to_unicode, path))) def sanitize(self, path): """ @@ -1233,7 +1234,7 @@ class LocalFileStorage(StorageInterface): if path.startswith(self.basefolder): path = path[len(self.basefolder) :] path = path.replace(os.path.sep, "/") - if path.startswith("/"): + while path.startswith("/"): path = path[1:] return path diff --git a/tests/filemanager/test_localstorage.py b/tests/filemanager/test_localstorage.py index c6394a092..dd4594f02 100644 --- a/tests/filemanager/test_localstorage.py +++ b/tests/filemanager/test_localstorage.py @@ -714,6 +714,31 @@ class LocalStorageTest(unittest.TestCase): self.assertEqual(0, len(gcode_metadata["links"])) self.assertEqual(1, len(stl_metadata["links"])) + @data( + ("", ("", "")), + ("/", ("", "")), + ("some_file.gco", ("", "some_file.gco")), + ("/some_file.gco", ("", "some_file.gco")), + ("some/folder/and/some file.gco", ("some/folder/and", "some file.gco")), + ("/some/folder/and/some file.gco", ("some/folder/and", "some file.gco")), + ) + @unpack + def test_split_path(self, input, expected): + actual = self.storage.split_path(input) + self.assertEqual(expected, actual) + + @data( + (("", ""), ""), + (("", "some_file.gco"), "some_file.gco"), + (("/", "some_file.gco"), "some_file.gco"), + (("some/folder/and", "some file.gco"), "some/folder/and/some file.gco"), + (("/some/folder/and", "some file.gco"), "some/folder/and/some file.gco"), + ) + @unpack + def test_join_path(self, input, expected): + actual = self.storage.join_path(*input) + self.assertEqual(expected, actual) + @data( ("some_file.gco", "some_file.gco", False), ("some file.gco", "some file.gco", False), From 3e62a2a5b3e75095a2e5c3adc1c2b9cab2d06a3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Wed, 6 Dec 2023 17:38:14 +0100 Subject: [PATCH 3/3] =?UTF-8?q?=F0=9F=90=9B=20Normalize=20paths=20in=20fil?= =?UTF-8?q?e=20manager?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Storage paths should always get their leading / removed. Makes sure to stay consistent in called hooks and triggered events. Closes #4719 --- src/octoprint/filemanager/__init__.py | 271 ++++++++++++++------------ tests/filemanager/test_filemanager.py | 17 +- 2 files changed, 158 insertions(+), 130 deletions(-) diff --git a/src/octoprint/filemanager/__init__.py b/src/octoprint/filemanager/__init__.py index 2fb8f31f4..d6c706d39 100644 --- a/src/octoprint/filemanager/__init__.py +++ b/src/octoprint/filemanager/__init__.py @@ -661,21 +661,21 @@ class FileManager: def list_files( self, - destinations=None, + locations=None, path=None, filter=None, recursive=None, level=0, force_refresh=False, ): - if not destinations: - destinations = list(self._storage_managers.keys()) - if isinstance(destinations, str): - destinations = [destinations] + if not locations: + locations = list(self._storage_managers.keys()) + if isinstance(locations, str): + locations = [locations] result = {} - for dst in destinations: - result[dst] = self._storage_managers[dst].list_files( + for loc in locations: + result[loc] = self._storage_managers[loc].list_files( path=path, filter=filter, recursive=recursive, @@ -686,7 +686,7 @@ class FileManager: def add_file( self, - destination, + location, path, file_object, links=None, @@ -698,10 +698,12 @@ class FileManager: if printer_profile is None: printer_profile = self._printer_profile_manager.get_current_or_default() + path_in_storage = self._storage(location).path_in_storage(path) + for name, hook in self._preprocessor_hooks.items(): try: hook_file_object = hook( - path, + path_in_storage, file_object, links=links, printer_profile=printer_profile, @@ -719,13 +721,12 @@ class FileManager: if hook_file_object is not None: file_object = hook_file_object - queue_entry = self._analysis_queue_entry(destination, path) - + queue_entry = self._analysis_queue_entry(location, path_in_storage) if queue_entry is not None: self._analysis_queue.dequeue(queue_entry) - path_in_storage = self._storage(destination).add_file( - path, + path_in_storage = self._storage(location).add_file( + path_in_storage, file_object, links=links, printer_profile=printer_profile, @@ -734,7 +735,7 @@ class FileManager: ) queue_entry = self._analysis_queue_entry( - destination, + location, path_in_storage, printer_profile=printer_profile, analysis=analysis, @@ -742,11 +743,11 @@ class FileManager: if queue_entry: self._analysis_queue.enqueue(queue_entry, high_priority=True) - _, name = self._storage(destination).split_path(path_in_storage) + _, name = self._storage(location).split_path(path_in_storage) eventManager().fire( Events.FILE_ADDED, { - "storage": destination, + "storage": location, "path": path_in_storage, "name": name, "type": get_file_type(name), @@ -756,16 +757,17 @@ class FileManager: eventManager().fire(Events.UPDATED_FILES, {"type": "printables"}) return path_in_storage - def remove_file(self, destination, path): - queue_entry = self._analysis_queue_entry(destination, path) + def remove_file(self, location, path): + path_in_storage = self._storage(location).path_in_storage(path) + queue_entry = self._analysis_queue_entry(location, path_in_storage) self._analysis_queue.dequeue(queue_entry) - self._storage(destination).remove_file(path) + self._storage(location).remove_file(path_in_storage) - _, name = self._storage(destination).split_path(path) + _, name = self._storage(location).split_path(path_in_storage) eventManager().fire( Events.FILE_REMOVED, { - "storage": destination, + "storage": location, "path": path, "name": name, "type": get_file_type(name), @@ -774,18 +776,18 @@ class FileManager: ) eventManager().fire(Events.UPDATED_FILES, {"type": "printables"}) - def copy_file(self, destination, source, dst): - path_in_storage = self._storage(destination).copy_file(source, dst) - if not self.has_analysis(destination, path_in_storage): - queue_entry = self._analysis_queue_entry(destination, path_in_storage) + def copy_file(self, location, source, destination): + path_in_storage = self._storage(location).copy_file(source, destination) + if not self.has_analysis(location, path_in_storage): + queue_entry = self._analysis_queue_entry(location, path_in_storage) if queue_entry: self._analysis_queue.enqueue(queue_entry) - _, name = self._storage(destination).split_path(path_in_storage) + _, name = self._storage(location).split_path(path_in_storage) eventManager().fire( Events.FILE_ADDED, { - "storage": destination, + "storage": location, "path": path_in_storage, "name": name, "type": get_file_type(name), @@ -794,25 +796,28 @@ class FileManager: ) eventManager().fire(Events.UPDATED_FILES, {"type": "printables"}) - def move_file(self, destination, source, dst): - queue_entry = self._analysis_queue_entry(destination, source) + def move_file(self, location, source, destination): + source_in_storage = self._storage(location).path_in_storage(source) + destination_in_storage = self._storage(location).path_in_storage(destination) + + queue_entry = self._analysis_queue_entry(location, source_in_storage) self._analysis_queue.dequeue(queue_entry) - path = self._storage(destination).move_file(source, dst) - if not self.has_analysis(destination, path): - queue_entry = self._analysis_queue_entry(destination, path) + path = self._storage(location).move_file( + source_in_storage, destination_in_storage + ) + if not self.has_analysis(location, path): + queue_entry = self._analysis_queue_entry(location, path) if queue_entry: self._analysis_queue.enqueue(queue_entry) - source_path_in_storage = self._storage(destination).path_in_storage(source) - _, source_name = self._storage(destination).split_path(source_path_in_storage) - dst_path_in_storage = self._storage(destination).path_in_storage(dst) - _, dst_name = self._storage(destination).split_path(dst_path_in_storage) + _, source_name = self._storage(location).split_path(source_in_storage) + _, destination_name = self._storage(location).split_path(destination_in_storage) eventManager().fire( Events.FILE_REMOVED, { - "storage": destination, - "path": source_path_in_storage, + "storage": location, + "path": source_in_storage, "name": source_name, "type": get_file_type(source_name), "operation": "move", @@ -821,118 +826,127 @@ class FileManager: eventManager().fire( Events.FILE_ADDED, { - "storage": destination, - "path": dst_path_in_storage, - "name": dst_name, - "type": get_file_type(dst_name), + "storage": location, + "path": destination_in_storage, + "name": destination_name, + "type": get_file_type(destination_name), "operation": "move", }, ) eventManager().fire( Events.FILE_MOVED, { - "storage": destination, - "source_path": source_path_in_storage, + "storage": location, + "source_path": source_in_storage, "source_name": source_name, "source_type": get_file_type(source_name), - "destination_path": dst_path_in_storage, - "destination_name": dst_name, - "destination_type": get_file_type(dst_name), + "destination_path": destination_in_storage, + "destination_name": destination_name, + "destination_type": get_file_type(destination_name), }, ) eventManager().fire(Events.UPDATED_FILES, {"type": "printables"}) - def add_folder(self, destination, path, ignore_existing=True, display=None): - path_in_storage = self._storage(destination).add_folder( + def add_folder(self, location, path, ignore_existing=True, display=None): + path_in_storage = self._storage(location).add_folder( path, ignore_existing=ignore_existing, display=display ) - _, name = self._storage(destination).split_path(path_in_storage) + _, name = self._storage(location).split_path(path_in_storage) eventManager().fire( Events.FOLDER_ADDED, - {"storage": destination, "path": path_in_storage, "name": name}, + {"storage": location, "path": path_in_storage, "name": name}, ) eventManager().fire(Events.UPDATED_FILES, {"type": "printables"}) return path_in_storage - def remove_folder(self, destination, path, recursive=True): - self._analysis_queue.dequeue_folder(destination, path) + def remove_folder(self, location, path, recursive=True): + path_in_storage = self._storage(location).path_in_storage(path) + + self._analysis_queue.dequeue_folder(location, path_in_storage) self._analysis_queue.pause() - self._storage(destination).remove_folder(path, recursive=recursive) + self._storage(location).remove_folder(path_in_storage, recursive=recursive) self._analysis_queue.resume() - _, name = self._storage(destination).split_path(path) + _, name = self._storage(location).split_path(path_in_storage) eventManager().fire( - Events.FOLDER_REMOVED, {"storage": destination, "path": path, "name": name} + Events.FOLDER_REMOVED, + {"storage": location, "path": path_in_storage, "name": name}, ) eventManager().fire(Events.UPDATED_FILES, {"type": "printables"}) - def copy_folder(self, destination, source, dst): - path_in_storage = self._storage(destination).copy_folder(source, dst) + def copy_folder(self, location, source, destination): + path_in_storage = self._storage(location).copy_folder(source, destination) self._determine_analysis_backlog( - destination, self._storage(destination), root=path_in_storage + location, self._storage(location), root=path_in_storage ) - _, name = self._storage(destination).split_path(path_in_storage) + _, name = self._storage(location).split_path(path_in_storage) eventManager().fire( Events.FOLDER_ADDED, - {"storage": destination, "path": path_in_storage, "name": name}, + {"storage": location, "path": path_in_storage, "name": name}, ) eventManager().fire(Events.UPDATED_FILES, {"type": "printables"}) - def move_folder(self, destination, source, dst): - self._analysis_queue.dequeue_folder(destination, source) + def move_folder(self, location, source, destination): + source_in_storage = self._storage(location).path_in_storage(source) + destination_in_storage = self._storage(location).path_in_storage(destination) + + self._analysis_queue.dequeue_folder(location, source_in_storage) self._analysis_queue.pause() - dst_path_in_storage = self._storage(destination).move_folder(source, dst) + destination_in_storage = self._storage(location).move_folder( + source_in_storage, destination_in_storage + ) self._determine_analysis_backlog( - destination, self._storage(destination), root=dst_path_in_storage + location, self._storage(location), root=destination_in_storage ) self._analysis_queue.resume() - source_path_in_storage = self._storage(destination).path_in_storage(source) - _, source_name = self._storage(destination).split_path(source_path_in_storage) - _, dst_name = self._storage(destination).split_path(dst_path_in_storage) + _, source_name = self._storage(location).split_path(source_in_storage) + _, destination_name = self._storage(location).split_path(destination_in_storage) eventManager().fire( Events.FOLDER_REMOVED, - {"storage": destination, "path": source_path_in_storage, "name": source_name}, + {"storage": location, "path": source_in_storage, "name": source_name}, ) eventManager().fire( Events.FOLDER_ADDED, - {"storage": destination, "path": dst_path_in_storage, "name": dst_name}, + { + "storage": location, + "path": destination_in_storage, + "name": destination_name, + }, ) eventManager().fire( Events.FOLDER_MOVED, { - "storage": destination, - "source_path": source_path_in_storage, + "storage": location, + "source_path": source_in_storage, "source_name": source_name, - "destination_path": dst_path_in_storage, - "destination_name": dst_name, + "destination_path": destination_in_storage, + "destination_name": destination_name, }, ) eventManager().fire(Events.UPDATED_FILES, {"type": "printables"}) - def has_analysis(self, destination, path): - return self._storage(destination).has_analysis(path) + def has_analysis(self, location, path): + return self._storage(location).has_analysis(path) - def get_metadata(self, destination, path): - return self._storage(destination).get_metadata(path) + def get_metadata(self, location, path): + return self._storage(location).get_metadata(path) - def add_link(self, destination, path, rel, data): - self._storage(destination).add_link(path, rel, data) + def add_link(self, location, path, rel, data): + self._storage(location).add_link(path, rel, data) - def remove_link(self, destination, path, rel, data): - self._storage(destination).remove_link(path, rel, data) + def remove_link(self, location, path, rel, data): + self._storage(location).remove_link(path, rel, data) - def log_print( - self, destination, path, timestamp, print_time, success, printer_profile - ): + def log_print(self, location, path, timestamp, print_time, success, printer_profile): try: if success: - self._storage(destination).add_history( + self._storage(location).add_history( path, { "timestamp": timestamp, @@ -942,7 +956,7 @@ class FileManager: }, ) else: - self._storage(destination).add_history( + self._storage(location).add_history( path, { "timestamp": timestamp, @@ -951,20 +965,20 @@ class FileManager: }, ) eventManager().fire( - Events.METADATA_STATISTICS_UPDATED, {"storage": destination, "path": path} + Events.METADATA_STATISTICS_UPDATED, {"storage": location, "path": path} ) except NoSuchStorage: # if there's no storage configured where to log the print, we'll just not log it pass - def save_recovery_data(self, origin, path, pos): + def save_recovery_data(self, location, path, pos): import time from octoprint.util import atomic_write data = { - "origin": origin, - "path": self.path_in_storage(origin, path), + "origin": location, + "path": self.path_in_storage(location, path), "pos": pos, "date": time.time(), } @@ -1005,79 +1019,78 @@ class FileManager: ) self.delete_recovery_data() - def get_additional_metadata(self, destination, path, key): - self._storage(destination).get_additional_metadata(path, key) + def get_additional_metadata(self, location, path, key): + self._storage(location).get_additional_metadata(path, key) def set_additional_metadata( - self, destination, path, key, data, overwrite=False, merge=False + self, location, path, key, data, overwrite=False, merge=False ): - self._storage(destination).set_additional_metadata( + self._storage(location).set_additional_metadata( path, key, data, overwrite=overwrite, merge=merge ) - def remove_additional_metadata(self, destination, path, key): - self._storage(destination).remove_additional_metadata(path, key) + def remove_additional_metadata(self, location, path, key): + self._storage(location).remove_additional_metadata(path, key) - def path_on_disk(self, destination, path): - return self._storage(destination).path_on_disk(path) + def path_on_disk(self, location, path): + return self._storage(location).path_on_disk(path) - def canonicalize(self, destination, path): - return self._storage(destination).canonicalize(path) + def canonicalize(self, location, path): + return self._storage(location).canonicalize(path) - def sanitize(self, destination, path): - return self._storage(destination).sanitize(path) + def sanitize(self, location, path): + return self._storage(location).sanitize(path) - def sanitize_name(self, destination, name): - return self._storage(destination).sanitize_name(name) + def sanitize_name(self, location, name): + return self._storage(location).sanitize_name(name) - def sanitize_path(self, destination, path): - return self._storage(destination).sanitize_path(path) + def sanitize_path(self, location, path): + return self._storage(location).sanitize_path(path) - def split_path(self, destination, path): - return self._storage(destination).split_path(path) + def split_path(self, location, path): + return self._storage(location).split_path(path) - def join_path(self, destination, *path): - return self._storage(destination).join_path(*path) + def join_path(self, location, *path): + return self._storage(location).join_path(*path) - def path_in_storage(self, destination, path): - return self._storage(destination).path_in_storage(path) + def path_in_storage(self, location, path): + return self._storage(location).path_in_storage(path) - def last_modified(self, destination, path=None, recursive=False): - return self._storage(destination).last_modified(path=path, recursive=recursive) + def last_modified(self, location, path=None, recursive=False): + return self._storage(location).last_modified(path=path, recursive=recursive) - def _storage(self, destination): - if destination not in self._storage_managers: - raise NoSuchStorage(f"No storage configured for destination {destination}") - return self._storage_managers[destination] + def _storage(self, location): + if location not in self._storage_managers: + raise NoSuchStorage(f"No storage configured for destination {location}") + return self._storage_managers[location] - def _add_analysis_result(self, destination, path, result): - if destination not in self._storage_managers: + def _add_analysis_result(self, location, path, result): + if location not in self._storage_managers: return if not result: return - storage_manager = self._storage_managers[destination] + storage_manager = self._storage_managers[location] storage_manager.set_additional_metadata(path, "analysis", result, overwrite=True) def _on_analysis_finished(self, entry, result): self._add_analysis_result(entry.location, entry.path, result) - def _analysis_queue_entry( - self, destination, path, printer_profile=None, analysis=None - ): + def _analysis_queue_entry(self, location, path, printer_profile=None, analysis=None): if printer_profile is None: printer_profile = self._printer_profile_manager.get_current_or_default() - absolute_path = self._storage(destination).path_on_disk(path) - _, file_name = self._storage(destination).split_path(path) + path_in_storage = self._storage(location).path_in_storage(path) + absolute_path = self._storage(location).path_on_disk(path) + _, file_name = self._storage(location).split_path(path) file_type = get_file_type(absolute_path) if file_type: return QueueEntry( file_name, - path, + path_in_storage, file_type[-1], - destination, + location, absolute_path, printer_profile, analysis, diff --git a/tests/filemanager/test_filemanager.py b/tests/filemanager/test_filemanager.py index ca491ff4c..755272239 100644 --- a/tests/filemanager/test_filemanager.py +++ b/tests/filemanager/test_filemanager.py @@ -214,6 +214,7 @@ class FileManagerTest(unittest.TestCase): wrapper = object() self.local_storage.add_file.return_value = ("", "test.gcode") + self.local_storage.path_in_storage.return_value = "test.gcode" self.local_storage.path_on_disk.return_value = "prefix/test.gcode" self.local_storage.split_path.return_value = ("", "test.gcode") @@ -252,6 +253,7 @@ class FileManagerTest(unittest.TestCase): wrapper = object() self.local_storage.add_file.return_value = ("", "test.gcode") + self.local_storage.path_in_storage.return_value = "test.gcode" self.local_storage.path_on_disk.return_value = "prefix/test.gcode" self.local_storage.split_path.return_value = ("", "test.gcode") @@ -276,6 +278,7 @@ class FileManagerTest(unittest.TestCase): ) def test_remove_file(self): + self.local_storage.path_in_storage.return_value = "test.gcode" self.local_storage.path_on_disk.return_value = "prefix/test.gcode" self.local_storage.split_path.return_value = ("", "test.gcode") @@ -354,6 +357,7 @@ class FileManagerTest(unittest.TestCase): ) def test_remove_folder(self): + self.local_storage.path_in_storage.return_value = "test_folder" self.local_storage.split_path.return_value = ("", "test_folder") self.file_manager.remove_folder( @@ -380,6 +384,7 @@ class FileManagerTest(unittest.TestCase): self.fire_event.call_args_list = expected_events def test_remove_folder_nonrecursive(self): + self.local_storage.path_in_storage.return_value = "test_folder" self.local_storage.split_path.return_value = ("", "test_folder") self.file_manager.remove_folder( @@ -571,7 +576,17 @@ class FileManagerTest(unittest.TestCase): ) self.printer_profile_manager.get.return_value = None - # mock get_absolute_path method on local storage + # mock path_in_storage method on local storage + def path_in_storage(path): + if isinstance(path, tuple): + path = "/".join(path) + while path.startswith("/"): + path = path[1:] + return path + + self.local_storage.path_in_storage.side_effect = path_in_storage + + # mock path_on_disk method on local storage def path_on_disk(path): if isinstance(path, tuple): import os