o `@srddlmZddlZddlZddlmZmZddlm Z ddl m Z e Gdddej jZej dedS) )print_functionN)ClientJWTAuth)BackendException)standard_libraryc@s~eZdZddZddZddZddZd d Zd d Zd dZ dddZ ddZ ddZ ddZ ddZddZddZdS) BoxBackendcCsttjj|||||_|jddkr|jddn|j|_i|_| |j|_ |j dur8| |j|_ dSdS)Nr/) duplicitybackendBackend__init__get_box_client_clientpath_folder_file_to_metadata_mapget_id_from_path _folder_idmakedirs)self parsed_urlr?/usr/lib/python3/dist-packages/duplicity/backends/boxbackend.pyr "s  zBoxBackend.__init__c Csztj|jdd}tt|WStyL}z-tj d}|durDztt|WWYd}~StyC}zt dd}~wwt dd}~ww)NconfigrBOX_CONFIG_PATHzbox config file is not found.z.box config file is not specified or not found.) osr expanduser query_argsrrfrom_settings_file Exceptionenvirongetr)rr config_patherrrr1s&  zBoxBackend.get_box_clientcCsDz ||Wn tyYnw|j||ddS)zwUploads file to the specified remote folder (tries to delete it first to make sure the new one can be uploaded)) local_file remote_fileN)deletedecoder upload get_canonical)r source_pathremote_filenamerrr_putCs   zBoxBackend._putcCs|j||jddS)z-Downloads file from the specified remote path)r&r%N)downloadr(name)rr, local_pathrrr_getPs zBoxBackend._getcCs|S)z(Lists files in the specified remote path)folder_contentsrrrr_listXszBoxBackend._listcCs|j|ddS)z+Deletes file from the specified remote path)r&N)r'r()rfilenamerrr_delete]szBoxBackend._deletecsfdd|DS)z!Query metadata for a list of filecs$i|]}|j|ddiqS)size)rr"r().0r5r3rr ds  z*BoxBackend._query_list..r)r filename_listrr3r _query_listbs zBoxBackend._query_list0cCsdd|dD}|d}|dd} d}|jj|dD] }|j|kr,|j}nq |dur3dSt|dkr;|S|}|d}|dd}q) z'Get the folder or file id from its pathcS g|] }|dkr|qSstripr9xrrr mz/BoxBackend.get_id_from_path..rrr NT folder_id)splitrfolder get_itemsr/idlen)r remote_path parent_id path_itemsheadtailselected_item_iditemrrrrks(    zBoxBackend.get_id_from_pathcCsV|j|}|dur|dS|j||jd}|j|}|j|jd|j|j<|S)z Get the fild id by its file nameNrL)rOrLr7) rr"rrrfilerLr7r/)rr,rVfile_idrrrget_file_id_from_filenames z$BoxBackend.get_file_id_from_filenamecCsdd|dD}d}d}t|dkrCd}|jj|dD]}|j|dkr-|j}nq|dur5|}n|}|dd}t|dks|durZ|}|D]}|j||}|j}qK|S) z'Create folder(s) in a path if necessarycSr>r?rArCrrrrErFz'BoxBackend.makedirs..rr=NrrGr )rIrMrrJrKr/rLcreate_subfolder)rrNrPrOstart_folder_idrSrT subfolderrrrrs4    zBoxBackend.makedirscCsJdd|jj|jdjgddD}|jdd|Ddd|DS) z Lists files of a remote box pathcSsg|] }|jdkr|qS)rV)typerCrrrrEs  z.BoxBackend.folder_contents..rG)rLr/r7)fieldscSsi|] }|j|j|jdqS)rU)r/rLr7rCrrrr:sz.BoxBackend.folder_contents..cSsg|]}|jqSr)r/rCrrrrEs)rrJrrKrupdate)ritemsrrrr2s zBoxBackend.folder_contentscCs2|j|jj||d}|j|jd|j|j<dS)z#Upload local file to the box folder) file_path file_namerUN)rrJrr)rLr7rr/)rr&r%new_filerrrr)s zBoxBackend.uploadcCsL||}t|d}|j||WddS1swYdS)zDownload file in box folderwbN)rXopenrrV download_to)rr&r%rWfprrrr.s  "zBoxBackend.downloadcCs||}|j|dS)zDelete file in box folderN)rXrrVr')rr&rWrrrr's zBoxBackend.deleteN)r=)__name__ __module__ __qualname__r rr-r1r4r6r<rrXrr2r)r.r'rrrrr!s    rbox) __future__rrduplicity.backendr boxsdkrrduplicity.errorsrfuturerinstall_aliasesr r rregister_backendrrrrs   >