from __future__ import annotations from typing import Any, Dict, Sequence import json from . import register import models import pipeline as ctx from API import HydrusNetwork as hydrus_wrapper from ._shared import Cmdlet, CmdletArg, normalize_hash, should_show_help from SYS.logger import log CMDLET = Cmdlet( name="add-note", summary="Add or set a note on a Hydrus file.", usage="add-note [-hash ] ", arg=[ CmdletArg("hash", type="string", description="Override the Hydrus file hash (SHA256) to target instead of the selected result."), CmdletArg("name", type="string", required=True, description="The note name/key to set (e.g. 'comment', 'source', etc.)."), CmdletArg("text", type="string", required=True, description="The note text/content to store.", variadic=True), ], detail=[ "- Notes are stored in the 'my notes' service by default.", ], ) @register(["add-note", "set-note", "add_note"]) # aliases def add(result: Any, args: Sequence[str], config: Dict[str, Any]) -> int: # Help if should_show_help(args): log(json.dumps(CMDLET, ensure_ascii=False, indent=2)) return 0 from ._shared import parse_cmdlet_args parsed = parse_cmdlet_args(args, CMDLET) override_hash = parsed.get("hash") name = parsed.get("name") text_parts = parsed.get("text") if not name: log("Requires a note name") return 1 name = str(name).strip() if isinstance(text_parts, list): text = " ".join(text_parts).strip() else: text = str(text_parts or "").strip() if not text: log("Empty note text") return 1 # Handle @N selection which creates a list - extract the first item if isinstance(result, list) and len(result) > 0: result = result[0] hash_hex = normalize_hash(override_hash) if override_hash else normalize_hash(getattr(result, "hash_hex", None)) if not hash_hex: log("Selected result does not include a Hydrus hash") return 1 try: client = hydrus_wrapper.get_client(config) except Exception as exc: log(f"Hydrus client unavailable: {exc}") return 1 if client is None: log("Hydrus client unavailable") return 1 try: service_name = "my notes" client.set_notes(hash_hex, {name: text}, service_name) except Exception as exc: log(f"Hydrus add-note failed: {exc}") return 1 # Refresh notes view if we're operating on the currently selected subject try: from cmdlets import get_note as get_note_cmd # type: ignore except Exception: get_note_cmd = None if get_note_cmd: try: subject = ctx.get_last_result_subject() if subject is not None: def norm(val: Any) -> str: return str(val).lower() target_hash = norm(hash_hex) if hash_hex else None subj_hashes = [] if isinstance(subject, dict): subj_hashes = [norm(v) for v in [subject.get("hydrus_hash"), subject.get("hash"), subject.get("hash_hex"), subject.get("file_hash")] if v] else: subj_hashes = [norm(getattr(subject, f, None)) for f in ("hydrus_hash", "hash", "hash_hex", "file_hash") if getattr(subject, f, None)] if target_hash and target_hash in subj_hashes: get_note_cmd.get_notes(subject, ["-hash", hash_hex], config) return 0 except Exception: pass ctx.emit(f"Added note '{name}' ({len(text)} chars)") return 0