refactor: skip a enricher if fields it can provide are already filled

This commit is contained in:
2026-04-03 17:51:59 +02:00
parent 3917d68880
commit c91d5220bd
5 changed files with 19 additions and 1 deletions
+6
View File
@@ -26,6 +26,12 @@ def enrich_track(track: TrackMeta) -> TrackMeta:
"""
for enricher in _ENRICHERS:
try:
# Skip if all provided fields are already filled
if all(
getattr(track, field, None) is not None for field in enricher.provides
):
continue
result = enricher.enrich(track)
except Exception as e:
logger.warning(f"Enricher {enricher.name} failed: {e}")
+4
View File
@@ -20,6 +20,10 @@ class AudioTagEnricher(BaseEnricher):
def name(self) -> str:
return "audio-tag"
@property
def provides(self) -> set[str]:
return {"title", "artist", "album", "length"}
def enrich(self, track: TrackMeta) -> Optional[dict]:
if not track.is_local or not track.url:
return None
+4
View File
@@ -22,6 +22,10 @@ class BaseEnricher(ABC):
@abstractmethod
def name(self) -> str: ...
@property
@abstractmethod
def provides(self) -> set[str]: ...
@abstractmethod
def enrich(self, track: TrackMeta) -> Optional[dict]:
"""Return a dict of {field_name: value} for fields this enricher can fill.
+4
View File
@@ -33,6 +33,10 @@ class FileNameEnricher(BaseEnricher):
def name(self) -> str:
return "file-name"
@property
def provides(self) -> set[str]:
return {"artist", "title", "album"}
def enrich(self, track: TrackMeta) -> Optional[dict]:
if not track.is_local or not track.url:
return None