def PrepareFolderDownload()

in gDrive_download.py [0:0]


def PrepareFolderDownload(folderID,folderTargetDir,dataEnumerator=None):
    '''
    Enumerate files and directories in a single folder, specified by the GUID
    folderID.  Will be called once for every folder we encounter.  Does not make
    recursive calls.
    '''
    if dataEnumerator == None:

        dataEnumerator = DataEnumerator()

    try:

        fileList = drive.ListFile({'q': "'%s' in parents and trashed=false" % folderID}).GetList()

    except Exception as ex:

        # ex = sys.exc_info()[0]
        errorString = str(ex)
        print("Error listing directory {}:{}:{}".format(folderTargetDir,folderID,errorString))
        dataEnumerator.errors.append( ['folder',folderTargetDir,folderID,errorString] )
        return dataEnumerator

    titles = set()

    # Handle redundant directory names
    for f in fileList:
        
        title = f['title']
        nRenames = 0
        
        if title in titles:            
            nRenames = nRenames + 1
            if bMergeDuplicateFolders:
                print("Warning: folder conflict at {}/{}".format(folderTargetDir,title))
            else:
                # Try to rename folders and files the way the gDrive sync app does, i.e. if there are 
                # two files called "Blah",  we want "Blah" and "Blah (1)".
                newTitle = title + " ({})".format(nRenames)
                print("Renaming {} to {} in [{}]".format(title,newTitle,folderTargetDir))
                title = newTitle
                f['title'] = title                
        else:
            titles.add(title)
            
    # ...for every file in our list (handling redundant directory names)
    
    # Enumerate and process files in this folder
    for f in fileList:
    
        if maxFiles > 0 and dataEnumerator.nFiles > maxFiles:
            return dataEnumerator

        dataEnumerator.fileInfo.append(f)

        title = f['title']
            
        if f['mimeType']=='application/vnd.google-apps.folder': # if folder

            dataEnumerator.nFolders = dataEnumerator.nFolders + 1
        
            # Create the target directory if necessary
            outputDir = os.path.join(folderTargetDir,title)
            f['target'] = outputDir
            if downloadImages:
                if not os.path.exists(outputDir):
                    os.mkdir(outputDir)

            print("Enumerating folder {} to {}".format(title,outputDir))        

            # Recurse
            dataEnumerator = PrepareFolderDownload(f['id'],outputDir,dataEnumerator)            
    
        else:            

            dataEnumerator.nFiles = dataEnumerator.nFiles + 1
        
            targetFile = os.path.join(folderTargetDir,title)            
            f['target'] = targetFile
            print("Downloading file {} to {}".format(title,targetFile))        
            dataEnumerator.downloadList.append( [targetFile,f['id']] )

    # ...for each file in this folder
    
    return dataEnumerator