'Pre-process a tracklist, merging subtracks into a single track. The
title for the merged track is the one from the previous index track,
if present; otherwise it is a combination of the subtracks titles.'

def coalesce_tracks(self, raw_tracklist):

    def add_merged_subtracks(tracklist, subtracks):
        'Modify    `tracklist`    in    place,    merging    a    list    of    `subtracks`    into\n                                                a    single    track    into    `tracklist`.'
        (idx, medium_idx, sub_idx) = self.get_track_index(subtracks[0]['position'])
        position = ('%s%s' % ((idx or ''), (medium_idx or '')))
        if (tracklist and (not tracklist[(-1)]['position'])):
            if sub_idx:
                tracklist[(-1)]['position'] = position
            else:
                index_track = tracklist.pop()
                if index_track.get('artists'):
                    for subtrack in subtracks:
                        if (not subtrack.get('artists')):
                            subtrack['artists'] = index_track['artists']
                tracklist.extend(subtracks)
        else:
            track = subtracks[0].copy()
            track['title'] = '    /    '.join([t['title'] for t in subtracks])
            tracklist.append(track)
    subtracks = []
    tracklist = []
    prev_subindex = ''
    for track in raw_tracklist:
        if track['position']:
            (_, _, subindex) = self.get_track_index(track['position'])
            if subindex:
                if (subindex.rjust(len(raw_tracklist)) > prev_subindex):
                    subtracks.append(track)
                else:
                    add_merged_subtracks(tracklist, subtracks)
                    subtracks = [track]
                prev_subindex = subindex.rjust(len(raw_tracklist))
                continue
        if ((not track['position']) and ('sub_tracks' in track)):
            tracklist.append(track)
            add_merged_subtracks(tracklist, track['sub_tracks'])
            continue
        if subtracks:
            add_merged_subtracks(tracklist, subtracks)
            subtracks = []
            prev_subindex = ''
        tracklist.append(track)
    if subtracks:
        add_merged_subtracks(tracklist, subtracks)
    return tracklist
