Exemplo n.º 1
0
 def testSafeAppend(self):
     self.assertEquals("A - ", safeAppend("A",  " - "))
     self.assertEquals("",     safeAppend(None, " - "))
     self.assertEquals("",     safeAppend("Unknown", " - ", suppress="Unknown"))
Exemplo n.º 2
0
    def __init__(self, filePath, rootPath=None):
        MuseFile.__init__(self, filePath, rootPath)
        self.artist = None
        self.album  = None
        self.md5    = None
        self.frames = {}

        # Determine meta-data from file path

        match = AudioFile.splitPattern.match(filePath)

        if not match:
            raise AudioFileError(filePath, "Failed to parse file path")

        (dirName, fileName, fileExt) = match.group(1, 2, 3)
        match = AudioFile.dirPattern.match(dirName)

        if not match:
           raise AudioFileError(filePath, "Failed to parse directory name " + dirName)

        (dirLetter, dirArtist, dirAlbum) = match.group(1, 2, 3)

        if not match.group(2) and match.group(3):
            dirAlbum  = None

            if dirLetter or len(match.group(3)) > 1:
                dirArtist = match.group(3)
            else:
                dirLetter = match.group(3)

        #print "Directory: Letter %s Artist %s Album %s" % (dirLetter, dirArtist, dirAlbum)
        match = AudioFile.filePattern.match(fileName)

        if not match:
            raise AudioFileError(filePath, "Failed to parse file name " + fileName)

        (fileArtist, fileAlbum, self.track, self.title) = match.group(1, 2, 3, 4)
        #print ("File name: Letter %s Artist %s Album %s Track %s Song %s Ext %s" %
        #       (dirLetter, fileArtist, fileAlbum, self.track, self.title, fileExt))

        self.artist = reconcileStrings(dirArtist, fileArtist, default="Unknown")

        if self.artist == "Unknown":
            warn("Failed to determine an artist from the filepath", filePath)

        elif self.artist == None:
            self.artist = dirArtist
            match       = AudioFile.numericPattern.match(fileArtist)

            if match and self.track == None:
                self.track = match.group(1)
                fileArtist = match.group(2)
                fileAlbum  = None

            if fileArtist:
                match = AudioFile.artistAlbumPattern.match(fileArtist)

                if match and reconcileStrings(dirArtist, match.group(1)) and reconcileStrings(dirAlbum, match.group(2)):
                    fileAlbum = match.group(2)
                else:
                    match = AudioFile.withArtistPattern.match(fileArtist)

                    if (not (match and (reconcileStrings(match.group(1), dirArtist) or reconcileStrings(match.group(2), dirArtist)))
                        and not simpleString(dirArtist) == simpleString(fileArtist).replace("_", " ")):
                        error("Directory artist '%s' differs from file name artist '%s'" % (dirArtist, fileArtist), filePath)

        self.album = reconcileStrings(dirAlbum, fileAlbum, default="Unknown")

        if self.album == "Unknown":
            info("Failed to determine an album from the filepath", filePath)    # Too common to warn on

        elif self.album == None:
            self.album = dirAlbum

            if fileArtist.isdigit() and reconcileStrings(fileAlbum, dirArtist):
                self.track = fileArtist

            elif not reconcileStrings(fileAlbum, dirAlbum):
                match = AudioFile.yearAlbumPattern.match(dirAlbum)

                if match and reconcileStrings(fileAlbum, match.group(2)):
                    self.year  = match.group(1)
                    self.album = match.group(2)

                else:
                    error("Directory album '%s' differs from file name album '%s'" % (dirAlbum, fileAlbum), filePath)

        newPath = ("%s/%s%s%s%s.%s"
                   % (dirName, safeAppend(self.artist, " - ", suppress="Unknown"), safeAppend(self.album, " - ", suppress="Unknown"),
                      safeAppend(self.track, " - "), self.title, fileExt))

        if getOption("fix", default=False) and newPath != filePath and takeAction("rename %s to %s" % (filePath, newPath)):
            os.rename(filePath, newPath)
            os.utime(newPath, None)