"""Track preprocessing.""" from switchboard_consumer.formatters.common import format_cline, format_pline def preprocess(track): """Preprocess track.""" preprocess_track_cline(track) preprocess_track_pline(track) track['publishers'] = preprocess_publishers(track.get('publishers')) def preprocess_track_cline(track): """Preprocess product track cLine.""" track['cLine'] = format_cline(track.get('cLine')) def preprocess_track_pline(track): """Preprocess product track pLine.""" track['pLine'] = format_pline(track.get('pLine')) def preprocess_publishers(publishers): """Preprocess publishers. Takes first publisher in the list if duplicates. """ if isinstance(publishers, list): deduped_publishers = {} for publisher in publishers: lowercase_publisher_name = publisher.get('name').lower() if lowercase_publisher_name not in deduped_publishers: deduped_publishers[lowercase_publisher_name] = publisher return list(deduped_publishers.values()) else: return publishers