Skip to content

Commit 4af378f

Browse files
committed
Fix automatic macro naming generating duplicates after workspace reload
1 parent d4e2b37 commit 4af378f

File tree

5 files changed

+60
-7
lines changed

5 files changed

+60
-7
lines changed

datalab/gui/macroeditor.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,20 @@ def get_untitled_title() -> str:
186186
"""
187187
global UNTITLED_NB # pylint: disable=global-statement
188188
UNTITLED_NB += 1
189-
untitled = _("Untitled")
190-
return f"{untitled} {UNTITLED_NB:02d}"
189+
return f"macro_{UNTITLED_NB:02d}"
190+
191+
@staticmethod
192+
def set_untitled_number(number: int) -> None:
193+
"""Set the untitled number counter
194+
195+
This is useful when loading macros from HDF5 or files to ensure
196+
that the next untitled macro has a unique name.
197+
198+
Args:
199+
number: New untitled number
200+
"""
201+
global UNTITLED_NB # pylint: disable=global-statement
202+
UNTITLED_NB = number
191203

192204
def modification_changed(self, state: bool) -> None:
193205
"""Method called when macro's editor modification state changed

datalab/gui/panel/macro.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,8 @@ def deserialize_from_hdf5(
256256
# in a group but directly in the root of the HDF5 file
257257
obj = self.deserialize_object_from_hdf5(reader, name, reset_all)
258258
self.add_object(obj)
259+
# Update untitled number counter to prevent duplicate names
260+
self.update_untitled_counter()
259261

260262
def __len__(self) -> int:
261263
"""Return number of objects"""
@@ -302,6 +304,8 @@ def remove_all_objects(self) -> None:
302304
self.tabwidget.clear()
303305
self.__macros.clear()
304306
super().remove_all_objects()
307+
# Reset untitled counter when clearing all macros
308+
Macro.set_untitled_number(0)
305309

306310
# ---- Macro panel API -------------------------------------------------------------
307311
def setup_actions(self) -> None:
@@ -576,9 +580,27 @@ def import_macro_from_file(self, filename: str | None = None) -> int:
576580
Conf.main.base_dir.set(filename)
577581
macro = self.add_macro()
578582
macro.from_file(filename)
583+
# Update untitled number counter to prevent duplicate names
584+
self.update_untitled_counter()
579585
return self.get_number_from_macro(macro)
580586
return -1
581587

588+
def update_untitled_counter(self) -> None:
589+
"""Update the untitled counter based on existing macro titles
590+
591+
This scans all macro titles to find the highest "macro_XX" number
592+
and updates the global counter to prevent duplicate names.
593+
"""
594+
max_untitled = 0
595+
for macro in self.__macros:
596+
# Match titles like "macro_01", "macro_02", etc.
597+
match = re.match(r"macro_(\d+)", macro.title)
598+
if match:
599+
number = int(match.group(1))
600+
max_untitled = max(max_untitled, number)
601+
# Set the counter to the highest found number
602+
Macro.set_untitled_number(max_untitled)
603+
582604
def remove_macro(self, number_or_title: int | str | None = None) -> None:
583605
"""Remove macro
584606

datalab/locale/fr/LC_MESSAGES/datalab.po

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ msgid ""
66
msgstr ""
77
"Project-Id-Version: datalab\n"
88
"Report-Msgid-Bugs-To: [email protected]\n"
9-
"POT-Creation-Date: 2025-12-03 12:01+0100\n"
9+
"POT-Creation-Date: 2025-12-04 17:48+0100\n"
1010
"PO-Revision-Date: 2025-05-22 15:46+0200\n"
1111
"Last-Translator: Christophe Debonnel <[email protected]>\n"
1212
"Language: fr\n"
@@ -672,9 +672,6 @@ msgstr "Aucune donnée prise en charge dans le(s) fichier(s) HDF5."
672672
msgid "Macro simple example"
673673
msgstr "Exemple simple de macro"
674674

675-
msgid "Untitled"
676-
msgstr "Sans titre"
677-
678675
#, python-format
679676
msgid "# ==> Unable to run '%s' macro"
680677
msgstr "# ==> Impossible d'exécuter la macro '%s'"

doc/locale/fr/LC_MESSAGES/release_notes/release_1.00.po

Lines changed: 16 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-12-04 15:55+0100\n"
10+
"POT-Creation-Date: 2025-12-04 17:48+0100\n"
1111
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
1212
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
1313
"Language: fr\n"
@@ -34,6 +34,21 @@ msgstr "**Disposition du panneau des macros :**"
3434
msgid "Fixed macro console taking excessive vertical space on first open - the script editor now properly gets 70% of space and console 30% by default, ensuring comfortable editing without manual resizing"
3535
msgstr "Correction du panneau de la console des macros prenant un espace vertical excessif à la première ouverture - l'éditeur de scripts obtient désormais correctement 70 % de l'espace et la console 30 % par défaut, garantissant une édition confortable sans redimensionnement manuel"
3636

37+
msgid "**Macro naming:**"
38+
msgstr "**Nommage des macros :**"
39+
40+
msgid "Fixed automatic macro naming generating duplicate names after reloading workspace from HDF5 files"
41+
msgstr "Correction du nommage automatique des macros générant des noms en double après le rechargement de l'espace de travail à partir de fichiers HDF5"
42+
43+
msgid "When macros were deserialized from saved workspaces or imported from files, the internal counter used for generating default names (e.g., \"macro_01\", \"macro_02\") was not updated, causing the next new macro to potentially reuse an existing name"
44+
msgstr "Lorsque les macros étaient désérialisées à partir d'espaces de travail enregistrés ou importées à partir de fichiers, le compteur interne utilisé pour générer des noms par défaut (par exemple, \"macro_01\", \"macro_02\") n'était pas mis à jour, ce qui pouvait entraîner la réutilisation d'un nom existant pour la prochaine nouvelle macro"
45+
46+
msgid "The counter now synchronizes with existing macro names after loading, ensuring unique sequential naming"
47+
msgstr "Le compteur se synchronise désormais avec les noms de macros existants après le chargement, garantissant un nommage séquentiel unique"
48+
49+
msgid "Default macro names simplified from translated \"Untitled XX\" to language-neutral \"macro_XX\" format for better consistency across locales"
50+
msgstr "Les noms de macros par défaut ont été simplifiés du format traduit \"Sans titre XX\" au format neutre en termes de langue \"macro_XX\" pour une meilleure cohérence entre les locales"
51+
3752
msgid "**Macro execution:**"
3853
msgstr "**Exécution des macros :**"
3954

doc/release_notes/release_1.00.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@
88

99
* Fixed macro console taking excessive vertical space on first open - the script editor now properly gets 70% of space and console 30% by default, ensuring comfortable editing without manual resizing
1010

11+
**Macro naming:**
12+
13+
* Fixed automatic macro naming generating duplicate names after reloading workspace from HDF5 files
14+
* When macros were deserialized from saved workspaces or imported from files, the internal counter used for generating default names (e.g., "macro_01", "macro_02") was not updated, causing the next new macro to potentially reuse an existing name
15+
* The counter now synchronizes with existing macro names after loading, ensuring unique sequential naming
16+
* Default macro names simplified from translated "Untitled XX" to language-neutral "macro_XX" format for better consistency across locales
17+
1118
**Macro execution:**
1219

1320
* Fixed syntax errors when using f-strings with nested quotes in macros (e.g., `f'text {func("arg")}'` now works correctly)

0 commit comments

Comments
 (0)