dfd
This commit is contained in:
@@ -1128,6 +1128,47 @@ def merge_sequences(*sources: Optional[Iterable[Any]], case_sensitive: bool = Tr
|
||||
return merged
|
||||
|
||||
|
||||
def collapse_namespace_tags(tags: Optional[Iterable[Any]], namespace: str, prefer: str = "last") -> list[str]:
|
||||
"""Reduce tags so only one entry for a given namespace remains.
|
||||
|
||||
Keeps either the first or last occurrence (default last) while preserving overall order
|
||||
for non-matching tags. Useful for ensuring a single title: tag.
|
||||
"""
|
||||
if not tags:
|
||||
return []
|
||||
ns = str(namespace or "").strip().lower()
|
||||
if not ns:
|
||||
return list(tags) if isinstance(tags, list) else list(tags)
|
||||
|
||||
prefer_last = str(prefer or "last").lower() != "first"
|
||||
ns_prefix = ns + ":"
|
||||
|
||||
items = list(tags)
|
||||
if prefer_last:
|
||||
kept: list[str] = []
|
||||
seen_ns = False
|
||||
for tag in reversed(items):
|
||||
text = str(tag)
|
||||
if text.lower().startswith(ns_prefix):
|
||||
if seen_ns:
|
||||
continue
|
||||
seen_ns = True
|
||||
kept.append(text)
|
||||
kept.reverse()
|
||||
return kept
|
||||
else:
|
||||
kept_ns = False
|
||||
result: list[str] = []
|
||||
for tag in items:
|
||||
text = str(tag)
|
||||
if text.lower().startswith(ns_prefix):
|
||||
if kept_ns:
|
||||
continue
|
||||
kept_ns = True
|
||||
result.append(text)
|
||||
return result
|
||||
|
||||
|
||||
def extract_tags_from_result(result: Any) -> list[str]:
|
||||
tags: list[str] = []
|
||||
if isinstance(result, models.PipeObject):
|
||||
|
||||
Reference in New Issue
Block a user