def download_from(self, buff, remote_path): # copy-pasted from the webdav lib with the non-needed additional http queries returned urn = Urn(remote_path) response = self.execute_request(action='download', path=urn.quote()) for chunk in response.iter_content(chunk_size=128): buff.write(chunk)
def download_file(self, remote_path, local_path, progress=None): # copy-pasted from the webdav lib with the non-needed additional http queries returned urn = Urn(remote_path) with open(local_path, 'wb') as local_file: response = self.execute_request('download', urn.quote()) for block in response.iter_content(1024): local_file.write(block)
def upload_to(self, buff, remote_path): # copy-pasted from the webdav lib with the non-needed additional http queries returned urn = Urn(remote_path) if urn.is_dir(): raise OptionNotValid(name="remote_path", value=remote_path) self.execute_request(action='upload', path=urn.quote(), data=buff)
def download_files(self, remote_paths: List[str], dst_dir: str) -> io.BytesIO: # copy-pasted from the webdav lib with the non-needed additional http queries returned for remote_path in remote_paths: urn = Urn(remote_path) file_name = os.path.join(dst_dir, os.path.basename(remote_path)) with open(file_name, 'wb') as local_file: response = self.execute_request('download', urn.quote()) for block in response.iter_content(1024): local_file.write(block)
def mkdir(self, remote_path): # copy-pasted from the webdav lib with the non-needed additional http queries returned directory_urn = Urn(remote_path, directory=True) try: response = self.execute_request(action='mkdir', path=directory_urn.quote()) except MethodNotSupported: # Yandex WebDAV returns 405 status code when directory already exists return True return response.status_code in (200, 201)
def upload_file(self, remote_path, local_path, progress=None): # copy-pasted from the webdav lib with the non-needed additional http queries returned if not os.path.exists(local_path): raise LocalResourceNotFound(local_path) urn = Urn(remote_path) if urn.is_dir(): raise OptionNotValid(name="remote_path", value=remote_path) if os.path.isdir(local_path): raise OptionNotValid(name="local_path", value=local_path) with open(local_path, "rb") as local_file: self.execute_request(action='upload', path=urn.quote(), data=local_file)
def list(self, remote_path=Client.root, get_info=False): # copy-pasted from the webdav lib with the non-needed additional http queries returned directory_urn = Urn(remote_path, directory=True) path = Urn.normalize_path(self.get_full_path(directory_urn)) response = self.execute_request(action='list', path=directory_urn.quote()) if get_info: subfiles = WebDavXmlUtils.parse_get_list_info_response( response.content) return [ subfile for subfile in subfiles if Urn.compare_path(path, subfile.get('path')) is False ] urns = WebDavXmlUtils.parse_get_list_response(response.content) return [ urn.filename() for urn in urns if Urn.compare_path(path, urn.path()) is False ]