feat(dl): add tag_rules to conditionally swap the group tag

This commit is contained in:
imSp4rky
2026-08-04 09:42:42 -06:00
parent 051f1e668f
commit 3e0d7b5f75
9 changed files with 598 additions and 3 deletions
+2
View File
@@ -130,6 +130,7 @@ class Config:
self.redact_paths: bool = kwargs.get("redact_paths", True)
self.language_tags: dict = kwargs.get("language_tags") or {}
self.tag_rules: list = kwargs.get("tag_rules") or []
self.dual_multi_mode: str = (kwargs.get("dual_multi_mode") or "strict").lower()
self.output_template: dict = kwargs.get("output_template") or {}
folder_cfg = self.output_template.pop("folder", "")
@@ -203,6 +204,7 @@ class Config:
"edition",
"repack",
"lang_tag",
"title_type",
}
unsafe_chars = r'[<>:"/\\|?*]'
+10
View File
@@ -95,6 +95,9 @@ class Title:
"hfr": "",
"edition": "",
"lang_tag": "",
"title_type": {"Movie": "movie", "Episode": "series", "Song": "music"}.get(
type(self).__name__, type(self).__name__.lower()
),
}
if self.tracks:
@@ -199,6 +202,13 @@ class Title:
sub_langs = [s.language for s in self.tracks.subtitles]
context["lang_tag"] = evaluate_language_tag(lang_tag_rules, audio_langs, sub_langs)
if config.tag_rules:
from unshackle.core.utils.tag_rules import evaluate_tag_rules
override = evaluate_tag_rules(config.tag_rules, context)
if override:
context["tag"] = override
return context
@abstractmethod
+116
View File
@@ -0,0 +1,116 @@
"""Release-group tag rule engine for output filename templates."""
from __future__ import annotations
import logging
import operator
import re
from collections.abc import Mapping
from typing import Any, Callable, Optional
log = logging.getLogger(__name__)
_OPS: dict[str, Callable[[Any, Any], bool]] = {
">=": operator.ge,
"<=": operator.le,
"!=": operator.ne,
"==": operator.eq,
">": operator.gt,
"<": operator.lt,
"=": operator.eq,
}
_ORDERING = (">=", "<=", ">", "<")
_NUMBER = re.compile(r"\d+(?:\.\d+)?")
def evaluate_tag_rules(rules: Any, context: dict[str, Any]) -> Optional[str]:
"""Evaluate tag rules against a filename template context.
Rules are evaluated in order; the first matching rule's tag is returned.
Conditions are AND-ed, compared case-insensitively, and a list matches if
any entry matches. A value may lead with a comparison operator, e.g.
">=2160" or "!=DV".
Args:
rules: Rules straight from the user's YAML, so any shape is possible.
Each valid rule is a mapping with a ``when`` mapping and a ``tag``.
context: The already-built filename template context.
Returns:
The tag string from the first matching rule, or ``None`` if none match.
"""
if not isinstance(rules, list):
log.warning("tag_rules must be a list of rules, ignoring: %r", rules)
return None
for rule in rules:
if not isinstance(rule, Mapping):
log.warning("Tag rule must be a mapping of 'when' and 'tag', skipping: %r", rule)
continue
when = rule.get("when")
tag = rule.get("tag")
if when is not None and not isinstance(when, Mapping):
log.warning("Tag rule 'when' must be a mapping of conditions, skipping: %s", rule)
continue
if not when or not tag:
log.warning("Tag rule needs both 'when' conditions and a 'tag', skipping: %s", rule)
continue
if isinstance(tag, bool) or any(_has_bool(value) for value in when.values()):
log.warning("Tag rule has a YAML boolean, quote the value to use it as text, skipping: %s", rule)
continue
if not isinstance(tag, (str, int, float)):
log.warning("Tag rule 'tag' must be text or a number, skipping: %s", rule)
continue
unknown = [key for key in when if key not in context]
if unknown:
log.warning("Tag rule has unknown 'when' key(s) %s, skipping: %s", ", ".join(sorted(unknown)), rule)
continue
if all(_matches(expected, context[key]) for key, expected in when.items()):
log.debug("Tag rule matched: %s -> %s", rule, tag)
return str(tag)
return None
def _has_bool(expected: Any) -> bool:
"""Check for a YAML boolean, which is an unquoted word the user meant as text."""
values = expected if isinstance(expected, list) else [expected]
return any(isinstance(x, bool) for x in values)
def _matches(expected: Any, actual: Any) -> bool:
"""Check one condition, where a list matches on any entry."""
values = expected if isinstance(expected, list) else [expected]
return any(_matches_one(x, actual) for x in values)
def _matches_one(expected: Any, actual: Any) -> bool:
"""Check a single value, which may carry a leading comparison operator."""
text = "" if expected is None else str(expected)
for op in sorted(_OPS, key=len, reverse=True):
if text.startswith(op):
return _compare(op, text[len(op) :].strip(), actual)
return str(actual or "").lower() == text.lower()
def _compare(op: str, operand: str, actual: Any) -> bool:
"""Compare against an operand, numerically when the operand is a number."""
if not operand:
log.warning("Tag rule condition '%s' has no value to compare against, condition failed", op)
return False
try:
wanted = float(operand)
except ValueError:
if op in _ORDERING:
log.warning("Tag rule condition '%s%s' needs a numeric operand, condition failed", op, operand)
return False
return _OPS[op](str(actual or "").lower(), operand.lower())
found = _NUMBER.search(str(actual or ""))
if not found:
return op == "!="
return _OPS[op](float(found.group()), wanted)