Skip to content

Commit eb3f6de

Browse files
committed
Fix: update file loading behavior to create groups for multiple objects when loading from directory
Fix #163
1 parent b3d5000 commit eb3f6de

File tree

4 files changed

+43
-15
lines changed

4 files changed

+43
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ See DataLab [roadmap page](https://datalab-platform.com/en/contributing/roadmap.
1818
* Fixed [Issue #160](https://github.com/DataLab-Platform/DataLab/issues/160) - When selecting multiple images and clearing ROI in ROI editor, only the first image is affected
1919
* Fixed [Issue #161](https://github.com/DataLab-Platform/DataLab/issues/161) - Refresh image items only if necessary (when editing ROI, pasting/deleting metadata)
2020
* Fixed [Issue #162](https://github.com/DataLab-Platform/DataLab/issues/162) - View in a new window: when displaying multiple images, the item list panel should be visible
21+
* Fixed [Issue #163](https://github.com/DataLab-Platform/DataLab/issues/163) - Open from directory: expected one group per folder when loading multiple files
2122

2223
## DataLab Version 0.19.0 ##
2324

cdl/core/gui/panel/base.py

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import abc
1212
import dataclasses
1313
import glob
14+
import os
1415
import os.path as osp
1516
import re
1617
import warnings
@@ -853,19 +854,24 @@ def set_current_object_title(self, title: str) -> None:
853854
obj.title = title
854855
self.objview.update_item(obj.uuid)
855856

856-
def __load_from_file(self, filename: str) -> list[SignalObj] | list[ImageObj]:
857+
def __load_from_file(
858+
self, filename: str, create_group: bool = True
859+
) -> list[SignalObj] | list[ImageObj]:
857860
"""Open objects from file (signal/image), add them to DataLab and return them.
858861
859862
Args:
860863
filename: file name
864+
create_group: if True, create a new group if more than one object is loaded.
865+
Defaults to True. If False, all objects are added to the current group.
861866
862867
Returns:
863868
New object or list of new objects
864869
"""
865870
worker = CallbackWorker(lambda worker: self.IO_REGISTRY.read(filename, worker))
866871
objs = qt_long_callback(self, _("Adding objects to workspace"), worker, True)
867872
group_id = None
868-
if len(objs) > 1:
873+
if len(objs) > 1 and create_group:
874+
# Create a new group if more than one object is loaded
869875
group_id = self.add_group(osp.basename(filename)).uuid
870876
for obj in objs:
871877
obj.metadata["source"] = filename
@@ -901,23 +907,38 @@ def load_from_directory(self, directory: str | None = None) -> list[TypeObj]:
901907
directory = getexistingdirectory(self, _("Open"), basedir)
902908
if not directory:
903909
return []
904-
# Get all files in the directory:
905-
relfnames = sorted(
906-
osp.relpath(path, start=directory)
907-
for path in glob.glob(osp.join(directory, "**", "*.*"), recursive=True)
908-
)
909-
# When Python 3.9 will be dropped, we can use (support for `root_dir` is added):
910-
# relfnames = sorted(glob.glob("**/*.*", root_dir=directory, recursive=True))
911-
filenames = [osp.join(directory, fname) for fname in relfnames]
912-
return self.load_from_files(filenames, ignore_unknown=True)
910+
objs = []
911+
# Iterate over all subfolders in the directory:
912+
for path in glob.glob(osp.join(directory, "**"), recursive=True):
913+
if osp.isdir(path) and len(os.listdir(path)) > 0:
914+
path = osp.normpath(path)
915+
self.add_group(osp.basename(path), select=True)
916+
fnames = [
917+
osp.join(path, fname)
918+
for fname in os.listdir(path)
919+
if osp.isfile(osp.join(path, fname))
920+
]
921+
new_objs = self.load_from_files(
922+
fnames, create_group=False, ignore_unknown=True
923+
)
924+
objs += new_objs
925+
if len(new_objs) == 0:
926+
self.remove_object(force=True) # Remove empty group
927+
return objs
913928

914929
def load_from_files(
915-
self, filenames: list[str] | None = None, ignore_unknown: bool = False
930+
self,
931+
filenames: list[str] | None = None,
932+
create_group: bool = False,
933+
ignore_unknown: bool = False,
916934
) -> list[TypeObj]:
917935
"""Open objects from file (signals/images), add them to DataLab and return them.
918936
919937
Args:
920938
filenames: File names
939+
create_group: if True, create a new group if more than one object is loaded
940+
for a single file. Defaults to False: all objects are added to the current
941+
group.
921942
ignore_unknown: if True, ignore unknown file types (default: False)
922943
923944
Returns:
@@ -935,7 +956,7 @@ def load_from_files(
935956
with qt_try_loadsave_file(self.parent(), filename, "load"):
936957
Conf.main.base_dir.set(filename)
937958
try:
938-
objs += self.__load_from_file(filename)
959+
objs += self.__load_from_file(filename, create_group=create_group)
939960
except NotImplementedError as exc:
940961
if ignore_unknown:
941962
# Ignore unknown file types

doc/locale/fr/LC_MESSAGES/api/core.gui/panel.po

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ msgid ""
88
msgstr ""
99
"Project-Id-Version: DataLab \n"
1010
"Report-Msgid-Bugs-To: \n"
11-
"POT-Creation-Date: 2025-04-07 19:11+0200\n"
11+
"POT-Creation-Date: 2025-04-08 10:13+0200\n"
1212
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
1313
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
1414
"Language: fr\n"
@@ -343,6 +343,9 @@ msgstr ""
343343
msgid "File names"
344344
msgstr ""
345345

346+
msgid "if True, create a new group if more than one object is loaded for a single file. Defaults to False: all objects are added to the current group."
347+
msgstr ""
348+
346349
msgid "if True, ignore unknown file types (default: False)"
347350
msgstr ""
348351

doc/locale/fr/LC_MESSAGES/contributing/changelog.po

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ msgid ""
77
msgstr ""
88
"Project-Id-Version: DataLab \n"
99
"Report-Msgid-Bugs-To: \n"
10-
"POT-Creation-Date: 2025-04-08 09:02+0200\n"
10+
"POT-Creation-Date: 2025-04-08 10:13+0200\n"
1111
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
1212
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
1313
"Language: fr\n"
@@ -66,6 +66,9 @@ msgstr "Correction de l'[Issue #161](https://github.com/DataLab-Platform/DataLab
6666
msgid "Fixed [Issue #162](https://github.com/DataLab-Platform/DataLab/issues/162) - View in a new window: when displaying multiple images, the item list panel should be visible"
6767
msgstr "Correction de l'[Issue #162](https://github.com/DataLab-Platform/DataLab/issues/162) - Afficher dans une nouvelle fenêtre : lors de l'affichage de plusieurs images, le panneau de liste d'items doit être visible"
6868

69+
msgid "Fixed [Issue #163](https://github.com/DataLab-Platform/DataLab/issues/163) - Open from directory: expected one group per folder when loading multiple files"
70+
msgstr "Ceci corrige l'[Issue #163](https://github.com/DataLab-Platform/DataLab/issues/163) - Ouvrir depuis le répertoire : un groupe par dossier attendu lors du chargement de plusieurs fichiers"
71+
6972
msgid "DataLab Version 0.19.0"
7073
msgstr "DataLab Version 0.19.0"
7174

0 commit comments

Comments
 (0)