From dee80e3bc1381ba54b3ba78f3c844aa5f850481c Mon Sep 17 00:00:00 2001 From: Kevin Yu Date: Mon, 25 Jan 2021 14:59:01 -0800 Subject: [PATCH] Support for shortcuts and fix bug in setup credentials --- gdrive_access/access.py | 24 +++++++++++++++++------- gdrive_access/display.py | 2 +- requirements.txt | 2 +- setup.py | 4 ++-- 4 files changed, 21 insertions(+), 11 deletions(-) diff --git a/gdrive_access/access.py b/gdrive_access/access.py index 6b4b3a3..9a084c9 100644 --- a/gdrive_access/access.py +++ b/gdrive_access/access.py @@ -1,8 +1,9 @@ +import glob import os import time -from pydrive.auth import GoogleAuth -from pydrive.drive import GoogleDrive +from pydrive2.auth import GoogleAuth +from pydrive2.drive import GoogleDrive from .display import * from .errors import CredentialsNotFound, MultipleFilesError, NotFoundError @@ -92,6 +93,15 @@ def _get_auth(self, settings_file="settings.yaml"): "'python -m gdrive_access.setup_credentials --dir CREDENTIALDIR'\n" "or fixing the location of the credentials location set in {}".format(settings_file)) + def _to_id(self, file_): + """Return the id of the object unless it is a shortcut, then find the true id. + """ + if file_["mimeType"] == "application/vnd.google-apps.shortcut": + file_.FetchMetadata(fields="shortcutDetails") + return file_["shortcutDetails"]["targetId"] + else: + return file_["id"] + def _find_one_level(self, dir, filename): """Look for a filename in google drive directory @@ -105,7 +115,7 @@ def _find_one_level(self, dir, filename): time.sleep(0.01) file_list = PyDriveListWrapper(self.drive.ListFile({ "q": "title = '{}' and '{}' in parents and " - "trashed = false".format(filename, dir["id"]) + "trashed = false".format(filename, self._to_id(dir)) }).GetList()) if not len(file_list): @@ -133,9 +143,9 @@ def find(self, root, *dirnames): def ls(self, root, *dirnames): if not len(dirnames): - id_ = root["id"] + id_ = self._to_id(root) else: - id_ = self.find(root, *dirnames)["id"] + id_ = self._to_id(self.find(root, *dirnames)) time.sleep(0.01) return PyDriveListWrapper(self.drive.ListFile({ @@ -185,7 +195,7 @@ def create_folder(self, create_in, folder_name, return_if_exists=True): new_folder = self.drive.CreateFile({ "title": folder_name, - "parents": [{"id": create_in["id"]}], + "parents": [{"id": self._to_id(create_in)}], "mimeType": "application/vnd.google-apps.folder" }) time.sleep(0.01) @@ -210,7 +220,7 @@ def upload_file(self, local_file_path, upload_to, uploaded_name=None, overwrite= raise Exception("File already exists, can't overwrite with overwrite=False") new_file = self.drive.CreateFile({ - "parents": [{"id": upload_to["id"]}], + "parents": [{"id": self._to_id(upload_to)}], "title": uploaded_name or filename, }) new_file.SetContentFile(local_file_path) diff --git a/gdrive_access/display.py b/gdrive_access/display.py index cfcb72e..fc88f8b 100644 --- a/gdrive_access/display.py +++ b/gdrive_access/display.py @@ -2,7 +2,7 @@ Wrappers for displaying pydrive results nicely """ -from pydrive.files import GoogleDriveFile +from pydrive2.files import GoogleDriveFile def _file_to_string(self): diff --git a/requirements.txt b/requirements.txt index e7fdd5c..60d2721 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1 @@ -PyDrive==1.3.1 +PyDrive2==1.7.0 diff --git a/setup.py b/setup.py index 95a1a2e..2f8374d 100644 --- a/setup.py +++ b/setup.py @@ -3,14 +3,14 @@ setup( name="gdrive_access", - version="0.0.2", + version="0.0.3", description="Simplified functions for downloading and uploading to Google Drive", author="Kevin Yu", author_email="kvnyu@berkeley.edu", license="MIT License", packages=["gdrive_access"], install_requires=[ - "PyDrive==1.3.1", + "PyDrive2==1.7.0", ], classifiers=[ "License :: OSI Approved :: MIT License",