Пример #1
0
def get_paths(drive: GoogleDrive, file: GoogleDriveFile) -> List[str]:
    """Returns a list of all paths under which the file can be found in the drive.

    Args:
      drive: The google drive in which to search.
      file: The file for which we need the paths.
    """
    # The root folder is the only one without parents and its path is the empty string
    if len(file.get('parents', [])) == 0:
        return ['']
    else:
        parents = [drive.get_file(p['id']) for p in file['parents']]
        parent_paths = itertools.chain(*[get_paths(drive, parent) for parent in parents])
        return [_normalize_path(f"{pp}/{file['title']}") for pp in parent_paths] 
Пример #2
0
def get_file_by_path(drive: GoogleDrive, path:str) -> GoogleDriveFile:
    """Tries to obtain a file from the drive via the given path.

    Args:
      drive: The google drive in question
      path: The path to obtain.
    """
    fname = path.split('/')[-1]
    if len(fname) == 0:
        return drive.get_file('root')
    candidate_list = drive.ListFile({'q': f"title = '{fname}' and trashed = false"}).GetList()
    
    for cand in candidate_list:
        cand_paths = drive.get_paths(cand)
        if _normalize_path(path) in cand_paths:
            return cand
    raise FileNotFoundError(f'Could not find file {path}')