o `V4@sddlmZddlmZddlZddlmZddlmZddlmZddl ZGdddej j Z ej d e  ej d e  ej d e ej jgd dS) )next)strN)log)util)BackendExceptionc@sXeZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ ddZ dS)PyDriveBackendz)Connect to remote store using PyDrive APIc stjj||z ddl}ddlm}Wnty(}ztdt |d}~wwi|_ d|_ d|j vrE|j dd|_ d|j ddd|_ zddl m}dd lm}dd lm}m}Wn=ty}z1zddlm}dd lm}dd lm}m}Wnty}ztd t |d}~wwWYd}~nd}~wwz dd lm} d|_Wndd lm} ddlm} d|_Ydtjvrtjd} |jr| |j d|j!| dd} n| j"#| }| |j d|j!|dd} | $|%|dd}| |_&nZdtjvr |tjddd}|'nFdtjvrLdtjvrL|dd}|(tjd|)tjd|j&dur6|'n |j*r?|+n|,|-tjdntd|||_.|j r]|j }n0|j./ddi0}|ru|dddd}n|j.1dd i}|2|ddd}|3|j45d!}|D]csqdd"|d#i}|6|j |j./|0}t7fd$d%|Dd}|durd&d|igd'}|dd6|j |6|j |j.1|}|j r|j2d(did)n|2|d}q||_8i|_9dS)*Nr)buildz}PyDrive backend requires PyDrive and Google API client installation. Please read the manpage for setup details. Exception: %sdriveID teamDriveT)corpora teamDriveIdincludeTeamDriveItemssupportsTeamDrives) GoogleAuth) GoogleDriveApiRequestErrorFileNotUploadedErrorzhPyDrive backend requires PyDrive installation. Please read the manpage for setup details. Exception: %s)SignedJwtAssertionCredentials)ServiceAccountCredentials)cryptFGOOGLE_DRIVE_ACCOUNT_KEY@z%https://www.googleapis.com/auth/drive)scopes<) http_timeoutGOOGLE_DRIVE_SETTINGS) settings_filerGOOGLE_SECRETS_FILEGOOGLE_CREDENTIALS_FILEzoGOOGLE_DRIVE_ACCOUNT_KEY or GOOGLE_DRIVE_SETTINGS environment variable not set. Please read the manpage to fix.qz#'Root' in parents and trashed=falseparentsidtitle i_am_in_root/'' in parents and trashed=falsec3s,|]}|dkr|ddkr|VqdS)r#mimeType"application/vnd.google-apps.folderN.0item folder_namer*C/usr/lib/python3/dist-packages/duplicity/backends/pydrivebackend.py s  z*PyDriveBackend.__init__..r))r#r(r!rparam): duplicitybackendBackend__init__httplib2apiclient.discoveryr ImportErrorrr api_paramsshared_drive_id query_args pydrive2.authrpydrive2.driverpydrive2.filesrr pydrive.auth pydrive.drive pydrive.filesoauth2client.clientr oldClientoauth2client.service_accountr oauth2clientrosenvironusernamehostnameSigner from_string authorizeHttp credentialsCommandLineAuthLoadClientConfigFileLoadCredentialsFileaccess_token_expiredRefresh AuthorizeSaveCredentialsFiledriveListFileGetList CreateFileUploadDeletepathsplitupdaterfolderid_cache)self parsed_urlr8rerrrrrrr account_keyrPsignergauthparent_folder_id file_list file_in_root folder_nameslist_file_argsracreate_file_argsr*r.r0r7!s                            zPyDriveBackend.__init__c Cszddlm}Wntyddlm}Ynwt|}||jvr|j|}|jd|i}z*|d|krX|ddsX|dD]}|d|j krWt d||f|WSq@Wn|yu}z|j dj jd krkWYd}~nd}~wwt d ||f|j|=d ||j f}d }||d } | |j|j| } t| dkrt td|fn| r| dd}| dd|j|<t d||f| dSt d|fdS)Nr)rr"r#labelstrashedr!z7PyDrive backend: found file '%s' with id %s in ID cacheizCPyDrive backend: invalidating '%s' (previously ID %s) from ID cachez0title='%s' and '%s' in parents and trashed=falsez>items(title,id,fileSize,downloadUrl,exportLinks),nextPageTokenr fieldsz,PyDrive backend: multiple files called '%s'.zFPyDrive backend: found file '%s' with id %s on server, adding to cachez:PyDrive backend: file '%s' not found in cache or on server)r@rr:rCrfsdecoderbrXr[rarInfoargsrespstatusr`r;rYrZlen FatalError_) rcfilenamerfile_id drive_fileparenterrorr rrrmflistr*r*r0 file_by_names^           zPyDriveBackend.file_by_namecCs||}|dur dS|dS)Nr")r)rcr|r~r*r*r0 id_by_names zPyDriveBackend.id_by_namecCst|}||}|dur1|d|jdgd}|dd|j|j|}t d|fn t d||df| t|j |j rQ|j d d id n| |d|j|<dS) Nzdrive#fileLink)kindr")r#r!r!rz'PyDrive backend: creating new file '%s'z:PyDrive backend: replacing existing file '%s' with id '%s'r"rTr2)rrtrrar`r;rXr[rruSetContentFilenamer<r\rb)rc source_pathremote_filenamer~rnr*r*r0_puts&   zPyDriveBackend._putcCs ||}|t|jdS)N)rGetContentFilerrtr)rcr local_pathr~r*r*r0_gets zPyDriveBackend._getcCs~d|jddd}||j|j|}tdd|D}t|j D]}||vr:| |dur:| |q(t|S)Nr&r'zitems(title,id),nextPageTokenrqcss|]}|dVqdS)r#Nr*r+r*r*r0r1sz'PyDriveBackend._list..) rar`r;rXrYrZsetlistrbkeysradd)rcrm drive_files filenamesr|r*r*r0_lists   zPyDriveBackend._listcCst||}|dkrtdt|fdS|jr+|jjj j |ddid dS|jjj j |d dS)Nrz2File '%s' does not exist while trying to delete itrT)fileIdr3)r) rrWarnrrtr<rXauthservicefilesdeleteexecute)rcr|r}r*r*r0_deletes $zPyDriveBackend._deletecCs2||}|durd}d|iSt|d}d|iS)NfileSizesize)rint)rcr|r~rr*r*r0_querys  zPyDriveBackend._querycCsdz ddlm}m}Wntyddlm}m}Ynwt||r%tjjSt||r.tjj Stjj S)Nrr) r@rrr:rC isinstancer ErrorCodebackend_not_foundbackend_permission_denied backend_error)rc operationrrrr*r*r0 _error_codes   zPyDriveBackend._error_codeN) __name__ __module__ __qualname____doc__r7rrrrrrrrr*r*r*r0rsv0 rpydrive pydrive+gdocsgdocs)rrr)builtinsrrrHr4rrduplicity.errorsrduplicity.backendr5r6rregister_backend uses_netlocextendr*r*r*r0s     w