refactor: skip a enricher if fields it can provide are already filled
This commit is contained in:
@@ -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}")
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user