From 3196678666b8541270872f566c2bb3da0dac4bcf Mon Sep 17 00:00:00 2001 From: Jann Stute <46534683+Computerdores@users.noreply.github.com> Date: Thu, 5 Dec 2024 09:43:39 +0100 Subject: [PATCH] fix: multiple macro errors (#612) * port fixes from json branch * fix: correctly pass grid_idx in autofill macro * fix(BUILD_URL macro): only add url if url was successfully built * fix(AUTOFILL macro): error when trying to add tag with name that already exists * fix: test was wrongly renaming sidecar file --- tagstudio/src/core/library/alchemy/library.py | 9 +++++++++ tagstudio/src/core/ts_core.py | 2 +- tagstudio/src/qt/ts_qt.py | 18 ++++++++++-------- tagstudio/tests/macros/test_sidecar.py | 2 +- 4 files changed, 21 insertions(+), 10 deletions(-) diff --git a/tagstudio/src/core/library/alchemy/library.py b/tagstudio/src/core/library/alchemy/library.py index d960afbd1..a5398a6d9 100644 --- a/tagstudio/src/core/library/alchemy/library.py +++ b/tagstudio/src/core/library/alchemy/library.py @@ -998,6 +998,15 @@ def get_tag(self, tag_id: int) -> Tag: return tag + def get_tag_by_name(self, tag_name: str) -> Tag | None: + with Session(self.engine) as session: + statement = ( + select(Tag) + .outerjoin(TagAlias) + .where(or_(Tag.name == tag_name, TagAlias.name == tag_name)) + ) + return session.scalar(statement) + def get_alias(self, tag_id: int, alias_id: int) -> TagAlias: with Session(self.engine) as session: alias_query = select(TagAlias).where(TagAlias.id == alias_id, TagAlias.tag_id == tag_id) diff --git a/tagstudio/src/core/ts_core.py b/tagstudio/src/core/ts_core.py index 9611397ec..aaae942e4 100644 --- a/tagstudio/src/core/ts_core.py +++ b/tagstudio/src/core/ts_core.py @@ -24,7 +24,7 @@ def get_gdl_sidecar(cls, filepath: Path, source: str = "") -> dict: Return a formatted object with notable values or an empty object if none is found. """ info = {} - _filepath = filepath.parent / (filepath.stem + ".json") + _filepath = filepath.parent / (filepath.name + ".json") # NOTE: This fixes an unknown (recent?) bug in Gallery-DL where Instagram sidecar # files may be downloaded with indices starting at 1 rather than 0, unlike the posts. diff --git a/tagstudio/src/qt/ts_qt.py b/tagstudio/src/qt/ts_qt.py index d1d09d43b..c6ef2034e 100644 --- a/tagstudio/src/qt/ts_qt.py +++ b/tagstudio/src/qt/ts_qt.py @@ -72,7 +72,7 @@ SearchMode, ) from src.core.library.alchemy.fields import _FieldID -from src.core.library.alchemy.library import LibraryStatus +from src.core.library.alchemy.library import Entry, Library, LibraryStatus from src.core.media_types import MediaCategories from src.core.ts_core import TagStudioCore from src.core.utils.refresh_dir import RefreshDirTracker @@ -130,6 +130,7 @@ class QtDriver(DriverMixin, QObject): SIGTERM = Signal() preview_panel: PreviewPanel + lib: Library def __init__(self, backend, args): super().__init__() @@ -788,9 +789,9 @@ def run_macros(self, name: MacroID, grid_idx: list[int]): def run_macro(self, name: MacroID, grid_idx: int): """Run a specific Macro on an Entry given a Macro name.""" - entry = self.frame_content[grid_idx] - ful_path = self.lib.library_dir / entry.path - source = entry.path.parts[0] + entry: Entry = self.frame_content[grid_idx] + full_path = self.lib.library_dir / entry.path + source = "" if entry.path.parent == Path(".") else entry.path.parts[0].lower() logger.info( "running macro", @@ -804,10 +805,10 @@ def run_macro(self, name: MacroID, grid_idx: int): for macro_id in MacroID: if macro_id == MacroID.AUTOFILL: continue - self.run_macro(macro_id, entry.id) + self.run_macro(macro_id, grid_idx) elif name == MacroID.SIDECAR: - parsed_items = TagStudioCore.get_gdl_sidecar(ful_path, source) + parsed_items = TagStudioCore.get_gdl_sidecar(full_path, source) for field_id, value in parsed_items.items(): if isinstance(value, list) and len(value) > 0 and isinstance(value[0], str): value = self.lib.tag_from_strings(value) @@ -818,8 +819,9 @@ def run_macro(self, name: MacroID, grid_idx: int): ) elif name == MacroID.BUILD_URL: - url = TagStudioCore.build_url(entry.id, source) - self.lib.add_entry_field_type(entry.id, field_id=_FieldID.SOURCE, value=url) + url = TagStudioCore.build_url(entry, source) + if url is not None: + self.lib.add_entry_field_type(entry.id, field_id=_FieldID.SOURCE, value=url) elif name == MacroID.MATCH: TagStudioCore.match_conditions(self.lib, entry.id) elif name == MacroID.CLEAN_URL: diff --git a/tagstudio/tests/macros/test_sidecar.py b/tagstudio/tests/macros/test_sidecar.py index 700169f45..2bea7ba8e 100644 --- a/tagstudio/tests/macros/test_sidecar.py +++ b/tagstudio/tests/macros/test_sidecar.py @@ -12,7 +12,7 @@ def test_sidecar_macro(qt_driver, library, cwd, entry_full): entry_full.path = Path("newgrounds/foo.txt") fixture = cwd / "fixtures/sidecar_newgrounds.json" - dst = library.library_dir / "newgrounds" / (entry_full.path.stem + ".json") + dst = library.library_dir / "newgrounds" / (entry_full.path.name + ".json") dst.parent.mkdir() shutil.copy(fixture, dst)