85 lines
2.6 KiB
Python
85 lines
2.6 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import Any, Dict, Sequence
|
|
import json
|
|
|
|
from . import register
|
|
import models
|
|
import pipeline as ctx
|
|
from helper import hydrus as hydrus_wrapper
|
|
from ._shared import Cmdlet, CmdletArg, normalize_hash
|
|
from helper.logger import log
|
|
|
|
CMDLET = Cmdlet(
|
|
name="add-note",
|
|
summary="Add or set a note on a Hydrus file.",
|
|
usage="add-note [-hash <sha256>] <name> <text>",
|
|
args=[
|
|
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),
|
|
],
|
|
details=[
|
|
"- 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
|
|
try:
|
|
if any(str(a).lower() in {"-?", "/?", "--help", "-h", "help", "--cmdlet"} for a in args):
|
|
log(json.dumps(CMDLET, ensure_ascii=False, indent=2))
|
|
return 0
|
|
except Exception:
|
|
pass
|
|
|
|
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
|
|
ctx.emit(f"Added note '{name}' ({len(text)} chars)")
|
|
return 0
|
|
|