Skip to content

Commit 3196678

Browse files
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
1 parent bea6913 commit 3196678

File tree

4 files changed

+21
-10
lines changed

4 files changed

+21
-10
lines changed

tagstudio/src/core/library/alchemy/library.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -998,6 +998,15 @@ def get_tag(self, tag_id: int) -> Tag:
998998

999999
return tag
10001000

1001+
def get_tag_by_name(self, tag_name: str) -> Tag | None:
1002+
with Session(self.engine) as session:
1003+
statement = (
1004+
select(Tag)
1005+
.outerjoin(TagAlias)
1006+
.where(or_(Tag.name == tag_name, TagAlias.name == tag_name))
1007+
)
1008+
return session.scalar(statement)
1009+
10011010
def get_alias(self, tag_id: int, alias_id: int) -> TagAlias:
10021011
with Session(self.engine) as session:
10031012
alias_query = select(TagAlias).where(TagAlias.id == alias_id, TagAlias.tag_id == tag_id)

tagstudio/src/core/ts_core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def get_gdl_sidecar(cls, filepath: Path, source: str = "") -> dict:
2424
Return a formatted object with notable values or an empty object if none is found.
2525
"""
2626
info = {}
27-
_filepath = filepath.parent / (filepath.stem + ".json")
27+
_filepath = filepath.parent / (filepath.name + ".json")
2828

2929
# NOTE: This fixes an unknown (recent?) bug in Gallery-DL where Instagram sidecar
3030
# files may be downloaded with indices starting at 1 rather than 0, unlike the posts.

tagstudio/src/qt/ts_qt.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
SearchMode,
7373
)
7474
from src.core.library.alchemy.fields import _FieldID
75-
from src.core.library.alchemy.library import LibraryStatus
75+
from src.core.library.alchemy.library import Entry, Library, LibraryStatus
7676
from src.core.media_types import MediaCategories
7777
from src.core.ts_core import TagStudioCore
7878
from src.core.utils.refresh_dir import RefreshDirTracker
@@ -130,6 +130,7 @@ class QtDriver(DriverMixin, QObject):
130130
SIGTERM = Signal()
131131

132132
preview_panel: PreviewPanel
133+
lib: Library
133134

134135
def __init__(self, backend, args):
135136
super().__init__()
@@ -788,9 +789,9 @@ def run_macros(self, name: MacroID, grid_idx: list[int]):
788789

789790
def run_macro(self, name: MacroID, grid_idx: int):
790791
"""Run a specific Macro on an Entry given a Macro name."""
791-
entry = self.frame_content[grid_idx]
792-
ful_path = self.lib.library_dir / entry.path
793-
source = entry.path.parts[0]
792+
entry: Entry = self.frame_content[grid_idx]
793+
full_path = self.lib.library_dir / entry.path
794+
source = "" if entry.path.parent == Path(".") else entry.path.parts[0].lower()
794795

795796
logger.info(
796797
"running macro",
@@ -804,10 +805,10 @@ def run_macro(self, name: MacroID, grid_idx: int):
804805
for macro_id in MacroID:
805806
if macro_id == MacroID.AUTOFILL:
806807
continue
807-
self.run_macro(macro_id, entry.id)
808+
self.run_macro(macro_id, grid_idx)
808809

809810
elif name == MacroID.SIDECAR:
810-
parsed_items = TagStudioCore.get_gdl_sidecar(ful_path, source)
811+
parsed_items = TagStudioCore.get_gdl_sidecar(full_path, source)
811812
for field_id, value in parsed_items.items():
812813
if isinstance(value, list) and len(value) > 0 and isinstance(value[0], str):
813814
value = self.lib.tag_from_strings(value)
@@ -818,8 +819,9 @@ def run_macro(self, name: MacroID, grid_idx: int):
818819
)
819820

820821
elif name == MacroID.BUILD_URL:
821-
url = TagStudioCore.build_url(entry.id, source)
822-
self.lib.add_entry_field_type(entry.id, field_id=_FieldID.SOURCE, value=url)
822+
url = TagStudioCore.build_url(entry, source)
823+
if url is not None:
824+
self.lib.add_entry_field_type(entry.id, field_id=_FieldID.SOURCE, value=url)
823825
elif name == MacroID.MATCH:
824826
TagStudioCore.match_conditions(self.lib, entry.id)
825827
elif name == MacroID.CLEAN_URL:

tagstudio/tests/macros/test_sidecar.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def test_sidecar_macro(qt_driver, library, cwd, entry_full):
1212
entry_full.path = Path("newgrounds/foo.txt")
1313

1414
fixture = cwd / "fixtures/sidecar_newgrounds.json"
15-
dst = library.library_dir / "newgrounds" / (entry_full.path.stem + ".json")
15+
dst = library.library_dir / "newgrounds" / (entry_full.path.name + ".json")
1616
dst.parent.mkdir()
1717
shutil.copy(fixture, dst)
1818

0 commit comments

Comments
 (0)