def extractStats(args):
    # Retrieve folder global information
    printRetrieveFolderInfo()
    folderInfo = FolderInfo(args['folder'])
    printRootFolderInfo(folderInfo)
    # Stat internals
    artists = []
    genres = []
    labels = []
    analyzedTracks = 0
    totalTracks = folderInfo.flacCounter + folderInfo.mp3Counter
    # Analyze progression utils
    printStatStart(args['folder'], totalTracks)
    step = 10
    percentage = step
    startTime = time.time()
    # Sort directories so they are handled in the alphabetical order
    for root, directories, files in sorted(os.walk(args['folder'])):
        files = [f for f in files if not f[0] == '.']  # Ignore hidden files
        directories[:] = [d for d in directories
                          if not d[0] == '.']  # ignore hidden directories
        # Split root into an array of folders
        path = root.split(os.sep)
        # Mutagen needs a preserved path when using ID3() or FLAC()
        preservedPath = list(path)
        rootPathLength = len(args['folder'].split(os.sep))
        # Poping all path element that are not the root folder, the artist sub folder or the album sub sub folder
        for x in range(rootPathLength - 1):
            path.pop(0)
        # Current path is for an album directory : perform tests
        if len(path) == 2 and path[1] != '':
            albumStats = StatMaker(files, preservedPath)
            artists = insertArtistInListIfNotExisting(
                artists, albumStats.artistsDetails)
            genres = insertInListIfNotExisting(genres, albumStats.genres)
            labels = insertInListIfNotExisting(labels, albumStats.labels)
            analyzedTracks += len(albumStats.tracks)
        # Display a progress every step %
        fillPercentage = (analyzedTracks * 100) / totalTracks
        if totalTracks > 10 and fillPercentage >= step and analyzedTracks < totalTracks:
            if (analyzedTracks *
                    100) / totalTracks > percentage and percentage < 100:
                printStatProgress(percentage, analyzedTracks)
                percentage += step
    # In this case, ui has display a percentage progression. No need to add a line break if no progression is to be displayed
    if totalTracks > 10 and percentage != 10:
        printLineBreak()
    duration = round(time.time() - startTime, 2)
    printStatEnd(duration, analyzedTracks)
    # Compute and save JSON report
    if args['dump']:
        saveReportFile(
            computeStatReport(scriptVersion, duration, artists, genres, labels,
                              folderInfo.folder), 'Stats', args['minify'],
            args['path'])
    else:
        print(artists)
        print(genres)
        print(labels)
def fillTags(args):
    # Retrieve folder global information
    printRetrieveFolderInfo()
    folderInfo = FolderInfo(args['folder'])
    printRootFolderInfo(folderInfo)
    # Fill internals
    filledTracks = 0
    totalTracks = folderInfo.flacCounter + folderInfo.mp3Counter
    # Start Fill
    printFillStart(args['folder'], totalTracks)
    rootPathLength = len(args['folder'].split(os.sep))
    albumFillers = []
    # Fill progression utils
    step = 10
    percentage = step
    startTime = time.time()
    # Sort directories so they are handled in the alphabetical order
    for root, directories, files in sorted(os.walk(args['folder'])):
        files = [f for f in files if not f[0] == '.']  # Ignore hidden files
        directories[:] = [d for d in directories
                          if not d[0] == '.']  # ignore hidden directories
        # Split root into an array of folders
        path = root.split(os.sep)
        # Mutagen needs a preserved path when using ID3() or FLAC()
        preservedPath = list(path)
        # Poping all path element that are not the root folder, the artist sub folder or the album sub sub folder
        for x in range(rootPathLength - 1):
            path.pop(0)
        # Current path is for an album directory : perform tests
        if len(path) == 2 and path[1] != '':
            albumFiller = AlbumFiller(files, preservedPath, args['verbose'],
                                      args['errors'])
            albumFillers.append(albumFiller)
            if albumFiller.hasErrors is False:
                filledTracks += albumFiller.album.totalTrack
        # Display a progress every step %
        fillPercentage = (filledTracks * 100) / totalTracks
        if totalTracks > 10 and fillPercentage >= step and filledTracks < totalTracks:
            if (filledTracks *
                    100) / totalTracks > percentage and percentage < 100:
                printFillProgress(percentage, filledTracks)
                percentage += step
    # Couldn't fill all track because of naming error
    if totalTracks is not filledTracks:
        printInvalidFolderStructure(filledTracks, totalTracks, 'fill')
    # In this case, ui has display a percentage progression. No need to add a line break if no progression is to be displayed
    if totalTracks > 10 and percentage != 10:  # If more than 10 tracks and percentage have been displayed (if % = 10, it is its init value)
        printLineBreak()
    duration = round(time.time() - startTime, 2)
    printFillEnd(duration, filledTracks)
