"""Tools for interacting with CSV exports of the library from Myriad Playout."""fromcsvimportDictReaderfromdataclassesimportdataclassfromtypingimportCallable,Iterable,Optionalfrom.modelsimportTrack
[docs]defhas_hook(entry:dict[str,str])->bool:matchentry['HasHook']:case'YES':returnTruecase'':returnFalseraiseValueError(f'could not recognise HasHook entry {entry["HasHook"]!r}')
[docs]defmatched_track(self)->Optional[Track]:""" Return the :class:`.Track` that matches this entry, if applicable. """defpass_if_none_exists(get_track=Callable[[],Track])->Optional[Track]:try:returnget_track()except(Track.DoesNotExist,Track.MultipleObjectsReturned):returnNonereturn(pass_if_none_exists(lambda:Track.objects.get(media_id=self.media_id))orpass_if_none_exists(lambda:Track.objects.get(revealed__isnull=False,hidden=False,id3_title=self.title,id3_artist__startswith=self.artists,))orNone)
[docs]defupdate_matched_track(self)->bool:""" Update and save the :class:`.Track` we're able to match against this entry. :returns: :data:`True` if we matched against something and updated it, otherwise :data:`False`. """matched_track=self.matched_track()ifmatched_trackisNone:returnFalsematched_track.media_id=self.media_idmatched_track.save()returnTrue