Refactor repo opening into the GitMIDI class

This commit is contained in:
Gergely Polonkai 2016-05-24 11:40:22 +02:00
parent 03c28e42e4
commit 07f51738cf

View File

@ -97,7 +97,12 @@ class GitMIDI(MIDIFile):
# TODO: Make this configurable # TODO: Make this configurable
self.addTempo(0, 0, 120) self.addTempo(0, 0, 120)
def __init__(self, tracks=None):
def __setup_repo(self):
repo = Repo(self.__repo_dir)
self.branch_head = repo.heads[self.__branch].commit
def __init__(self, tracks=None, repository='.', branch='master'):
if tracks is None: if tracks is None:
tracks = [("Sample Track", 120)] tracks = [("Sample Track", 120)]
@ -109,6 +114,12 @@ class GitMIDI(MIDIFile):
self.mem_file = StringIO() self.mem_file = StringIO()
self.__written = False self.__written = False
self.__repo_dir = repository
self.__repo = None
self.__branch = branch
self.branch_head = None
self.__setup_repo()
def write_mem(self): def write_mem(self):
self.writeFile(self.mem_file) self.writeFile(self.mem_file)
@ -147,15 +158,14 @@ if __name__ == '__main__':
args = parser.parse_args() args = parser.parse_args()
try: try:
repo = Repo(args.repository) repo_midi = GitMIDI(repository=args.repository, branch=args.branch)
except InvalidGitRepositoryError: except InvalidGitRepositoryError:
print("{} is not a valid Git repository".format( print("{} is not a valid Git repository".format(
os.path.abspath(args.repository))) os.path.abspath(args.repository)))
sys.exit(1) sys.exit(1)
try:
branch = repo.heads[args.branch]
except IndexError: except IndexError:
print("Branch '{}' does not exist in this repo".format(args.branch)) print("Branch '{}' does not exist in this repo".format(args.branch))
@ -166,7 +176,7 @@ orig_log = []
if args.verbose: if args.verbose:
print("Generating Git log…") print("Generating Git log…")
gen_history(orig_log, branch.commit) gen_history(orig_log, repo_midi.branch_head.commit)
if args.verbose: if args.verbose:
print("Sorting commits…") print("Sorting commits…")
@ -181,7 +191,6 @@ log = map(gen_note, orig_log)
if args.verbose: if args.verbose:
print("Creating MIDI…") print("Creating MIDI…")
MyMIDI = GitMIDI()
track = 0 track = 0
time = 0 time = 0
log_channel = 0 log_channel = 0
@ -190,32 +199,32 @@ decor_channel = 1
# Duration of one note # Duration of one note
duration = 0.3 duration = 0.3
MyMIDI.addProgramChange(track, log_channel, 0, 104) repo_midi.addProgramChange(track, log_channel, 0, 104)
MyMIDI.addProgramChange(track, decor_channel, 0, 115) repo_midi.addProgramChange(track, decor_channel, 0, 115)
# WRITE THE SEQUENCE # WRITE THE SEQUENCE
for section in log: for section in log:
section_len = len(section['file_notes']) * duration section_len = len(section['file_notes']) * duration
# Add a long note # Add a long note
MyMIDI.addNote(track, log_channel, repo_midi.addNote(track, log_channel,
section['commit_note'], time, section['commit_note'], time,
section_len, section['commit_volume']) section_len, section['commit_volume'])
for i, file_note in enumerate(section['file_notes']): for i, file_note in enumerate(section['file_notes']):
MyMIDI.addNote(track, decor_channel, repo_midi.addNote(track, decor_channel,
file_note['note'], time + i * duration, file_note['note'], time + i * duration,
duration, file_note['volume']) duration, file_note['volume'])
time += section_len time += section_len
MyMIDI.write_mem() repo_midi.write_mem()
if args.file: if args.file:
if args.verbose: if args.verbose:
print("Saving file to {}".format(args.file)) print("Saving file to {}".format(args.file))
MyMIDI.export_file(args.file) repo_midi.export_file(args.file)
if args.play: if args.play:
if args.verbose: if args.verbose:
@ -228,8 +237,8 @@ if args.play:
# PLAYBACK # PLAYBACK
pygame.init() pygame.init()
pygame.mixer.init() pygame.mixer.init()
MyMIDI.mem_file.seek(0) repo_midi.mem_file.seek(0)
pygame.mixer.music.load(MyMIDI.mem_file) pygame.mixer.music.load(repo_midi.mem_file)
pygame.mixer.music.play() pygame.mixer.music.play()
while pygame.mixer.music.get_busy(): while pygame.mixer.music.get_busy():