def get_bpm(cmd_path):
    cmd, path = cmd_path
    TAG = "bpm"
    try:
        bpm = check_output(cmd, shell=True)
        if bpm:
            song = formats.MusicFile(path)
            try:
                assert song.can_change(TAG)
            except AssertionError:
                raise ValueError("File does not support bpm tags.")
            song[TAG] = bpm
            try:
                assert song[TAG]
            except KeyError:
                raise ValueError("Couln't assign tag value.")
            song.write()
            song = formats.MusicFile(path)
            try:
                assert song[TAG]
            except KeyError:
                raise ValueError("Couldn't load tag value.")
            return 'Success %s = %s' % (path, song[TAG])
        raise ValueError("'%s' is no valid bpm value." % bpm)
    except (CalledProcessError, ValueError) as e:
        res = "Fail: %s" % cmd
        res += "\n%s" % e
        return res
def __parse_playlist(name, plfilename, files, library):
    playlist = Playlist.new(PLAYLISTS, name, library=library)
    songs = []
    win = WaitLoadWindow(
        None, len(files),
        _("Importing playlist.\n\n%(current)d/%(total)d songs added."))
    win.show()
    for i, filename in enumerate(files):
        try:
            uri = URI(filename)
        except ValueError:
            # Plain filename.
            filename = os.path.realpath(
                os.path.join(os.path.dirname(plfilename), filename))
            if library and filename in library:
                songs.append(library[filename])
            else:
                songs.append(formats.MusicFile(filename))
        else:
            if uri.scheme == "file":
                # URI-encoded local filename.
                filename = os.path.realpath(
                    os.path.join(os.path.dirname(plfilename), uri.filename))
                if library and filename in library:
                    songs.append(library[filename])
                else:
                    songs.append(formats.MusicFile(filename))
            else:
                # Who knows! Hand it off to GStreamer.
                songs.append(formats.remote.RemoteFile(uri))
        if win.step():
            break
    win.destroy()
    playlist.extend(filter(None, songs))
    return playlist
Exemple #3
0
 def _test_tag(self, tag, values, remove=True):
     self.failUnless(self.song.can_change(tag))
     for value in values:
         self.song[tag] = value
         self.song.write()
         written = formats.MusicFile(self.filename)
         self.failUnlessEqual(written[tag], value)
         if remove:
             del self.song[tag]
             self.song.write()
             deleted = formats.MusicFile(self.filename)
             self.failIf(tag in deleted)
    def test_music_file(self):
        path = get_data_path('silence-44-s.mp3')
        self.assertTrue(formats.MusicFile(path))

        # non existing
        with capture_output() as (stdout, stderr):
            song = formats.MusicFile(get_data_path("nope.mp3"))
            self.assertFalse(song)
            self.assertTrue(stderr.getvalue())

        # unknown extension
        with capture_output() as (stdout, stderr):
            song = formats.MusicFile(get_data_path("nope.xxx"))
            self.assertFalse(song)
            self.assertFalse(stderr.getvalue())
Exemple #5
0
    def test_music_file(self):
        path = os.path.join(DATA_DIR, 'silence-44-s.mp3')
        self.assertTrue(formats.MusicFile(path))

        # non existing
        with capture_output() as (stdout, stderr):
            song = formats.MusicFile(os.path.join(DATA_DIR, "nope.mp3"))
            self.assertFalse(song)
            self.assertTrue("Error" in stderr.getvalue())

        # unknown extension
        with capture_output() as (stdout, stderr):
            song = formats.MusicFile(os.path.join(DATA_DIR, "nope.xxx"))
            self.assertFalse(song)
            self.assertTrue("extension" in stderr.getvalue())
Exemple #6
0
 def __find_songs(self, selection):
     model, rows = selection.get_selected_rows()
     dirs = [model[row][0] for row in rows]
     songs = []
     to_add = []
     for dir in dirs:
         try:
             for file in filter(formats.filter, sorted(os.listdir(dir))):
                 raw_path = os.path.join(dir, file)
                 fn = normalize_path(raw_path, canonicalise=True)
                 if fn in self.__glibrary:
                     songs.append(self.__glibrary[fn])
                 elif fn not in self.__library:
                     song = formats.MusicFile(fn)
                     if song:
                         to_add.append(song)
                         songs.append(song)
                         yield songs
                 if fn in self.__library:
                     song = self.__library[fn]
                     if not song.valid():
                         self.__library.reload(song)
                     if song in self.__library:
                         songs.append(song)
         except OSError:
             pass
     self.__library.add(to_add)
     yield songs
