Exemple #1
0
def handleDirectory(directoryPath):
    """Take actions based on file types present and the user's configuration."""
    
    filePathsByType = functions.getFilePathsByType(directoryPath)
    
    if configuration.ACTIONS["IMAGE"] and "image" in filePathsByType:           # Rename/delete image(s)
        clean.handleImages(filePathsByType["image"])
        
    if configuration.ACTIONS["CLEAN"] and "other" in filePathsByType:           # Delete extra files
        clean.cleanDir(filePathsByType["other"])
    
    if configuration.ACTIONS["EXTRACT"] and "archive" in filePathsByType:       # Extract archives
        extract.extract(filePathsByType["archive"])                             # There may be new subdirectories
        return traverse(directoryPath)                                          # Traverse again
        
    if configuration.ACTIONS["CONVERT"] and "bad_audio" in filePathsByType:     # Convert audio to Ogg and scan again
        convert.convert(filePathsByType["bad_audio"])
        return handleDirectory(directoryPath)
        
    if not "good_audio" in filePathsByType:                                     # Continue if audio present
        log("\nNo audio found in %s." % quote(directoryPath))
        # FIXME: If the user has not requested the metadata process, should this
        # directory actually be accepted rather than deleted at this point?
        if directoryPath != configuration.PATHS["CURRENT"]:
            functions.deleteItem(directoryPath)
        return
            
    if configuration.ACTIONS["SPLIT"] and "cue" in filePathsByType:             # Split based on cue and scan again
        split.split(filePathsByType["cue"], filePathsByType["good_audio"])
        return handleDirectory(directoryPath)
        
    if configuration.ACTIONS["METADATA"]:                                       # Handle metadata
        audioPaths = clean.standardizeFilenames(filePathsByType["good_audio"])
        metadata.handleMetadata(directoryPath, audioPaths)
Exemple #2
0
def split(cuePaths, audioFilePaths):
    """Split audio files based on cue files.
    
    Takes a list of cue paths and a list of audio file paths.
    Rejects the folder if there are inequal numbers of audio and cue files.
    If there is one cue/audio pair, it is split using mp3splt. 
    If there are multiple pairs, they are moved into their own folders."""

    numCues = len(cuePaths)
    directoryPath = os.path.dirname(cuePaths[0])

    if numCues != len(audioFilePaths):
        log("There are unequal numbers of cues and audio files in %s." %
            quote(directoryPath))
        functions.rejectItem(directoryPath)
        return

    if numCues == 1:
        cuePath = cuePaths[0]
        audioFilePath = audioFilePaths[0]

        log("Splitting %s based on %s." %
            (quote(os.path.basename(audioFilePath)),
             quote(os.path.basename(cuePath))))

        command = [
            'mp3splt', '-d', directoryPath, '-c', cuePath, audioFilePath
        ]
        log(" ".join(command))

        try:
            p = subprocess.Popen(command)
            p.wait()
            success = p.returncode == 0
        except OSError:
            log("%s command not found." % command[0])
            success = False

        if success:
            log("Successfully split %s." % quote(audioFilePath))
            functions.deleteItem(cuePath)
            functions.deleteItem(audioFilePath)

        else:
            log("Unable to split %s." % quote(audioFilePath))
            functions.rejectItem(cuePath)
            functions.rejectItem(audioFilePath)

    else:
        log("Multiple cue/audio pairs in %s." % quote(directoryName))
        pairs = [(audioFilePaths[i], cuePaths[i]) for i in range(numCues)]
        functions.moveDiscsIntoFolders(pairs)
Exemple #3
0
def split(cuePaths, audioFilePaths):
    """Split audio files based on cue files.
    
    Takes a list of cue paths and a list of audio file paths.
    Rejects the folder if there are inequal numbers of audio and cue files.
    If there is one cue/audio pair, it is split using mp3splt. 
    If there are multiple pairs, they are moved into their own folders."""
    
    numCues = len(cuePaths)
    directoryPath = os.path.dirname(cuePaths[0])
    
    if numCues != len(audioFilePaths):
        log("There are unequal numbers of cues and audio files in %s." % 
            quote(directoryPath))
        functions.rejectItem(directoryPath)
        return

    if numCues == 1:
        cuePath = cuePaths[0]
        audioFilePath = audioFilePaths[0]
        
        log("Splitting %s based on %s." % 
            (quote(os.path.basename(audioFilePath)), 
             quote(os.path.basename(cuePath))))
        
        command = ['mp3splt', '-d', directoryPath, '-c', cuePath, audioFilePath]
        log(" ".join(command))
        
        try:
            p = subprocess.Popen(command)
            p.wait()
            success = p.returncode == 0
        except OSError:
            log("%s command not found." % command[0])
            success = False

        if success:
            log("Successfully split %s." % quote(audioFilePath))
            functions.deleteItem(cuePath)
            functions.deleteItem(audioFilePath)

        else:
            log("Unable to split %s." % quote(audioFilePath))
            functions.rejectItem(cuePath)
            functions.rejectItem(audioFilePath)
                           
    else: 
        log("Multiple cue/audio pairs in %s." % quote(directoryName))
        pairs = [(audioFilePaths[i], cuePaths[i]) for i in range(numCues)]
        functions.moveDiscsIntoFolders(pairs)
