class ItemLeaf(object): def __init__(self, maintext, url, subtext=""): self.maintext = maintext self.subtext = subtext self.url = url self.parent = None self.surface = None self.art = None def __str__(self): return self.maintext def __unicode__(self): return self.maintext def __repr__(self): return str(self) class ItemList(object): def __init__(self, name, serve_sorted=False, has_all_option=True): self.list = [] self.name = name self.parent = None self.surface = None self.serve_sorted = serve_sorted self.current_item = 0 self.art = None self.has_all_option = has_all_option if has_all_option: all_option = ItemLeaf("Play All", "playall://ordered") self.list.append(all_option) rnd_option = ItemLeaf("Shuffle All", "playall://random") self.list.append(rnd_option) def __getitem__(self, idx): return self.list[idx] def append(self, item): if not isinstance(item, ItemList) and not isinstance(item, ItemLeaf): raise "You can't put random shit in an ItemList" item.parent = self self.list.append(item) if self.serve_sorted: playall_options = [x for x in self.list if hasattr(x, "url") and x.url.startswith("playall://")] self.list = [x for x in self.list if not hasattr(x, "url") or not x.url.startswith("playall://")] self.list.sort(lambda a,b: cmp(unicode(a).lower(), unicode(b).lower())) self.list = playall_options + self.list def __str__(self): return self.name def __len__(self): return len(self.list) def __unicode__(self): return self.name def __repr__(self): return str(self) def dump(l, prefix=""): if hasattr(l, "url"): print " %s%s (leaf)" % (prefix, l) else: print "%s%s (parent %s)" % (prefix, l, l.parent) for sub in l: dump(sub, prefix + " ")