Skip to content

Commit

Permalink
Support for shortcuts and fix bug in setup credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinyu committed Jan 25, 2021
1 parent ec3d368 commit dee80e3
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
24 changes: 17 additions & 7 deletions gdrive_access/access.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion gdrive_access/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
PyDrive==1.3.1
PyDrive2==1.7.0
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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="[email protected]",
license="MIT License",
packages=["gdrive_access"],
install_requires=[
"PyDrive==1.3.1",
"PyDrive2==1.7.0",
],
classifiers=[
"License :: OSI Approved :: MIT License",
Expand Down

0 comments on commit dee80e3

Please sign in to comment.