def init_table(self): """Generates a table with the song scores.""" # Prepare table for inserting items self.table.clearContents() self.table.setSortingEnabled(False) # Current table row current_row = 0 for song in self.stats.find("SongScores"): # Current table row current_column = 0 # Get the song's group and title # location[0] should always be "Songs" location = song.attrib['Dir'].split('/') # Create group cell group = QTableWidgetItem(location[1]) self.table.setItem(current_row, current_column, group) current_column = current_column + 1 # Create title cell title = QTableWidgetItem(location[2]) self.table.setItem(current_row, current_column, title) current_column = current_column + 1 # step_counter will be used for traversing the scores in a song step_counter = 0 for diff in self.difficulties: try: if (song[step_counter].attrib['Difficulty'] == diff and song[step_counter].attrib['StepsType'] == self.mode): try: grade = smformat.highscore_grade(song[step_counter], self.theme) percent = float(parse.highscore_stat(song[step_counter], "PercentDP")) * 100 if self.theme == "sm5" and self.icons_enabled is True: cell = QTableWidgetItem('{:.2f}%'.format(percent)) cell.setIcon(QIcon(smtracker.__path__[0] + '/images/' + grade + '.png')) else: cell = QTableWidgetItem('{} ({:.2f}%)'.format(grade, percent)) # Get the timings for our song timings = parse.highscore_timings(song[step_counter]) # TODO: Figure out if there's a cleaner way of # doing this tooltip = """Marvelous: {} Perfect: {} Great: {} Good: {} Boo: {} Miss: {} ----- Modifiers: {} ----- SN2 Score: {} DDRA Score: {} IIDX EX Score: {}""".format(timings['W1'], timings['W2'], timings['W3'], timings['W4'], timings['W5'], timings['Miss'], parse.highscore_stat(song[step_counter], "Modifiers"), parse.calculate_score_supernova2(song[step_counter]), parse.calculate_score_ddra(song[step_counter]), parse.calculate_score_iidx(song[step_counter])) cell.setToolTip(tooltip) self.table.setItem(current_row, current_column, cell) # This exception is reached if a Song was played, but # has no score (AutoPlay, PlayerAutoPlay) except AttributeError: cell = QTableWidgetItem() self.table.setItem(current_row, current_column, cell) step_counter = step_counter + 1 # If there are no scores for the current difficulty, # add an empty cell instead else: cell = QTableWidgetItem() self.table.setItem(current_row, current_column, cell) # This exception is reached if we already reached the last # score on a song (using step_counter) except IndexError: cell = QTableWidgetItem() self.table.setItem(current_row, current_column, cell) current_column = current_column + 1 current_row = current_row + 1 # Final table adjustments self.table.resizeColumnsToContents() self.table.setSortingEnabled(True) self.table.sortByColumn(0, Qt.AscendingOrder)
def generate(stats, mode, difficulties, theme): """Generates an HTML file with all the scores from the Stats file. Arguments: stats -- an pre-rooted ElementTree of the Stats.xml file (as in 'stats = etree.parse(statsxml).getroot() mode -- the game mode to output scores from difficulties -- the difficulties which should be printed theme -- which metrics should be used for printing grades """ # Get profile name from tree profile_name = parse.get_profile_name(stats) last_played = parse.get_last_played(stats) songs = [] song_tuple = namedtuple('Song', ['group', 'title', 'scores']) score_tuple = namedtuple('Score', ['diff', 'grade', 'perc', 'W1', 'W2', 'W3', 'W4', 'W5', 'Miss']) for song in stats.find("SongScores"): location = song.attrib['Dir'].split('/') group = location[1] title = location[2] scores = [] step_counter = 0 for diff in difficulties: try: if song[step_counter].attrib['Difficulty'] == diff and \ song[step_counter].attrib['StepsType'] == mode: try: grade = smformat.highscore_grade(song[step_counter], theme) percent = "{:.2f}%".format(float(parse.highscore_stat( song[step_counter], "PercentDP")) * 100) timings = parse.highscore_timings(song[step_counter]) scores.append(score_tuple( diff=diff, grade=grade, perc=percent, W1=timings['W1'], W2=timings['W2'], W3=timings['W3'], W4=timings['W4'], W5=timings['W5'], Miss=timings['Miss'])) except AttributeError: scores.append({'diff': diff}) step_counter = step_counter + 1 else: scores.append({'diff': diff}) except IndexError: scores.append({'diff': diff}) songs.append(song_tuple(group, title, scores)) env = Environment(loader=PackageLoader('smtracker', 'templates')) template = env.get_template('template.html') return(template.render(name=profile_name, last_played=last_played, difficulties=difficulties, songs=songs))