"""Module for generating DDEX PartyList nodes for v43.""" from typing import Dict, List from soundrecording_utils.constants.ddex import constants from soundrecording_utils.ddex.utils.control_chars import remove_control_chars from soundrecording_utils.metadata.types import Track, TrackParticipation from .e import E def generate_party_reference(participation: TrackParticipation) -> str: """Generate a PartyReference string for a given participation.""" return f'P-{participation.participant.uuid}' def _generate_party(participation: TrackParticipation): """Generate a Party node for a given participation.""" return E( 'Party', E('PartyReference', generate_party_reference(participation)), E( 'PartyName', E('FullName', remove_control_chars(participation.participant.name)), ), E( 'PartyId', E( 'ProprietaryId', { 'Namespace': constants.ORCHARD_PARTY_ID, }, participation.participant.uuid, ), ), ) def generate_party_list(track_metadata: Track): """Generate PartyList node.""" filtered_participations = [ participation for participation in track_metadata.participations if participation.participated_as in constants.ALL_PARTY_ROLES ] unique_participants: Dict[str, List[TrackParticipation]] = {} for participation in filtered_participations: participant_uuid = participation.participant.uuid if participant_uuid not in unique_participants: unique_participants[participant_uuid] = [] unique_participants[participant_uuid].append(participation) return [ _generate_party(participations[0]) for participations in unique_participants.values() ] def generate_the_orchard_party(): """Generate The Orchard Party node.""" return E( 'Party', E('PartyReference', constants.ORCHARD_PARTY_REFERENCE), E('PartyName', E('FullName', constants.ORCHARD_PARTY_NAME)), E('PartyId', E('DPID', constants.ORCHARD_PARTY_ID)), ) def generate_label_party(track_metadata: Track): """Generate the label Party node.""" label = track_metadata.product.label return E( 'Party', E('PartyReference', f'P-{label.uuid}'), E('PartyName', E('FullName', remove_control_chars(label.name))), E( 'PartyId', E( 'ProprietaryId', { 'Namespace': constants.ORCHARD_PARTY_ID, }, label.uuid, ), ), )