diff --git a/cmdlet/get_relationship.py b/cmdlet/get_relationship.py index 7d8647b..f8fcd72 100644 --- a/cmdlet/get_relationship.py +++ b/cmdlet/get_relationship.py @@ -36,94 +36,72 @@ CMDLET = Cmdlet( def _run(result: Any, _args: Sequence[str], config: Dict[str, Any]) -> int: - pass + # Help + if should_show_help(_args): + log(f"Cmdlet: {CMDLET.name}\nSummary: {CMDLET.summary}\nUsage: {CMDLET.usage}") + return 0 - _add_relationship( - { - "hash": related_hash, - "type": entry_type, - "title": related_title, - "path": None, - "store": str(store_name), - } - ) + # Parse -query and -store override + override_query: str | None = None + override_store: str | None = None + args_list = list(_args) + i = 0 + while i < len(args_list): + a = args_list[i] + low = str(a).lower() + if low in {"-query", "--query", "query"} and i + 1 < len(args_list): + override_query = str(args_list[i + 1]).strip() + i += 2 + continue + if low in {"-store", "--store", "store"} and i + 1 < len(args_list): + override_store = str(args_list[i + 1]).strip() + i += 2 + continue + i += 1 - # Reverse relationships (alts pointing to this hash) - try: - reverse_children = db.find_files_pointing_to_hash(hash_hex) - except Exception: - reverse_children = [] + override_hash: str | None = ( + sh.parse_single_hash_query(override_query) if override_query else None + ) + if override_query and not override_hash: + log('get-relationship requires -query "hash:"', file=sys.stderr) + return 1 - for child in reverse_children or []: - child_hash = normalize_hash(str(child.get("hash") or "")) - rel_type = str(child.get("type") or "").strip().lower() - if not child_hash or child_hash == hash_hex: - continue + # Handle @N selection which creates a list + if isinstance(result, list): + if len(result) == 0: + result = None + elif len(result) > 1 and not override_hash: + log( + 'get-relationship expects a single item; select one row (e.g. @1) or pass -query "hash:"', + file=sys.stderr, + ) + return 1 + else: + result = result[0] - child_title = child_hash[:16] + "..." - try: - child_tags = db.get_tags(child_hash) - for t in child_tags: - if isinstance(t, - str) and t.lower().startswith("title:"): - child_title = t[6:].strip() - break - except Exception: - pass + # Initialize results collection + found_relationships = [] # List of dicts: {hash, type, title, path, store} + source_title = "Unknown" - entry_type = "alt" if rel_type == "alt" else ( - rel_type or "related" - ) - _add_relationship( - { - "hash": child_hash, - "type": entry_type, - "title": child_title, - "path": None, - "store": str(store_name), - } - ) + # Store/hash-first subject resolution + store_name: Optional[str] = override_store + if not store_name: + store_name = get_field(result, "store") - # Siblings (alts that share the same king) - for king_hash in king_hashes: - try: - siblings = db.find_files_pointing_to_hash(king_hash) - except Exception: - siblings = [] - for sib in siblings or []: - sib_hash = normalize_hash(str(sib.get("hash") or "")) - sib_type = str(sib.get("type") or "").strip().lower() - if not sib_hash or sib_hash in {hash_hex, - king_hash}: - continue + hash_hex = ( + normalize_hash(override_hash) + if override_hash else normalize_hash(get_hash_for_operation(None, result)) + ) - sib_title = sib_hash[:16] + "..." - try: - sib_tags = db.get_tags(sib_hash) - for t in sib_tags: - if isinstance( - t, - str) and t.lower().startswith("title:"): - sib_title = t[6:].strip() - break - except Exception: - pass + if not source_title or source_title == "Unknown": + source_title = ( + get_field(result, "title") or get_field(result, "name") + or (hash_hex[:16] + "..." if hash_hex else "Unknown") + ) - entry_type = "alt" if sib_type == "alt" else ( - sib_type or "related" - ) - _add_relationship( - { - "hash": sib_hash, - "type": entry_type, - "title": sib_title, - "path": None, - "store": str(store_name), - } - ) - - except Exception as e: - log(f"Error checking store relationships: {e}", file=sys.stderr) + if not hash_hex: + log('get-relationship requires -query "hash:"', file=sys.stderr) + return 1 # Fetch Hydrus relationships if we have a hash. diff --git a/cmdnat/pipe.py b/cmdnat/pipe.py index 0bf2299..7e5e981 100644 --- a/cmdnat/pipe.py +++ b/cmdnat/pipe.py @@ -1473,31 +1473,31 @@ def _run(result: Any, args: Sequence[str], config: Dict[str, Any]) -> int: name, items = result current_playlist_name = name - # Queue items (replacing current playlist) - if items: - _queue_items( - items, - clear_first=True, - config=config, - start_opts=start_opts - ) - else: - # Empty playlist, just clear - _send_ipc_command( - { - "command": ["playlist-clear"] - }, - silent=True - ) + # Queue items (replacing current playlist) + if items: + _queue_items( + items, + clear_first=True, + config=config, + start_opts=start_opts + ) + else: + # Empty playlist, just clear + _send_ipc_command( + { + "command": ["playlist-clear"] + }, + silent=True + ) - # Switch to list mode to show the result - list_mode = True - index_arg = None - # Fall through to list logic + # Switch to list mode to show the result + list_mode = True + index_arg = None + # Fall through to list logic - except ValueError: - debug(f"Invalid playlist ID: {index_arg}") - return 1 + except ValueError: + debug(f"Invalid playlist ID: {index_arg}") + return 1 # If we deleted or didn't have an index, list playlists if not index_arg: