def build_music_pages_ao1(self): self.music_pages_ao1 = [] index = 0 # add areas first for area in self.area_manager.areas: self.music_pages_ao1.append('{}#{}'.format(index, area.name)) index += 1 # then add music try: for item in self.music_list: self.music_pages_ao1.append('{}#{}'.format(index, item['category'])) index += 1 for song in item['songs']: self.music_pages_ao1.append('{}#{}'.format(index, song['name'])) index += 1 except KeyError as err: msg = ("The music list expected key '{}' for item {}, but could not find it." .format(err.args[0], item)) raise ServerError.MusicInvalid(msg) except TypeError: msg = ("The music list expected songs to be listed for item {}, but could not find any." .format(item)) raise ServerError.MusicInvalid(msg) self.music_pages_ao1 = [self.music_pages_ao1[x:x + 10] for x in range(0, len(self.music_pages_ao1), 10)]
def prepare_music_list(self, c=None, specific_music_list=None): """ If `specific_music_list` is not None, return a client-ready version of that music list. Else, if `c` is a client with a custom chosen music list, return their latest music list. Otherwise, return a client-ready version of the server music list. Parameters ---------- c: ClientManager.Client Client whose current music list if it exists will be considered if `specific_music_list` is None specific_music_list: list of dictionaries with key sets {'category', 'songs'} Music list to use if given Returns ------- list of str Music list ready to be sent to clients """ # If not provided a specific music list to overwrite if specific_music_list is None: specific_music_list = self.music_list # Default value # But just in case, check if this came as a request of a client who had a # previous music list preference if c and c.music_list is not None: specific_music_list = c.music_list prepared_music_list = list() try: for item in specific_music_list: prepared_music_list.append(item['category']) for song in item['songs']: if 'length' not in song: name, length = song['name'], -1 else: name, length = song['name'], song['length'] # Check that length is a number, and if not, abort. if not isinstance(length, (int, float)): msg = ( "The music list expected a numerical length for track '{}', but " "found it had length '{}'.").format( song['name'], song['length']) raise ServerError.MusicInvalidError(msg) prepared_music_list.append(name) except KeyError as err: msg = ( "The music list expected key '{}' for item {}, but could not find it." .format(err.args[0], item)) raise ServerError.MusicInvalid(msg) except TypeError: msg = ( "The music list expected songs to be listed for item {}, but could not find any." .format(item)) raise ServerError.MusicInvalid(msg) return prepared_music_list