Skip to content

Commit

Permalink
multiple paths upload implemented (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
raul-martin-dev committed May 12, 2023
1 parent 4c2322d commit 28d6269
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 32 deletions.
84 changes: 56 additions & 28 deletions Driveup/drive.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from typing import overload,Union,List
from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload
import os
Expand All @@ -8,44 +9,71 @@ def __init__(self,creds):
self.mode = creds['type']
self.service = build('drive', 'v3', credentials=creds['creds'])

def upload(self,file_path,folder_id,file_title=None,file_id=None,update=True,convert=False,url=True):
@overload
def upload(self,file_path:list,folder_id:Union[str, List[str]],file_title:str=None,file_id:Union[str, List[str]]=None,update=True,convert=False,url=True):
...
@overload
def upload(self,file_path:str,folder_id:Union[str, List[str]],file_title:str=None,file_id:Union[str, List[str]]=None,update=True,convert=False,url=True):
...

def upload(self,file_path: Union[str, List[str]],folder_id:Union[str, List[str]],file_title:str=None,file_id: Union[str, List[str]]=None,update=True,convert=False,url=True):

if url == True:
folder_id = self.url_to_id(folder_id)
if isinstance(file_path, list):
if isinstance(folder_id, list): # needs a warning saying that will ignore file ids
for file,folder in zip(file_path,folder_id):
self.upload(file,folder_id=folder,file_title=file_title,file_id=file_id,update=update,convert=convert,url=url)
else:
if file_id == None:
for file in file_path:
self.upload(file,folder_id,file_title=file_title,file_id=file_id,update=update,convert=convert,url=url)
else: # needs a warning controlling if sizes of both lists are the same
for file,id in zip(file_path,file_id):
self.upload(file,folder_id,file_title=file_title,file_id=id,update=update,convert=convert,url=url)
else:
if isinstance(file_id, list): # needs a warning saying that file_id as 'list' is not intended for a single path
# print ('warning')
file_id = file_id[0]

if isinstance(folder_id, list): # needs a warning saying that folder_id as 'list' is not intended for a single path
# print ('warning')
folder_id = folder_id[0]

if url == True:
folder_id = self.url_to_id(folder_id)

if file_title == None:
file_title = self.get_filename(file_path)
if file_title == None:
file_title = self.get_filename(file_path)

drive_service = self.service
drive_service = self.service

file_metadata = None
file_metadata = None

media = MediaFileUpload(file_path, resumable=True)
media = MediaFileUpload(file_path, resumable=True)

# possible refactor
if update == True:
file_metadata = self.get_update(file_title,file_id,folder_id,drive_service)

if file_metadata == None: # Doesn't exist in the folder already or update=False
if self.mode == 'client':
file_metadata = {'name': file_title,'parents': [folder_id]}
else:
file_metadata = {'name': file_title,'parents': folder_id}
# possible refactor
if update == True:
file_metadata = self.get_update(file_title,file_id,folder_id,drive_service)
if file_metadata == None: # Doesn't exist in the folder already or update=False
if self.mode == 'client':
file_metadata = {'name': file_title,'parents': [folder_id]}
else:
file_metadata = {'name': file_title,'parents': folder_id}

if convert == True:
file_metadata = self.convert(file_metadata,self.get_file_extension(file_path))
if convert == True:
file_metadata = self.convert(file_metadata,self.get_file_extension(file_path))

gfile = drive_service.files().create(body=file_metadata, media_body=media, fields='id').execute()
else:
file_id = file_metadata['id']
void_metadata = {}
gfile = drive_service.files().update(fileId=file_id, body=void_metadata, media_body=media).execute()
gfile = drive_service.files().create(body=file_metadata, media_body=media, fields='id').execute()
else:
file_id = file_metadata['id']
void_metadata = {}
gfile = drive_service.files().update(fileId=file_id, body=void_metadata, media_body=media).execute()

if self.mode == 'service':
old_parents = gfile.get('parents')
file_id = gfile.get('id')
if self.mode == 'service':
old_parents = gfile.get('parents')
file_id = gfile.get('id')

drive_service.files().update(fileId=file_id,removeParents=old_parents,addParents=folder_id).execute()
drive_service.files().update(fileId=file_id,removeParents=old_parents,addParents=folder_id).execute()

# returns metadata for the file (whether it exists or not)
def get_update(self,name,file_id,folder_id,service):
Expand Down
2 changes: 1 addition & 1 deletion tests/__testsDatafiles/drive_testFiles/test_file.csv
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
test column key,test column value
test1,1
test1,5
test2,2
test3,3
test4,4
Expand Down
41 changes: 38 additions & 3 deletions tests/drive_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import unittest
import os
import Preln.preprocessing as pln

from Driveup.drive import Drive
from Driveup.features.auth import authorize
Expand All @@ -23,13 +24,46 @@ def test_upload(self):

drive_obj.upload(UPLOAD_FILE_PATH_2,DRIVE_FOLDER_ID)

def test_upload_list(self):
creds = authorize(SERVICE_SECRET_PATH)

drive_obj = Drive(creds)

files = [UPLOAD_FILE_PATH_1,UPLOAD_FILE_PATH_2]

drive_obj.upload(files,DRIVE_FOLDER_ID)

def test_upload_list_ids(self):
creds = authorize(SERVICE_SECRET_PATH)

drive_obj = Drive(creds)

ids = ['1LKUzha5IqtfQi3t8fnqK09pWfonfLKc3','1BJs_eDUR0XDR82N3sxzRz4wqrueJFGQX']

files = [UPLOAD_FILE_PATH_1,UPLOAD_FILE_PATH_2]
# files = [UPLOAD_FILE_PATH_1]
# files = UPLOAD_FILE_PATH_1

drive_obj.upload(files,'1R8caV6WVxSqKDC41EhmZB5ARBcp1F-7d',file_id=ids)

def test_upload_list_folders(self):
creds = authorize(SERVICE_SECRET_PATH)

drive_obj = Drive(creds)

# ids = ['1LKUzha5IqtfQi3t8fnqK09pWfonfLKc3','1BJs_eDUR0XDR82N3sxzRz4wqrueJFGQX']
files = [UPLOAD_FILE_PATH_1,UPLOAD_FILE_PATH_2]
folders = [DRIVE_FOLDER_ID,'1R8caV6WVxSqKDC41EhmZB5ARBcp1F-7d']

# drive_obj.upload(files,DRIVE_FOLDER_ID,file_id=ids)
drive_obj.upload(files,folders)
# drive_obj.upload(files,folders,file_id=ids)
# drive_obj.upload(UPLOAD_FILE_PATH_1,folders)

def test_upload_existing(self):
drive_obj = Drive(client_secret_path=CLIENT_SECRET_PATH)
drive_obj.upload(UPLOAD_FILE_PATH_1,DRIVE_FOLDER_ID)

# def test_convert(self):
# drive_obj = Drive(client_secret_path=CLIENT_SECRET_PATH)
# drive_obj.convert()

def test_upload_folder(self):

Expand Down Expand Up @@ -61,6 +95,7 @@ def test_upload_folder_service(self):
drive_obj = Drive(creds)

drive_obj.upload_folder(UPLOAD_FOLDER_PATH,DRIVE_FOLDER_ID,convert=True)



if __name__ == "__main__":
Expand Down

0 comments on commit 28d6269

Please sign in to comment.