#!/usr/bin/python import re from sys import stdout, stdin def process_chunk(array): split_array = [] non_note_positions = [] event_lengths = [] for string in array: split_array.append(string.split()) for line in split_array: ####### collect information if re.match(r"[0-9]+\:", line[0]): for idx in range(len(line)): if re.match(r"(?:(?:[.])?(?:[0-9][.])?(?:[-]?[0-9]+))|t(?!mp)|[zr]", line[idx]) is None: if idx not in non_note_positions: non_note_positions.append(idx) for line_index in range(len(split_array)): ######### use it now to change things, insert whitespace if re.match("[0-9]+\:", split_array[line_index][0]): for word_index in non_note_positions: if word_index < len(split_array[line_index]): #### don't go beyond the length of our test line if re.match(r"(?:(?:[.])?(?:[0-9][.])?(?:[-]?[0-9]+))|t(?!mp)|[zr]", split_array[line_index][word_index]): split_array[line_index].insert(word_index,'') for line in split_array: ##### collect rjustify info if re.match(r"[0-9]+\:", line[0]): for idx in range(len(line)): if len(event_lengths) < (idx + 1): event_lengths.append(len(line[idx])) else: if len(line[idx]) > event_lengths[idx]: event_lengths[idx] = len(line[idx]) for line_index in range(len(split_array)): ######### use it now to change things, insert whitespace if re.match("[0-9]+\:", split_array[line_index][0]): for word_index in range(1,len(split_array[line_index]),1): split_array[line_index][word_index] = split_array[line_index][word_index].rjust(event_lengths[word_index]) for line in split_array: stdout.write("%s\n" % ' '.join(line)) filetext = stdin.read() voiceline = 1 current_voice = "%i:" % voiceline while re.search(current_voice, filetext): voiceline += 1 current_voice = "%i:" % voiceline voiceline -= 1 voice_max = "%i\:" % voiceline project = [] for line in filetext.splitlines(): if re.match(voice_max, line): project.append(line) process_chunk(project) project = [] else: project.append(line)