def __set_bookmarks(self, marks): result = [] for time, mark in marks: if time < 0: raise ValueError("mark times must be positive") result.append(u"%s %s" % (util.format_time(time), mark)) result = u"\n".join(result) if result: self["~bookmark"] = result elif "~bookmark" in self: del(self["~bookmark"])
def __call__(self, key, default=u"", connector=" - "): """Return a key, synthesizing it if necessary. A default value may be given (like dict.get); the default default is an empty unicode string (even if the tag is numeric). If a tied tag ('a~b') is requested, the 'connector' keyword argument may be used to specify what it is tied with. For details on tied tags, see the documentation for util.tagsplit.""" if key[:1] == "~": key = key[1:] if "~" in key: if not isinstance(default, basestring): return default return connector.join( filter(None, map(lambda x: isinstance(x, basestring) and x or str(x), map(lambda x: (isinstance(x, float) and "%.2f" % x) or x, map(self.__call__, util.tagsplit("~" + key)))))) or default elif key == "#track": try: return int(self["tracknumber"].split("/")[0]) except (ValueError, TypeError, KeyError): return default elif key == "#disc": try: return int(self["discnumber"].split("/")[0]) except (ValueError, TypeError, KeyError): return default elif key == "length": length = self.get("~#length") if length is None: return default else: return util.format_time(length) elif key == "#rating": return dict.get(self, "~" + key, const.DEFAULT_RATING) elif key == "rating": return util.format_rating(self("~#rating")) elif key == "people": join = "\n".join people = filter(None, map(self.__call__, PEOPLE)) if not people: return default people = join(people).split("\n") index = people.index return join([person for (i, person) in enumerate(people) if index(person) == i]) elif key == "peoplesort": join = "\n".join people = filter(None, map(self.__call__, PEOPLE_SORT)) people = join(people).split("\n") index = people.index return (join([person for (i, person) in enumerate(people) if index(person) == i]) or self("~people", default, connector)) elif key == "performers" or key == "performer": performers = {} for key in self.keys(): if key.startswith("performer:"): role = key.split(":", 1)[1] for value in self.list(key): try: performers[str(value)] except: performers[str(value)] = [] performers[str(value)].append(util.title(role)) values = [] if len(performers) > 0: for performer in performers: roles = '' i = 0 for role in performers[performer]: if i > 0: roles += ', ' roles += role i += 1 values.append("%s (%s)" % (performer, roles)) values.extend(self.list("performer")) if not values: return default return "\n".join(values) elif key == "performerssort" or key == "performersort": values = [] for key in self.keys(): if key.startswith("performersort:"): role = key.split(":", 1)[1] for value in self.list(key): values.append("%s (%s)" % (value, role)) values.extend(self.list("performersort")) return ("\n".join(values) or self("~performers", default, connector)) elif key == "basename": return os.path.basename(self["~filename"]) or self["~filename"] elif key == "dirname": return os.path.dirname(self["~filename"]) or self["~filename"] elif key == "uri": try: return self["~uri"] except KeyError: return URI.frompath(util.fsdecode(self["~filename"])) elif key == "format": return self.get("~format", self.format) elif key == "year": return self.get("date", default)[:4] elif key == "#year": try: return int(self.get("date", default)[:4]) except (ValueError, TypeError, KeyError): return default elif key == "#tracks": try: return int(self["tracknumber"].split("/")[1]) except (ValueError, IndexError, TypeError, KeyError): return default elif key == "#discs": try: return int(self["discnumber"].split("/")[1]) except (ValueError, IndexError, TypeError, KeyError): return default elif key == "lyrics": try: fileobj = file(self.lyric_filename, "rU") except EnvironmentError: return default else: return fileobj.read().decode("utf-8", "replace") elif key[:1] == "#": key = "~" + key if key in self: self[key] elif key in INTERN_NUM_DEFAULT: return dict.get(self, key, 0) else: try: val = self[key[2:]] except KeyError: return default try: return int(val) except ValueError: try: return float(val) except ValueError: return default else: return dict.get(self, "~" + key, default) elif key == "title": title = dict.get(self, "title") if title is None: basename = self("~basename") basename = basename.decode(const.FSCODING, "replace") return "%s [%s]" % (basename, _("Unknown")) else: return title elif key in SORT_TO_TAG: try: return self[key] except KeyError: key = SORT_TO_TAG[key] return dict.get(self, key, default)