Skip to content

Commit

Permalink
fix: multiple macro errors (#612)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Computerdores authored Dec 5, 2024
1 parent bea6913 commit 3196678
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 deletions.
9 changes: 9 additions & 0 deletions tagstudio/src/core/library/alchemy/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion tagstudio/src/core/ts_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
18 changes: 10 additions & 8 deletions tagstudio/src/qt/ts_qt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -130,6 +130,7 @@ class QtDriver(DriverMixin, QObject):
SIGTERM = Signal()

preview_panel: PreviewPanel
lib: Library

def __init__(self, backend, args):
super().__init__()
Expand Down Expand Up @@ -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",
Expand All @@ -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)
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion tagstudio/tests/macros/test_sidecar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 3196678

Please sign in to comment.