Add callbacks to generator functions

These will be capable to update the GUI during data generating
This commit is contained in:
Gergely Polonkai 2016-06-09 17:35:26 +02:00
parent ffd3f616d1
commit 1b712dcd26

View File

@ -191,12 +191,15 @@ class GitMIDI(MIDIFile):
return self.__scale[note_num] return self.__scale[note_num]
def gen_beat(self, commit): def gen_beat(self, commit, callback=None):
stat = commit.stats stat = commit.stats
file_notes = [] file_notes = []
for file_name, file_stat in stat.files.items(): for file_name, file_stat in stat.files.items():
if callback is not None:
callback(max_count=None, current=None)
volume_mod = self.__program['file'].get('volume', 0) volume_mod = self.__program['file'].get('volume', 0)
file_notes.append({ file_notes.append({
'note': self.sha_to_note(get_file_sha(commit, file_name)) + 'note': self.sha_to_note(get_file_sha(commit, file_name)) +
@ -206,7 +209,11 @@ class GitMIDI(MIDIFile):
volume_mod), volume_mod),
}) })
if callback is not None:
callback(max_count=None, current=None)
volume_mod = self.__program['commit'].get('volume', 0) volume_mod = self.__program['commit'].get('volume', 0)
return { return {
'commit_note': self.sha_to_note(commit.hexsha) + 'commit_note': self.sha_to_note(commit.hexsha) +
self.__program['commit']['octave'] * 12, self.__program['commit']['octave'] * 12,
@ -216,7 +223,7 @@ class GitMIDI(MIDIFile):
'file_notes': file_notes, 'file_notes': file_notes,
} }
def gen_repo_data(self, force=False): def gen_repo_data(self, force=False, callback=None):
""" """
Populate __repo_data with the Git history data. If force is Populate __repo_data with the Git history data. If force is
False and the repo_data is already calculated, we do not do False and the repo_data is already calculated, we do not do
@ -242,6 +249,9 @@ class GitMIDI(MIDIFile):
commit = to_process.pop() commit = to_process.pop()
if callback is not None:
callback(max_count=None, current=None)
if not commit in self.__repo_data: if not commit in self.__repo_data:
self.__repo_data.append(commit) self.__repo_data.append(commit)
to_process += commit.parents to_process += commit.parents
@ -255,8 +265,11 @@ class GitMIDI(MIDIFile):
if self.__verbose: if self.__verbose:
print("Generating MIDI data…") print("Generating MIDI data…")
self.git_log = map(lambda commit: self.gen_beat(commit), self.git_log = map(
self.__repo_data[self.__skip:]) lambda commit: self.gen_beat(
commit,
callback=callback),
self.__repo_data[self.__skip:])
@property @property
def repo_data(self): def repo_data(self):
@ -277,7 +290,7 @@ class GitMIDI(MIDIFile):
self.mem_file.seek(0) self.mem_file.seek(0)
shutil.copyfileobj(self.mem_file, f) shutil.copyfileobj(self.mem_file, f)
def generate_midi(self): def generate_midi(self, callback=None):
if self.__verbose: if self.__verbose:
print("Creating MIDI…") print("Creating MIDI…")
@ -289,10 +302,17 @@ class GitMIDI(MIDIFile):
# Duration of one note # Duration of one note
duration = 0.3 duration = 0.3
log_length = len(self.git_log)
current = 0
# WRITE THE SEQUENCE # WRITE THE SEQUENCE
for section in self.git_log: for section in self.git_log:
current += 1
section_len = len(section['file_notes']) * duration section_len = len(section['file_notes']) * duration
if callback is not None:
callback(max_count=log_length, current=current)
# Add a long note # Add a long note
if self.__need_commits: if self.__need_commits:
self.addNote(track, log_channel, self.addNote(track, log_channel,