This commit is contained in:
2026-01-02 02:28:59 -08:00
parent deb05c0d44
commit 6e9a0c28ff
13 changed files with 1402 additions and 2334 deletions

View File

@@ -23,6 +23,14 @@ from SYS.utils import sha256_file
class Add_Note(Cmdlet):
DEFAULT_QUERY_HINTS = (
"title:",
"text:",
"hash:",
"caption:",
"sub:",
"subtitle:",
)
def __init__(self) -> None:
super().__init__(
@@ -124,6 +132,45 @@ class Add_Note(Cmdlet):
note_text = text_match.group(1).strip() if text_match else ""
return (note_name or None, note_text or None)
@classmethod
def _looks_like_note_query_token(cls, token: Any) -> bool:
text = str(token or "").strip().lower()
if not text:
return False
return any(hint in text for hint in cls.DEFAULT_QUERY_HINTS)
@classmethod
def _default_query_args(cls, args: Sequence[str]) -> List[str]:
tokens: List[str] = list(args or [])
lower_tokens = {str(tok).lower() for tok in tokens if tok is not None}
if "-query" in lower_tokens or "--query" in lower_tokens:
return tokens
for idx, tok in enumerate(tokens):
token_text = str(tok or "")
if not token_text or token_text.startswith("-"):
continue
if not cls._looks_like_note_query_token(token_text):
continue
combined_parts = [token_text]
end = idx + 1
while end < len(tokens):
next_text = str(tokens[end] or "")
if not next_text or next_text.startswith("-"):
break
if not cls._looks_like_note_query_token(next_text):
break
combined_parts.append(next_text)
end += 1
combined_query = " ".join(combined_parts)
tokens[idx:end] = [combined_query]
tokens.insert(idx, "-query")
return tokens
return tokens
def _resolve_hash(
self,
raw_hash: Optional[str],
@@ -153,11 +200,14 @@ class Add_Note(Cmdlet):
log(f"Cmdlet: {self.name}\nSummary: {self.summary}\nUsage: {self.usage}")
return 0
parsed = parse_cmdlet_args(args, self)
parsed_args = self._default_query_args(args)
parsed = parse_cmdlet_args(parsed_args, self)
store_override = parsed.get("store")
hash_override = normalize_hash(parsed.get("hash"))
note_name, note_text = self._parse_note_query(str(parsed.get("query") or ""))
note_name = str(note_name or "").strip()
note_text = str(note_text or "").strip()
if not note_name or not note_text:
log(
"[add_note] Error: -query must include title:<title> and text:<text>",
@@ -173,7 +223,6 @@ class Add_Note(Cmdlet):
return 1
explicit_target = bool(hash_override and store_override)
results = normalize_result_input(result)
if results and explicit_target:
# Direct targeting mode: apply note once to the explicit target and
@@ -194,14 +243,22 @@ class Add_Note(Cmdlet):
f"✓ add-note: 1 item in '{store_override}'",
file=sys.stderr
)
log(
"[add_note] Updated 1/1 item(s)",
file=sys.stderr
)
for res in results:
ctx.emit(res)
return 0
log(
"[add_note] Warning: Note write reported failure",
file=sys.stderr
)
return 1
except Exception as exc:
log(f"[add_note] Error: Failed to set note: {exc}", file=sys.stderr)
return 1
for res in results:
ctx.emit(res)
return 0
if not results:
if explicit_target:
# Allow standalone use (no piped input) and enable piping the target forward.
@@ -217,7 +274,7 @@ class Add_Note(Cmdlet):
return 1
store_registry = Store(config)
updated = 0
planned_ops = 0
# Batch write plan: store -> [(hash, name, text), ...]
note_ops: Dict[str,
@@ -271,12 +328,12 @@ class Add_Note(Cmdlet):
[]).append((resolved_hash,
note_name,
item_note_text))
updated += 1
planned_ops += 1
ctx.emit(res)
# Execute bulk writes per store.
wrote_any = False
successful_writes = 0
for store_name, ops in note_ops.items():
if not ops:
continue
@@ -285,16 +342,23 @@ class Add_Note(Cmdlet):
except Exception:
continue
store_success = 0
bulk_fn = getattr(backend, "set_note_bulk", None)
if callable(bulk_fn):
try:
ok = bool(bulk_fn(list(ops), config=config))
wrote_any = wrote_any or ok or True
ctx.print_if_visible(
f"✓ add-note: {len(ops)} item(s) in '{store_name}'",
file=sys.stderr
if ok:
store_success += len(ops)
ctx.print_if_visible(
f"✓ add-note: {len(ops)} item(s) in '{store_name}'",
file=sys.stderr
)
successful_writes += store_success
continue
log(
f"[add_note] Warning: bulk set_note returned False for '{store_name}'",
file=sys.stderr,
)
continue
except Exception as exc:
log(
f"[add_note] Warning: bulk set_note failed for '{store_name}': {exc}; falling back",
@@ -305,12 +369,23 @@ class Add_Note(Cmdlet):
for file_hash, name, text in ops:
try:
ok = bool(backend.set_note(file_hash, name, text, config=config))
wrote_any = wrote_any or ok
if ok:
store_success += 1
except Exception:
continue
log(f"[add_note] Updated {updated} item(s)", file=sys.stderr)
return 0 if (updated > 0 and wrote_any) else (0 if updated > 0 else 1)
if store_success:
successful_writes += store_success
ctx.print_if_visible(
f"✓ add-note: {store_success} item(s) in '{store_name}'",
file=sys.stderr
)
log(
f"[add_note] Updated {successful_writes}/{planned_ops} item(s)",
file=sys.stderr
)
return 0 if successful_writes > 0 else 1
CMDLET = Add_Note()