def initialize_dictionaries(service): ''' Initialize id_name_parents and name_id dictionaries These two dictionaries are data in memory that helps traveser the path of any given file/directory. The in-memory data increases efficiency, especially when a lot of commands are processed, requiring to find IDs of a lot of files/directores ''' root=service.files().get(fileId='root').execute() name_id[root['title']]=[root['id']] id_name_parents[root['id']]=idR(root['id'],"My Drive",None) results=service.files().list(q="trashed=false", spaces='drive', fields='items(id,title,parents,mimeType)').execute() items=results.get('items',[]) if not items: print ('No files exist in your drive') else: for item in items: fileID=item['id'] fileName=item['title'] if not (name_id.has_key(fileName)): name_id[fileName]=[fileID] else: name_id[fileName].append(fileID) parent_list=item['parents'] fileParents=[] for parent in parent_list: fileParents.append(parent['id']) id_name_parents[fileID]=idR(fileID,fileName,fileParents)
def add_id_name_parents(fileID,name,parent): ''' @param: fileID: file id of the file to be added into id_name_parents dictionary name: name of the file to be added parent: id (string) of the parent directory of the file in Google Drive @return: add an entry to the id_name_parents dictionary. This function is called when a file is uploaded into Google Drive ''' if not (fileID in id_name_parents): id_name_parents[fileID]=idR(fileID,name,[parent]) else: id_name_parents[fileID].add_parent(parent)