def website(self): """Look for a URL in the audio metadata, or a Google search if no URL can be found.""" if "website" in self: return self.list("website")[0] for cont in self.list("contact") + self.list("comment"): c = cont.lower() if (c.startswith("http://") or c.startswith("https://") or c.startswith("www.")): return cont elif c.startswith("//www."): return "http:" + cont else: text = "http://www.google.com/search?q=" esc = lambda c: ord(c) > 127 and '%%%x' % ord(c) or c if "labelid" in self: text += ''.join(map(esc, self["labelid"])) else: artist = util.escape("+".join(self("artist").split())) album = util.escape("+".join(self("album").split())) artist = encode(artist) album = encode(album) artist = "%22" + ''.join(map(esc, artist)) + "%22" album = "%22" + ''.join(map(esc, album)) + "%22" text += artist + "+" + album text += "&ie=UTF8" return text
def website(self): """Look for a URL in the audio metadata, or a Google search if no URL can be found.""" if "website" in self: return self.list("website")[0] for cont in self.list("contact") + self.list("comment"): c = cont.lower() if (c.startswith("http://") or c.startswith("https://") or c.startswith("www.")): return cont elif c.startswith("//www."): return "http:" + cont else: text = "https://www.google.com/search?q=" esc = lambda c: ord(c) > 127 and '%%%x' % ord(c) or c if "labelid" in self: text += ''.join(map(esc, self["labelid"])) else: artist = util.escape("+".join(self("artist").split())) album = util.escape("+".join(self("album").split())) artist = encode(artist) album = encode(album) artist = "%22" + ''.join(map(esc, artist)) + "%22" album = "%22" + ''.join(map(esc, album)) + "%22" text += artist + "+" + album text += "&ie=UTF8" return text
def to_dump(self): """A string of 'key=value' lines, similar to vorbiscomment output.""" def encode_key(k): return encode(k) if isinstance(k, text_type) else k s = [] for k in self.keys(): enc_key = encode_key(k) if isinstance(self[k], integer_types): s.append("%s=%d" % (enc_key, self[k])) elif isinstance(self[k], float): s.append("%s=%f" % (enc_key, self[k])) else: for v2 in self.list(k): if isinstance(v2, str): s.append("%s=%s" % (enc_key, v2)) else: s.append("%s=%s" % (enc_key, encode(v2))) for k in (NUMERIC_ZERO_DEFAULT - set(self.keys())): enc_key = encode_key(k) s.append("%s=%d" % (enc_key, self.get(k, 0))) if "~#rating" not in self: s.append("~#rating=%f" % self("~#rating")) s.append("~format=%s" % self.format) s.append("") return "\n".join(s)
def to_dump(self): """A string of 'key=value' lines, similar to vorbiscomment output.""" def encode_key(k): return encode(k) if isinstance(k, unicode) else k s = [] for k in self.keys(): enc_key = encode_key(k) if isinstance(self[k], int) or isinstance(self[k], long): s.append("%s=%d" % (enc_key, self[k])) elif isinstance(self[k], float): s.append("%s=%f" % (enc_key, self[k])) else: for v2 in self.list(k): if isinstance(v2, str): s.append("%s=%s" % (enc_key, v2)) else: s.append("%s=%s" % (enc_key, encode(v2))) for k in (INTERN_NUM_DEFAULT - set(self.keys())): enc_key = encode_key(k) s.append("%s=%d" % (enc_key, self.get(k, 0))) if "~#rating" not in self: s.append("~#rating=%f" % self("~#rating")) s.append("~format=%s" % self.format) s.append("") return "\n".join(s)
def to_dump(self): # dump without title title = None if "title" in self: title = self["title"] del self["title"] dump = super(IRFile, self).to_dump() if title is not None: self["title"] = title # add all generated tags lines = dump.splitlines() for tag in ["title", "artist", "~format"]: value = self.get(tag) if value is not None: lines.append(encode(tag) + b"=" + encode(value)) return b"\n".join(lines)
def fsencode(s, note=False): """Encode a string according to the filesystem encoding. note specifies whether a note should be appended if encoding failed.""" if isinstance(s, str): return s elif note: return encode(s, fscoding) else: return s.encode(fscoding, 'replace')
def to_dump(self): """A string of 'key=value' lines, similar to vorbiscomment output. Returns: bytes """ def encode_key(k): return encode(k) if isinstance(k, text_type) else k s = [] for k in self.keys(): enc_key = encode_key(k) assert isinstance(enc_key, bytes) if isinstance(self[k], integer_types): l = enc_key + encode("=%d" % self[k]) s.append(l) elif isinstance(self[k], float): l = enc_key + encode("=%f" % self[k]) s.append(l) else: for v2 in self.list(k): if not isinstance(v2, bytes): v2 = encode(v2) s.append(enc_key + b"=" + v2) for k in (NUMERIC_ZERO_DEFAULT - set(self.keys())): enc_key = encode_key(k) l = enc_key + encode("=%d" % self.get(k, 0)) s.append(l) if "~#rating" not in self: s.append(encode("~#rating=%f" % self("~#rating"))) s.append(encode("~format=%s" % self.format)) s.append(b"") return b"\n".join(s)
def to_dump(self): """A string of 'key=value' lines, similar to vorbiscomment output. Returns: bytes """ def encode_key(k): return encode(k) if isinstance(k, str) else k s = [] for k in self.keys(): enc_key = encode_key(k) assert isinstance(enc_key, bytes) if isinstance(self[k], int): l = enc_key + encode("=%d" % self[k]) s.append(l) elif isinstance(self[k], float): l = enc_key + encode("=%f" % self[k]) s.append(l) else: for v2 in self.list(k): if not isinstance(v2, bytes): v2 = encode(v2) s.append(enc_key + b"=" + v2) for k in (NUMERIC_ZERO_DEFAULT - set(self.keys())): enc_key = encode_key(k) l = enc_key + encode("=%d" % self.get(k, 0)) s.append(l) if "~#rating" not in self: s.append(encode("~#rating=%f" % self("~#rating"))) s.append(encode("~format=%s" % self.format)) s.append(b"") return b"\n".join(s)
def to_dump(self): """A string of 'key=value' lines, similar to vorbiscomment output.""" s = [] for k in self.keys(): k = str(k) if isinstance(self[k], int) or isinstance(self[k], long): s.append("%s=%d" % (k, self[k])) elif isinstance(self[k], float): s.append("%s=%f" % (k, self[k])) else: for v2 in self.list(k): if isinstance(v2, str): s.append("%s=%s" % (k, v2)) else: s.append("%s=%s" % (k, encode(v2))) for k in (INTERN_NUM_DEFAULT - set(self.keys())): s.append("%s=%d" % (k, self.get(k, 0))) if "~#rating" not in self: s.append("~#rating=%f" % self("~#rating")) s.append("~format=%s" % self.format) s.append("") return "\n".join(s)
def encode_key(k): return encode(k) if isinstance(k, text_type) else k
def encode_key(k): return encode(k) if isinstance(k, unicode) else k
def encode_key(k): return encode(k) if isinstance(k, str) else k
def copy(self, songlist, song): if self.__load_db() is None: return False track = gpod.itdb_track_new() # All values should be utf-8 encoded strings # Filepaths should be encoded with the fs encoding # Either combine tags with comma, or only take the first value if self['all_tags']: tag = song.comma else: tag = lambda key: (song.list(key) or ('', ))[0] title = tag('title') if self['title_version'] and song('version'): title = " - ".join([title, song('version')]) track.title = encode(title) album = tag('album') if self['album_part'] and song('discsubtitle'): album = " - ".join([album, song('discsubtitle')]) track.album = encode(album) # String keys for key in ['artist', 'genre', 'grouping', 'composer', 'albumartist']: if hasattr(track, key): # albumartist since libgpod-0.4.2 setattr(track, key, encode(tag(key))) # Sort keys (since libgpod-0.5.0) for key in ['artist', 'album', 'albumartist']: if hasattr(track, 'sort_' + key): setattr(track, 'sort_' + key, encode(tag(key + 'sort'))) # Numeric keys for key in ['bitrate', 'playcount', 'year']: try: setattr(track, key, int(song('~#' + key))) except ValueError: continue # Numeric keys where the names differ for key, value in { 'cd_nr': song('~#disc'), 'cds': song('~#discs'), 'rating': min(100, song('~#rating') * 100), 'time_added': self.__mactime(time.time()), 'time_modified': self.__mactime(mtime(song('~filename'))), 'track_nr': song('~#track'), 'tracklen': song('~#length') * 1000, 'tracks': song('~#tracks'), 'size': filesize(song('~filename')), 'soundcheck': self.__soundcheck(song), }.items(): try: setattr(track, key, int(value)) except ValueError: continue track.filetype = song('~format') track.comment = encode(fsdecode(song('~filename'))) # Associate a cover with the track if self['covers']: cover = song.find_cover() if cover: # libgpod will copy the file later when the iTunesDB # is saved, so we have to keep a reference around in # case the cover is a temporary file. self.__covers.append(cover) gpod.itdb_track_set_thumbnails(track, fsencode(cover.name)) # Add the track to the master playlist gpod.itdb_track_add(self.__itdb, track, -1) master = gpod.itdb_playlist_mpl(self.__itdb) gpod.itdb_playlist_add_track(master, track, -1) # Copy the actual file if gpod.itdb_cp_track_to_ipod(track, song['~filename'], None) == 1: return IPodSong(track) else: return False
def test_empty(self): self.failUnlessEqual(encode(""), "")
def copy(self, parent_widget, song): if self.__load_db() is None: return False track = gpod.itdb_track_new() # All values should be utf-8 encoded strings # Filepaths should be encoded with the fs encoding # Either combine tags with comma, or only take the first value if self['all_tags']: tag = song.comma else: tag = lambda key: (song.list(key) or ('',))[0] title = tag('title') if self['title_version'] and song('version'): title = " - ".join([title, song('version')]) track.title = encode(title) album = tag('album') if self['album_part'] and song('discsubtitle'): album = " - ".join([album, song('discsubtitle')]) track.album = encode(album) # String keys for key in ['artist', 'genre', 'grouping', 'composer', 'albumartist']: if hasattr(track, key): # albumartist since libgpod-0.4.2 setattr(track, key, encode(tag(key))) # Sort keys (since libgpod-0.5.0) for key in ['artist', 'album', 'albumartist']: if hasattr(track, 'sort_' + key): setattr(track, 'sort_' + key, encode(tag(key + 'sort'))) # Numeric keys for key in ['bitrate', 'playcount', 'year']: try: setattr(track, key, int(song('~#' + key))) except ValueError: continue # Numeric keys where the names differ for key, value in { 'cd_nr': song('~#disc'), 'cds': song('~#discs'), 'rating': min(100, song('~#rating') * 100), 'time_added': self.__mactime(time.time()), 'time_modified': self.__mactime(mtime(song('~filename'))), 'track_nr': song('~#track'), 'tracklen': song('~#length') * 1000, 'tracks': song('~#tracks'), 'size': filesize(song('~filename')), 'soundcheck': self.__soundcheck(song), }.items(): try: setattr(track, key, int(value)) except ValueError: continue track.filetype = encode(song('~format')) track.comment = encode(fsdecode(song('~filename'))) # Associate a cover with the track if self['covers']: cover = app.cover_manager.get_cover(song) if cover: # libgpod will copy the file later when the iTunesDB # is saved, so we have to keep a reference around in # case the cover is a temporary file. self.__covers.append(cover) gpod.itdb_track_set_thumbnails( track, fsnative2glib(cover.name)) # Add the track to the master playlist gpod.itdb_track_add(self.__itdb, track, -1) master = gpod.itdb_playlist_mpl(self.__itdb) gpod.itdb_playlist_add_track(master, track, -1) # Copy the actual file if gpod.itdb_cp_track_to_ipod(track, song['~filename'], None) == 1: return IPodSong(track) else: return False
def test_unicode(self): self.failUnlessEqual(encode(u"abcde"), "abcde")
def copy(self, parent_widget, song): if self.__load_db() is None: return False track = gpod.itdb_track_new() # All values should be utf-8 encoded strings # Filepaths should be encoded with the fs encoding # Either combine tags with comma, or only take the first value if self["all_tags"]: tag = song.comma else: tag = lambda key: (song.list(key) or ("",))[0] title = tag("title") if self["title_version"] and song("version"): title = " - ".join([title, song("version")]) track.title = encode(title) album = tag("album") if self["album_part"] and song("discsubtitle"): album = " - ".join([album, song("discsubtitle")]) track.album = encode(album) # String keys for key in ["artist", "genre", "grouping", "composer", "albumartist"]: if hasattr(track, key): # albumartist since libgpod-0.4.2 setattr(track, key, encode(tag(key))) # Sort keys (since libgpod-0.5.0) for key in ["artist", "album", "albumartist"]: if hasattr(track, "sort_" + key): setattr(track, "sort_" + key, encode(tag(key + "sort"))) # Numeric keys for key in ["bitrate", "playcount", "year"]: try: setattr(track, key, int(song("~#" + key))) except ValueError: continue # Numeric keys where the names differ for key, value in { "cd_nr": song("~#disc"), "cds": song("~#discs"), "rating": min(100, song("~#rating") * 100), "time_added": self.__mactime(time.time()), "time_modified": self.__mactime(mtime(song("~filename"))), "track_nr": song("~#track"), "tracklen": song("~#length") * 1000, "tracks": song("~#tracks"), "size": filesize(song("~filename")), "soundcheck": self.__soundcheck(song), }.items(): try: setattr(track, key, int(value)) except ValueError: continue track.filetype = encode(song("~format")) track.comment = encode(fsdecode(song("~filename"))) # Associate a cover with the track if self["covers"]: cover = app.cover_manager.get_cover(song) if cover: # libgpod will copy the file later when the iTunesDB # is saved, so we have to keep a reference around in # case the cover is a temporary file. self.__covers.append(cover) gpod.itdb_track_set_thumbnails(track, fsnative2glib(cover.name)) # Add the track to the master playlist gpod.itdb_track_add(self.__itdb, track, -1) master = gpod.itdb_playlist_mpl(self.__itdb) gpod.itdb_playlist_add_track(master, track, -1) # Copy the actual file if gpod.itdb_cp_track_to_ipod(track, song["~filename"], None) == 1: return IPodSong(track) else: return False