def cleanTags(args):
    # Retrieve folder global information
    printRetrieveFolderInfo()
    folderInfo = FolderInfo(args['folder'])
    printRootFolderInfo(folderInfo)
    # Fill internals
    cleanedTracks = 0
    totalTracks = folderInfo.flacCounter + folderInfo.mp3Counter
    # Start Fill
    printCleanStart(args['folder'], totalTracks)
    rootPathLength = len(args['folder'].split(os.sep))
    albumCleaners = []
    # Fill progression utils
    step = 10
    percentage = step
    startTime = time.time()
    # Sort directories so they are handled in the alphabetical order
    for root, directories, files in sorted(os.walk(args['folder'])):
        files = [f for f in files if not f[0] == '.']  # Ignore hidden files
        directories[:] = [d for d in directories
                          if not d[0] == '.']  # ignore hidden directories
        # Split root into an array of folders
        path = root.split(os.sep)
        # Mutagen needs a preserved path when using ID3() or FLAC()
        preservedPath = list(path)
        # Poping all path element that are not the root folder, the artist sub folder or the album sub sub folder
        for x in range(rootPathLength - 1):
            path.pop(0)
        # Current path is for an album directory : perform tests
        if len(path) == 2 and path[1] != '':
            albumCleaner = AlbumCleaner(files, preservedPath)
            albumCleaners.append(albumCleaner)
            cleanedTracks += albumCleaner.album.totalTrack
        # Display a progress every step %
        cleanPercentage = (cleanedTracks * 100) / totalTracks
        if totalTracks > 10 and cleanPercentage >= step:
            if (cleanedTracks *
                    100) / totalTracks > percentage and percentage < 100:
                printCleanProgress(percentage, cleanedTracks)
                percentage += step
    # In this case, ui has display a percentage progression. No need to add a line break if no progression is to be displayed
    if totalTracks > 10:
        printLineBreak()
    duration = round(time.time() - startTime, 2)
    printCleanEnd(duration, cleanedTracks)
def scanFolder(args):
    # Retrieve folder global information
    printRetrieveFolderInfo()
    folderInfo = FolderInfo(args['folder'])
    printRootFolderInfo(folderInfo)
    # Scan internals
    totalTracks = folderInfo.flacCounter + folderInfo.mp3Counter
    rootPathLength = len(args['folder'].split(os.sep))
    scannedTracks = 0
    errorCounter = 0
    albumTesters = []
    # Scan progression utils
    step = 10
    percentage = step
    previousLetter = '1'  # Ordered folder/file parsing begins with numbers
    # Start scan
    printScanStart(args['folder'], totalTracks, len(ErrorEnum))
    startTime = time.time()
    # Sort directories so they are handled in the alphabetical order
    for root, directories, files in sorted(os.walk(args['folder'])):
        files = [f for f in files if not f[0] == '.']  # Ignore hidden files
        directories[:] = [d for d in directories
                          if not d[0] == '.']  # ignore hidden directories
        # Split root into an array of folders
        path = root.split(os.sep)
        # Mutagen needs a preserved path when using ID3() or FLAC()
        preservedPath = list(path)
        # Poping all path element that are not the root folder, the artist sub folder or the album sub sub folder
        for x in range(rootPathLength - 1):
            path.pop(0)
        # Current path is for an album directory : perform tests
        if len(path) == 2 and path[1] != '':
            albumTester = AlbumTester(files, preservedPath)
            scannedTracks += albumTester.album.totalTrack
            errorCounter += albumTester.tracksErrorCounter()
            errorCounter += albumTester.errorCounter
            albumTesters.append(albumTester)
            # Display a progress every step %
            scannedPercentage = (scannedTracks * 100) / totalTracks
            if totalTracks > 10 and scannedPercentage >= step and scannedTracks < totalTracks:
                if (scannedTracks *
                        100) / totalTracks > percentage and percentage < 100:
                    printScanProgress(
                        percentage, previousLetter, path[0][0], errorCounter,
                        scannedTracks,
                        computePurity(errorCounter, scannedTracks))
                    percentage += step
                    previousLetter = path[0][0]  # path[0] is the Artists name
    # In this case, ui has display a percentage progression. No need to add a line break if no progression is to be displayed
    if totalTracks > 10 and percentage != 10:
        printLineBreak()
    duration = round(time.time() - startTime, 2)
    printScanEnd(duration, errorCounter, totalTracks,
                 computePurity(errorCounter, scannedTracks))
    # Compute and save JSON report
    if args['dump']:
        saveReportFile(
            computeFillReport(scriptVersion, duration, folderInfo,
                              albumTesters, errorCounter,
                              computePurity(errorCounter, scannedTracks)),
            'Errors', args['minify'], args['path'])
    # Verbose report
    if args['verbose']:
        printErroredTracksReport(albumTesters)