This commit is contained in:
2026-03-25 22:39:30 -07:00
parent c31402c8f1
commit 562acd809c
46 changed files with 2367 additions and 1868 deletions

View File

@@ -6,6 +6,9 @@ import sys
import re
from SYS.logger import log, debug
from SYS.item_accessors import extract_item_tags, get_string_list, set_field
from SYS.payload_builders import extract_title_tag_value
from SYS.result_publication import publish_result_table
from SYS import models
from SYS import pipeline as ctx
@@ -24,7 +27,6 @@ collapse_namespace_tag = sh.collapse_namespace_tag
should_show_help = sh.should_show_help
get_field = sh.get_field
from Store import Store
from SYS.utils import sha256_file
_FIELD_NAME_RE = re.compile(r"^[A-Za-z0-9_]+$")
@@ -239,33 +241,15 @@ def _try_compile_extract_template(
def _extract_title_tag(tags: List[str]) -> Optional[str]:
"""Return the value of the first title: tag if present."""
for t in tags:
if t.lower().startswith("title:"):
value = t.split(":", 1)[1].strip()
return value or None
return None
return extract_title_tag_value(tags)
def _extract_item_tags(res: Any) -> List[str]:
if isinstance(res, models.PipeObject):
raw = getattr(res, "tag", None)
elif isinstance(res, dict):
raw = res.get("tag")
else:
raw = None
if isinstance(raw, list):
return [str(t) for t in raw if t is not None]
if isinstance(raw, str) and raw.strip():
return [raw]
return []
return extract_item_tags(res)
def _set_item_tags(res: Any, tags: List[str]) -> None:
if isinstance(res, models.PipeObject):
res.tag = tags
elif isinstance(res, dict):
res["tag"] = tags
set_field(res, "tag", tags)
def _apply_title_to_result(res: Any, title_value: Optional[str]) -> None:
@@ -401,7 +385,7 @@ def _refresh_result_table_title(
# Keep the underlying history intact; update only the overlay so @.. can
# clear the overlay then continue back to prior tables (e.g., the search list).
ctx.set_last_result_table_overlay(new_table, updated_items)
publish_result_table(ctx, new_table, updated_items, overlay=True)
except Exception:
pass
@@ -439,30 +423,21 @@ def _refresh_tag_view(
refresh_args: List[str] = ["-query", f"hash:{target_hash}"]
# Build a lean subject so get-tag fetches fresh tags instead of reusing cached payloads.
def _value_has_content(value: Any) -> bool:
if value is None:
return False
if isinstance(value, str):
return bool(value.strip())
if isinstance(value, (list, tuple, set)):
return len(value) > 0
return True
def _build_refresh_subject() -> Dict[str, Any]:
payload: Dict[str, Any] = {}
payload["hash"] = target_hash
if _value_has_content(store_name):
if sh.value_has_content(store_name):
payload["store"] = store_name
path_value = target_path or get_field(subject, "path")
if not _value_has_content(path_value):
if not sh.value_has_content(path_value):
path_value = get_field(subject, "target")
if _value_has_content(path_value):
if sh.value_has_content(path_value):
payload["path"] = path_value
for key in ("title", "name", "url", "relations", "service_name"):
val = get_field(subject, key)
if _value_has_content(val):
if sh.value_has_content(val):
payload[key] = val
extra_value = get_field(subject, "extra")
@@ -473,7 +448,7 @@ def _refresh_tag_view(
}
if cleaned:
payload["extra"] = cleaned
elif _value_has_content(extra_value):
elif sh.value_has_content(extra_value):
payload["extra"] = extra_value
return payload
@@ -570,15 +545,15 @@ class Add_Tag(Cmdlet):
extract_debug = bool(parsed.get("extract-debug", False))
extract_debug_rx, extract_debug_err = _try_compile_extract_template(extract_template)
query_hash = sh.parse_single_hash_query(parsed.get("query"))
if parsed.get("query") and not query_hash:
log(
"[add_tag] Error: -query must be of the form hash:<sha256>",
file=sys.stderr
)
query_hash, query_valid = sh.require_single_hash_query(
parsed.get("query"),
"[add_tag] Error: -query must be of the form hash:<sha256>",
log_file=sys.stderr,
)
if not query_valid:
return 1
hash_override = normalize_hash(query_hash) if query_hash else None
hash_override = query_hash
# If add-tag is in the middle of a pipeline (has downstream stages), default to
# including temp files. This enables common flows like:
@@ -879,21 +854,11 @@ class Add_Tag(Cmdlet):
)
return 1
resolved_hash = (
normalize_hash(hash_override)
if hash_override else normalize_hash(raw_hash)
resolved_hash = sh.resolve_hash_for_cmdlet(
str(raw_hash) if raw_hash else None,
str(raw_path) if raw_path else None,
str(hash_override) if hash_override else None,
)
if not resolved_hash and raw_path:
try:
p = Path(str(raw_path))
stem = p.stem
if len(stem) == 64 and all(c in "0123456789abcdef"
for c in stem.lower()):
resolved_hash = stem.lower()
elif p.exists() and p.is_file():
resolved_hash = sha256_file(p)
except Exception:
resolved_hash = None
if not resolved_hash:
log(
@@ -903,9 +868,13 @@ class Add_Tag(Cmdlet):
ctx.emit(res)
continue
try:
backend = store_registry[str(store_name)]
except Exception as exc:
backend, store_registry, exc = sh.get_store_backend(
config,
str(store_name),
store_registry=store_registry,
suppress_debug=True,
)
if backend is None:
log(
f"[add_tag] Error: Unknown store '{store_name}': {exc}",
file=sys.stderr