def test_to_dump_long(self): if not PY2: return b = AudioFile(bar_1_1) b["~#length"] = long(200000000000) dump = b.to_dump() num = len(set(bar_1_1.keys()) | NUMERIC_ZERO_DEFAULT) self.failUnlessEqual(dump.count("\n"), num + 2, msg=dump) n = AudioFile() n.from_dump(dump) self.failUnless(set(dump.split("\n")) == set(n.to_dump().split("\n")))
def test_numeric_comma(self): songs = [Fakesong({ "~#added": long(1), "~#rating": 0.5, "~#bitrate": 42, "~#length": 1, })] album = Album(songs[0]) album.songs = set(songs) self.assertEqual(album.comma("~#added"), 1) self.assertEqual(album.comma("~#rating"), 0.5) self.assertEqual(album.comma("~#bitrate"), 42)
def test_parse(self): # gst.TagList can't be filled using pyGtk... so use a dict instead l = {} l["extended-comment"] = u"foo=bar" self.failUnless("foo" in parse_gstreamer_taglist(l)) l["extended-comment"] = [u"foo=bar", u"bar=foo", u"bar=foo2"] self.failUnless("foo" in parse_gstreamer_taglist(l)) self.failUnless("bar" in parse_gstreamer_taglist(l)) self.failUnlessEqual(parse_gstreamer_taglist(l)["bar"], "foo\nfoo2") # date is abstract, so define our own # (might work with pygobject now) class Foo(object): def to_iso8601_string(self): return "3000-10-2" l["date"] = Foo() date = Gst.DateTime Gst.DateTime = Foo self.failUnlessEqual(parse_gstreamer_taglist(l)["date"], "3000-10-2") Gst.DateTime = date l["foo"] = u"äöü" parsed = parse_gstreamer_taglist(l) self.assertTrue(isinstance(parsed["foo"], text_type)) self.assertTrue(u"äöü" in parsed["foo"].split("\n")) l["foo"] = u"äöü".encode("utf-8") parsed = parse_gstreamer_taglist(l) self.assertTrue(isinstance(parsed["foo"], text_type)) self.assertTrue(u"äöü" in parsed["foo"].split("\n")) l["bar"] = 1.2 self.failUnlessEqual(parse_gstreamer_taglist(l)["bar"], 1.2) l["bar"] = long(9) self.failUnlessEqual(parse_gstreamer_taglist(l)["bar"], 9) l["bar"] = 9 self.failUnlessEqual(parse_gstreamer_taglist(l)["bar"], 9) l["bar"] = Gst.TagList() # some random gst instance self.failUnless( isinstance(parse_gstreamer_taglist(l)["bar"], text_type)) self.failUnless("GstTagList" in parse_gstreamer_taglist(l)["bar"])
def test_sanitize_py2_normal(self): if PY3: return af = AudioFile({ b"foo": u"bar", u"öäü": u"bla", "~#num": 1, "~#num2": long(2), "~#num3": 1.25, "~filename": fsnative(u"filename"), "~mountpoint": fsnative(u"mount"), "~somethingdifferent": u"hello", }) data = dump_audio_files([af]) new = load_audio_files(data) assert dict(new[0]) == dict(af)
def sanitize_tags(tags, stream=False): """Returns a new sanitized tag dict. stream defines if the tags of a main/base song should be changed or of a stream song. e.g. title will be removed for the base song but not for the stream one. """ san = {} for key, value in iteritems(tags): key = key.lower() key = {"location": "website"}.get(key, key) if isinstance(value, text_type): lower = value.lower().strip() if key == "channel-mode": if "stereo" in lower or "dual" in lower: value = u"stereo" elif key == "audio-codec": if "mp3" in lower: value = u"MP3" elif "aac" in lower or "advanced" in lower: value = u"MPEG-4 AAC" elif "vorbis" in lower: value = u"Ogg Vorbis" if lower in ("http://www.shoutcast.com", "http://localhost/", "default genre", "none", "http://", "unnamed server", "unspecified", "n/a"): continue if key == "duration": try: value = int(long(value) / 1000) except ValueError: pass else: if not stream: continue key = "~#length" elif key == "bitrate": try: value = int(value) / 1000 except ValueError: pass else: if not stream: continue key = "~#bitrate" elif key == "nominal-bitrate": try: value = int(value) / 1000 except ValueError: pass else: if stream: continue key = "~#bitrate" if key in ("emphasis", "mode", "layer", "maximum-bitrate", "minimum-bitrate", "has-crc", "homepage"): continue if not stream and key in ("title", "album", "artist", "date"): continue if isinstance(value, number_types): if not key.startswith("~#"): key = "~#" + key san[key] = value else: if key.startswith("~#"): key = key[2:] if not isinstance(value, text_type): continue value = value.strip() if key in san: if value not in san[key].split("\n"): san[key] += "\n" + value else: san[key] = value return san
def test_huge_playcount(self): count = long(1000000000000000) self.song["~#playcount"] = count self.song.write() song = type(self.song)(self.filename) self.failUnlessEqual(song["~#playcount"], count)