Exemple #4
0
def convert(audioFilePaths):
    """Convert undesirable audio formats into ogg.
    
    Takes a list of audio files and converts each to ogg using appropriate
    commands. These commands (mac, oggenc, mpc123) must be present."""

    for audioFilePath in audioFilePaths:
        fileName = os.path.basename(audioFilePath)
        with logSection("Converting %s." % quote(fileName)):
            filePathWithoutExtension, extension = os.path.splitext(
                audioFilePath)
            commands = convertorCommands[extension]

            success = True
            for command in commands:
                cmd = [
                    arg.replace("$$", filePathWithoutExtension)
                    for arg in command
                ]

                log(" ".join(cmd))
                try:
                    p = subprocess.Popen(cmd)
                    p.wait()
                except OSError:
                    log("% command not found." % cmd[0])
                    success = False
                    break

                if p.returncode != 0:
                    success = False
                    break

            if not success:
                # FIXME: Should we reject this file or this entire directory?
                log("Unable to convert %s." % quote(fileName))
                functions.rejectItem(audioFilePath)
            else:
                functions.deleteItem(audioFilePath)

            if len(commands) > 1:  # If we created an intermediate wav file
                functions.deleteItem(filePathWithoutExtension + ".wav", True)
Exemple #5
0
def extract(archivePaths):
    """Extract archives using appropriate utility.
    
    Takes a list of paths to archives and for each:
    Creates a directory with the same name as the archive, without extension.
    Chooses the utility to use for extraction based on the archive's extension.
    Attempts to extract the archive into the newly created directory.
    If the extraction fails, the directory is deleted and the archive rejected.
    If the extraction succeeds, the archive is discarded."""

    for archivePath in archivePaths:
        fileName = os.path.basename(archivePath)
        with logSection("Extracting %s." % quote(fileName)):
            destDirectoryPath, ext = os.path.splitext(archivePath)
            if not os.path.exists(destDirectoryPath):
                os.mkdir(destDirectoryPath)

            command = extractorCommands[ext.lower()][:]
            for (i, arg) in enumerate(command):
                if arg == "$a":
                    command[i] = archivePath
                elif arg == "$d":
                    command[i] = destDirectoryPath

            log(" ".join(command))

            try:
                p = subprocess.Popen(command)
                p.wait()
                success = p.returncode == 0
            except OSError:
                log("%s command not found." % command[0])
                success = False

            if not success:
                log("Unable to extract %s." % quote(archivePath))
                functions.deleteItem(destDirectoryPath)
                functions.rejectItem(archivePath)
            else:
                functions.deleteItem(archivePath)
Exemple #6
0
def extract(archivePaths):
    """Extract archives using appropriate utility.
    
    Takes a list of paths to archives and for each:
    Creates a directory with the same name as the archive, without extension.
    Chooses the utility to use for extraction based on the archive's extension.
    Attempts to extract the archive into the newly created directory.
    If the extraction fails, the directory is deleted and the archive rejected.
    If the extraction succeeds, the archive is discarded."""
    
    for archivePath in archivePaths:
        fileName = os.path.basename(archivePath)
        with logSection("Extracting %s." % quote(fileName)):
            destDirectoryPath, ext = os.path.splitext(archivePath)
            if not os.path.exists(destDirectoryPath):
                os.mkdir(destDirectoryPath)   
            
            command = extractorCommands[ext.lower()][:]
            for (i, arg) in enumerate(command):
                if arg == "$a":
                    command[i] = archivePath
                elif arg == "$d":
                    command[i] = destDirectoryPath
            
            log(" ".join(command))
            
            try:
                p = subprocess.Popen(command)
                p.wait()
                success = p.returncode == 0
            except OSError:
                log("%s command not found." % command[0])
                success = False
            
            if not success:
                log("Unable to extract %s." % quote(archivePath))
                functions.deleteItem(destDirectoryPath)
                functions.rejectItem(archivePath)
            else:
                functions.deleteItem(archivePath)
Exemple #7
0
def convert(audioFilePaths):
    """Convert undesirable audio formats into ogg.
    
    Takes a list of audio files and converts each to ogg using appropriate
    commands. These commands (mac, oggenc, mpc123) must be present."""

    for audioFilePath in audioFilePaths:
        fileName = os.path.basename(audioFilePath)
        with logSection("Converting %s." % quote(fileName)):
            filePathWithoutExtension, extension = os.path.splitext(audioFilePath)
            commands = convertorCommands[extension]

            success = True
            for command in commands:
                cmd = [arg.replace("$$", filePathWithoutExtension) for arg in command]

                log(" ".join(cmd))
                try:
                    p = subprocess.Popen(cmd)
                    p.wait()
                except OSError:
                    log("% command not found." % cmd[0])
                    success = False
                    break

                if p.returncode != 0:
                    success = False
                    break

            if not success:
                # FIXME: Should we reject this file or this entire directory?
                log("Unable to convert %s." % quote(fileName))
                functions.rejectItem(audioFilePath)
            else:
                functions.deleteItem(audioFilePath)

            if len(commands) > 1:  # If we created an intermediate wav file
                functions.deleteItem(filePathWithoutExtension + ".wav", True)