def plugin_album(self, songs): songs.sort(lambda a, b: cmp(a('~#track'), b('~#track')) or cmp(a('~basename'), b('~basename')) or cmp(a, b)) chooser = filechooser(save=True, title=songs[0]('album')) resp = chooser.run() fn = chooser.get_filename() chooser.destroy() if resp != Gtk.ResponseType.ACCEPT: return base, ext = splitext(fn) if not ext: fn = extsep.join([fn, 'tags']) global lastfolder lastfolder = dirname(fn) out = open(fn, 'w') for song in songs: print>>out, str(song('~basename')) keys = song.keys() keys.sort() for key in keys: if key.startswith('~'): continue for val in song.list(key): print>>out, '%s=%s' % (key, val.encode('utf-8')) print>>out
def compare_rating(a1, a2): if a1 is None: return -1 if a2 is None: return 1 if not a1.title: return 1 if not a2.title: return -1 return (-cmp(a1("~#rating"), a2("~#rating")) or cmpa(a1.date, a2.date) or cmpa(a1.sort, a2.sort) or cmp(a1.key, a2.key))
def compare_avgplaycount(a1, a2): a1, a2 = a1.album, a2.album if a1 is None: return -1 if a2 is None: return 1 if not a1.title: return 1 if not a2.title: return -1 return (-cmp(a1("~#playcount:avg"), a2("~#playcount:avg")) or cmpa(a1.date, a2.date) or cmpa(a1.sort, a2.sort) or cmp(a1.key, a2.key))
def cmp_rows(model, i1, i2, data): t1, t2 = model[i1][0], model[i2][0] pos1 = _ORDERING.get(t1, 0) pos2 = _ORDERING.get(t2, 0) if pos1 or pos2: return cmp(pos1, pos2) if not isinstance(t1, AlbumNode): return cmp(util.human_sort_key(t1), util.human_sort_key(t2)) a1, a2 = t1.album, t2.album return (cmpa(a1.peoplesort, a2.peoplesort) or cmpa(a1.date, a2.date) or cmpa(a1.sort, a2.sort) or cmp(a1.key, a2.key))
def cmp_rows(model, i1, i2, data): t1, t2 = model[i1][0], model[i2][0] pos1 = _ORDERING.get(t1, 0) pos2 = _ORDERING.get(t2, 0) if pos1 or pos2: return cmp(pos1, pos2) if not isinstance(t1, AlbumNode): return cmp(util.human_sort_key(t1), util.human_sort_key(t2)) a1, a2 = t1.album, t2.album return (cmp(a1.peoplesort and a1.peoplesort[0], a2.peoplesort and a2.peoplesort[0]) or cmp(a1.date or "ZZZZ", a2.date or "ZZZZ") or cmp((a1.sort, a1.key), (a2.sort, a2.key)))
def cmpa(a, b): """Like cmp but treats values that evaluate to false as inf""" if not a and b: return 1 if not b and a: return -1 return cmp(a, b)
def test__sort_on_value(self): m = ObjectStore() iterBob = m.append(row=["bob"]) iterAlice = m.append(row=["alice"]) m.append(row=["charlie"]) result = ObjectStore._sort_on_value(m, iterAlice, iterBob, None) self.assertEqual(result, cmp("alice", "bob"))
def sort(model, i1, i2, data): t1, t2 = model[i1][0], model[i2][0] if t1 is None or t2 is None: # FIXME: why? return 0 # FIXME: order this deterministically if t1 is MultiNode or t1 is UnknownNode or \ t2 is MultiNode or t2 is UnknownNode: return -cmp(t1, t2) if not isinstance(t1, AlbumNode): return cmp(util.human_sort_key(t1), util.human_sort_key(t2)) a1, a2 = t1.album, t2.album return (cmp(a1.peoplesort and a1.peoplesort[0], a2.peoplesort and a2.peoplesort[0]) or cmp(a1.date or "ZZZZ", a2.date or "ZZZZ") or cmp((a1.sort, a1.key), (a2.sort, a2.key)))
def compare_artist(a1, a2): if a1 is None: return -1 if a2 is None: return 1 if not a1.title: return 1 if not a2.title: return -1 return (cmpa(a1.peoplesort, a2.peoplesort) or cmpa(a1.date, a2.date) or cmpa(a1.sort, a2.sort) or cmp(a1.key, a2.key))
def compare_date(a1, a2): a1, a2 = a1.album, a2.album if a1 is None: return -1 if a2 is None: return 1 if not a1.title: return 1 if not a2.title: return -1 return (cmpa(a1.date, a2.date) or cmpa(a1.sort, a2.sort) or cmp(a1.key, a2.key))
def compare_title(a1, a2): # All albums should stay at the top if a1 is None: return -1 if a2 is None: return 1 # Move albums without a title to the bottom if not a1.title: return 1 if not a2.title: return -1 return (cmpa(a1.sort, a2.sort) or cmp(a1.key, a2.key))
def compare_date(a1, a2): if a1 is None: return -1 if a2 is None: return 1 if not a1.title: return 1 if not a2.title: return -1 return (cmpa(a1.date, a2.date) or cmpa(a1.sort, a2.sort) or cmp(a1.key, a2.key))
def compare_artist(a1, a2): a1, a2 = a1.album, a2.album if a1 is None: return -1 if a2 is None: return 1 if not a1.title: return 1 if not a2.title: return -1 return (cmpa(a1.peoplesort, a2.peoplesort) or cmpa(a1.date, a2.date) or cmpa(a1.sort, a2.sort) or cmp(a1.key, a2.key))
def sort_func(model, iter_a, iter_b, data): a = model.get_value(iter_a, 0) b = model.get_value(iter_b, 0) return -cmp(a, b)
def _sort_on_value(m, a, b, data): """Sorts two items in an ObjectStore, suitable for passing to `set_default_sort_func`""" return cmp(m[a][0], m[b][0])
def plugin_album(self, songs): songs.sort(lambda a, b: cmp(a('~#track'), b('~#track')) or cmp(a('~basename'), b('~basename')) or cmp(a, b)) chooser = filechooser(save=False, title=songs[0]('album')) box = Gtk.HBox() rename = Gtk.CheckButton("Rename Files") rename.set_active(False) box.pack_start(rename, True, True, 0) append = Gtk.CheckButton("Append Metadata") append.set_active(True) box.pack_start(append, True, True, 0) box.show_all() chooser.set_extra_widget(box) resp = chooser.run() append = append.get_active() rename = rename.get_active() fn = chooser.get_filename() chooser.destroy() if resp != Gtk.ResponseType.ACCEPT: return global lastfolder lastfolder = dirname(fn) metadata = [] names = [] index = 0 for line in open(fn, 'rU'): if index == len(metadata): names.append(line[:line.rfind('.')]) metadata.append({}) elif line == '\n': index = len(metadata) else: key, value = line[:-1].split('=', 1) value = value.decode('utf-8') try: metadata[index][key].append(value) except KeyError: metadata[index][key] = [value] if not (len(songs) == len(metadata) == len(names)): ErrorMessage(None, "Songs mismatch", "There are %(select)d songs selected, but %(meta)d " "songs in the file. Aborting." % dict(select=len(songs), meta=len(metadata))).run() return for song, meta, name in zip(songs, metadata, names): for key, values in iteritems(meta): if append and key in song: values = song.list(key) + values song[key] = '\n'.join(values) if rename: origname = song['~filename'] newname = name + origname[origname.rfind('.'):] app.library.rename(origname, newname)
def choose(r1, r2): if r1 or r2: return cmp(random.random(), r1 / (r1 + r2)) else: return random.randint(-1, 1)
def __cmp__(self, other): return (cmp(self._order, other._order) or cmp(type(self).__name__, type(other).__name__))
def sort_func(model, a, b, data): a = model.get_value(a) b = model.get_value(b) return cmp(a.name, b.name)