From 4a55af66ee7e72c24e598d2ef707d0004b188fb4 Mon Sep 17 00:00:00 2001 From: Travis Abendshien Date: Wed, 24 Apr 2024 16:22:36 -0700 Subject: [PATCH] Remove duplicate tag IDs when loading library Tags with duplicate IDs inside a library save file are removed when opening the library. Cleans up the mess from #38. --- tagstudio/src/core/library.py | 68 ++++++++++++++++++----------------- 1 file changed, 36 insertions(+), 32 deletions(-) diff --git a/tagstudio/src/core/library.py b/tagstudio/src/core/library.py index 4b74fe08..ac35ec7f 100644 --- a/tagstudio/src/core/library.py +++ b/tagstudio/src/core/library.py @@ -630,41 +630,45 @@ class Library: if 'id' in tag.keys(): id = tag['id'] - if int(id) >= self._next_tag_id: - self._next_tag_id = int(id) + 1 + # Don't load tags with duplicate IDs + if id not in [t.id for t in self.tags]: + if int(id) >= self._next_tag_id: + self._next_tag_id = int(id) + 1 - name = '' - if 'name' in tag.keys(): - name = tag['name'] - shorthand = '' - if 'shorthand' in tag.keys(): - shorthand = tag['shorthand'] - aliases = [] - if 'aliases' in tag.keys(): - aliases = tag['aliases'] - subtag_ids = [] - if 'subtag_ids' in tag.keys(): - subtag_ids = tag['subtag_ids'] - color = '' - if 'color' in tag.keys(): - color = tag['color'] + name = '' + if 'name' in tag.keys(): + name = tag['name'] + shorthand = '' + if 'shorthand' in tag.keys(): + shorthand = tag['shorthand'] + aliases = [] + if 'aliases' in tag.keys(): + aliases = tag['aliases'] + subtag_ids = [] + if 'subtag_ids' in tag.keys(): + subtag_ids = tag['subtag_ids'] + color = '' + if 'color' in tag.keys(): + color = tag['color'] - t = Tag( - id=int(id), - name=name, - shorthand=shorthand, - aliases=aliases, - subtags_ids=subtag_ids, - color=color - ) + t = Tag( + id=int(id), + name=name, + shorthand=shorthand, + aliases=aliases, + subtags_ids=subtag_ids, + color=color + ) - # NOTE: This does NOT use the add_tag_to_library() method! - # That method is only used for Tags added at runtime. - # This process uses the same inner methods, but waits until all of the - # Tags are registered in the Tags list before creating the Tag clusters. - self.tags.append(t) - self._map_tag_id_to_index(t, -1) - self._map_tag_strings_to_tag_id(t) + # NOTE: This does NOT use the add_tag_to_library() method! + # That method is only used for Tags added at runtime. + # This process uses the same inner methods, but waits until all of the + # Tags are registered in the Tags list before creating the Tag clusters. + self.tags.append(t) + self._map_tag_id_to_index(t, -1) + self._map_tag_strings_to_tag_id(t) + else: + logging.info(f'[LIBRARY]Skipping Tag with duplicate ID: {tag}') # Step 3: Map each Tag's subtags together now that all Tag objects in it. for t in self.tags: