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 get_content_in_path(conn_settings, path, root, list_dirs_only=True): client = Client(conn_settings) content = [] folders = [] files = [] remote_files = client.list(root) # Remove first file because it's always the parent folder for file in remote_files[1:]: # Retrieve only children of this path child_path = root + file if Urn(file).is_dir() and path.startswith(child_path): try: nested = get_content_in_path(conn_settings, path, child_path, list_dirs_only=list_dirs_only) except Exception as e: nested = [] folders.append({ 'name': file, 'value': child_path, 'options': nested, 'selected': False, 'expanded': True }) elif Urn(file).is_dir(): folders.append({ 'name': file, 'value': child_path, 'options': RETRIEVING_OPTIONS, 'selected': False, 'expanded': False }) elif not list_dirs_only: files.append({ 'name': file, 'value': child_path, 'options': [], 'selected': False, 'expanded': False }) # Folders and files are collected separately so folders can be listed first content.extend(folders) content.extend(files) return content
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 ]