本文整理汇总了Python中quodlibet.util.string.encode函数的典型用法代码示例。如果您正苦于以下问题:Python encode函数的具体用法?Python encode怎么用?Python encode使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了encode函数的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: website
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
示例2: to_dump
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)
示例3: to_dump
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)
示例4: to_dump
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)
示例5: copy
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
示例6: encode_key
def encode_key(k):
return encode(k) if isinstance(k, text_type) else k
示例7: test_unicode
def test_unicode(self):
self.failUnlessEqual(encode(u"abcde"), "abcde")
示例8: test_empty
def test_empty(self):
self.failUnlessEqual(encode(""), "")
示例9: encode_key
def encode_key(k):
return encode(k) if isinstance(k, unicode) else k
示例10: encode_key
def encode_key(k):
return encode(k) if isinstance(k, str) else k
示例11: copy
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