diff --git a/gapi/drive_api.py b/gapi/drive_api.py index c1b00ba..68e44d4 100644 --- a/gapi/drive_api.py +++ b/gapi/drive_api.py @@ -95,7 +95,7 @@ class drive_folder(drive_file): 'parents' : [self.id] } info = self.api.service.files().create(body = meta).execute() - self.folders['name'] = drive_folder(self.api,info,self) + self.folders[name] = drive_folder(self.api,info,self) def refresh(self): '''finds all files and folders from a parent''' @@ -133,8 +133,6 @@ class drive_api(API): self.root = drive_folder.from_id('root',self,None) def get_file_by_path(self,path,ret_file = True): - if path == '/': - return self.root '''gets a file or folder by remote path''' if isinstance(path,str): path = split_path(path) @@ -142,6 +140,8 @@ class drive_api(API): path = path.copy() parent = self.root end = path.pop() + if end == '/': + return self.root for sub in path: if sub == '/': pass @@ -172,7 +172,7 @@ class drive_api(API): else: path = path.copy() missing = [] - for i in range(len(path),-1,-1): + for i in range(len(path),0,-1): try: parent = self.get_file_by_path(path[:i],False) break @@ -180,9 +180,11 @@ class drive_api(API): missing.append(path[i-1]) while len(missing) > 0: name = missing.pop() + parent.create_folder(name) parent = parent.folders[name] return parent - def upload(self,local_path,remote_path): + + def upload(self,local_path,remote_path,verbose = False): if not isinstance(remote_path,str): remote_path = pathjoin(*remote_path) if os.path.isdir(local_path): @@ -190,8 +192,9 @@ class drive_api(API): for file in files: new_path = path_translate(local_path,remote_path,root,file) self.__upload_file__(os.path.join(root,file),new_path) - print(new_path) - return self.get_file_by_path(remote_path,False,False) + if verbose: + print(new_path) + return self.get_file_by_path(remote_path,False) else: return self.__upload_file__(local_path,remote_path) @@ -205,14 +208,14 @@ class drive_api(API): try: old_file = self.get_file_by_path(remote_path) - self.service.files().update(fileId=old_file['id'],media_body=_upload).execute() + self.service.files().update(fileId=old_file.id,media_body=_upload).execute() return old_file except FileNotFoundError: path = remote_path end = path.pop() parent = self.mkdirs(path) - parent_id = parent[0]['id'] + parent_id = parent.id meta = { 'name':end, 'parents':[parent_id] @@ -222,9 +225,9 @@ class drive_api(API): media_body = _upload ).execute() new_f_meta = without(new_f_meta,'kind') - new_f_meta['parent'] = parent - parent['files'][end] = new_f_meta - return new_f_meta + new_f = drive_file(self,new_f_meta,parent) + parent.files[end] = new_f + return new_f if __name__ == "__main__": my_api = drive_api(APPLICATION_NAME,r'..\test\drive\client_secret.json',r'..\test\drive') diff --git a/test_drive.py b/test_drive.py index 84c638b..9cab088 100644 --- a/test_drive.py +++ b/test_drive.py @@ -2,6 +2,7 @@ import os import gapi from gapi.drive_api import drive_api,APPLICATION_NAME my_api = drive_api(APPLICATION_NAME,r'test\drive\client_secret.json',r'test\drive') -# t1 = my_api.get_file_by_path('/Python/Lib',False) -##t1 = my_api.mkdirs('/test/t1/this/is/a/path/why/isnt/this/working') -t1 = my_api.upload('test','/test/t3') +# t1 = my_api.get_file_by_path('/Python/Lib/site-packages',False) +# t1.refresh() +t2 = my_api.mkdirs('/test/t1/this/is/a/path/why/isnt/this/working') +t3 = my_api.upload(os.getcwd(),'/test/t3') \ No newline at end of file