Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: multiple macro errors #612

Merged
merged 6 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -997,6 +997,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