Exemple #7
0
 def setUp(self):
     """Copy the base silent file to a temp name/location and load it"""
     config.init()
     fd, self.filename = mkstemp(suffix=self.ext, text=False)
     dst = os.fdopen(fd, 'w')
     src = open(self.base + self.ext, 'rb')
     copyfileobj(src, dst)
     dst.close()
     self.song = formats.MusicFile(self.filename)
Exemple #8
0
def _af_for(filename, library, pl_dir):
    full_path = os.path.join(pl_dir, filename)
    filename = os.path.realpath(full_path)

    af = None
    if library:
        af = library.get_filename(filename)
    if af is None:
        af = formats.MusicFile(filename)
    return af
Exemple #9
0
 def test_strip_id3(self):
     self.song["title"] = "Test"
     self.song.write()
     id3 = ID3()
     id3.add(TIT2(encoding=2, text=u"Test but differently"))
     id3.save(filename=self.filename)
     song2 = formats.MusicFile(self.filename)
     self.failUnlessEqual(type(self.song), type(song2))
     self.failUnlessEqual(self.song["title"], song2["title"])
     song2.write()
     self.assertRaises(ID3NoHeaderError, ID3, self.filename)
Exemple #10
0
def __parse_playlist(name, plfilename, files, library):
    playlist = FileBackedPlaylist.new(PLAYLISTS, name, library=library)
    songs = []
    win = WaitLoadWindow(
        None, len(files),
        _("Importing playlist.\n\n%(current)d/%(total)d songs added."))
    win.show()
    for i, filename in enumerate(files):
        if not uri_is_valid(filename):
            if os.name == "nt":
                filename = filename.decode("utf-8", "replace")
            # Plain filename.
            filename = os.path.realpath(os.path.join(
                os.path.dirname(plfilename), filename))
            if library and filename in library:
                songs.append(library[filename])
            else:
                songs.append(formats.MusicFile(filename))
        else:
            try:
                filename = uri_to_path(filename)
            except ValueError:
                # Who knows! Hand it off to GStreamer.
                songs.append(formats.remote.RemoteFile(filename))
            else:
                # URI-encoded local filename.
                filename = os.path.realpath(os.path.join(
                    os.path.dirname(plfilename), filename))
                if library and filename in library:
                    songs.append(library[filename])
                else:
                    songs.append(formats.MusicFile(filename))
        if win.step():
            break
    win.destroy()
    playlist.extend(filter(None, songs))
    return playlist
    def __changed(self, selector, selection, label):
        model, rows = selection.get_selected_rows()
        files = []

        if len(rows) < 2:
            count = len(model or [])
        else:
            count = len(rows)
        label.set_text(numeric_phrase("%d song", "%d songs", count))

        for row in rows:
            filename = model[row][0]
            if not os.path.exists(filename):
                pass
            elif filename in self.__library:
                song = self.__library[filename]
                if song("~#mtime") + 1. < mtime(filename):
                    try:
                        song.reload()
                    except AudioFileError:
                        pass
                files.append(song)
            else:
                files.append(formats.MusicFile(filename))
        files = list(filter(None, files))
        if len(files) == 0:
            self.set_title("Ex Falso")
        elif len(files) == 1:
            self.set_title("%s - Ex Falso" % files[0].comma("title"))
        else:
            params = ({
                'title': files[0].comma("title"),
                'count': format_int_locale(len(files) - 1)
            })
            self.set_title("%s - Ex Falso" % (ngettext(
                "%(title)s and %(count)s more", "%(title)s and %(count)s more",
                len(files) - 1) % params))
        self.__library.add(files)
        self.emit('changed', files)
    def __changed(self, selector, selection, label):
        model, rows = selection.get_selected_rows()
        files = []

        if len(rows) < 2:
            count = len(model or [])
        else:
            count = len(rows)
        label.set_text(ngettext("%d song", "%d songs", count) % count)

        for row in rows:
            filename = util.fsnative(model[row][0])
            if not os.path.exists(filename):
                pass
            elif filename in self.__library:
                file = self.__library[filename]
                if file("~#mtime") + 1. < mtime(filename):
                    try:
                        file.reload()
                    except StandardError:
                        pass
                files.append(file)
            else:
                files.append(formats.MusicFile(filename))
        files = filter(None, files)
        if len(files) == 0:
            self.set_title("Ex Falso")
        elif len(files) == 1:
            self.set_title("%s - Ex Falso" % files[0].comma("title"))
        else:
            self.set_title("%s - Ex Falso" % (ngettext(
                "%(title)s and %(count)d more", "%(title)s and %(count)d more",
                len(files) - 1) % ({
                    'title': files[0].comma("title"),
                    'count': len(files) - 1
                })))
        self.__library.add(files)
        self.emit('changed', files)