def push(self, remote_directory, local_directory): def prune(src, exp): return [sub(exp, '', item) for item in src] urn = Urn(remote_directory, directory=True) if not self.is_dir(urn.path()): raise OptionNotValid(name='remote_path', value=remote_directory) if not os.path.isdir(local_directory): raise OptionNotValid(name='local_path', value=local_directory) if not os.path.exists(local_directory): raise LocalResourceNotFound(local_directory) paths = self.list(urn.path()) expression = f'^{urn.path()}' remote_resource_names = prune(paths, expression) for local_resource_name in listdir(local_directory): local_path = os.path.join(local_directory, local_resource_name) remote_path = f'{urn.path()}{local_resource_name}' if os.path.isdir(local_path): if not self.check(remote_path=remote_path): self.mkdir(remote_path=remote_path) self.push(remote_directory=remote_path, local_directory=local_path) else: if local_resource_name in remote_resource_names: continue self.upload_file(remote_path=remote_path, local_path=local_path)
def download(self, remote_path, local_path, progress=None): urn = Urn(remote_path) if self.is_dir(urn.path()): self.download_directory(local_path=local_path, remote_path=remote_path, progress=progress) else: self.download_file(local_path=local_path, remote_path=remote_path, progress=progress)
def download_to(self, buff, remote_path): try: urn = Urn(remote_path) if self.is_dir(urn.path()): raise OptionNotValid(name="remote_path", value=remote_path) if not self.check(urn.path()): raise RemoteResourceNotFound(urn.path()) url = {'hostname': self.webdav.hostname, 'root': self.webdav.root, 'path': urn.quote()} options = { 'URL': "{hostname}{root}{path}".format(**url), 'WRITEFUNCTION': buff.write, 'HTTPHEADER': self.get_header('download_to'), 'NOBODY': 0 } request = self.Request(options=options) request.perform() request.close() except pycurl.error: raise NotConnection(self.webdav.hostname)
def push(self, remote_directory, local_directory): def prune(src, exp): return [sub(exp, "", item) for item in src] urn = Urn(remote_directory, directory=True) if not self.is_dir(urn.path()): raise OptionNotValid(name="remote_path", value=remote_directory) if not os.path.isdir(local_directory): raise OptionNotValid(name="local_path", value=local_directory) if not os.path.exists(local_directory): raise LocalResourceNotFound(local_directory) paths = self.list(urn.path()) expression = "{begin}{end}".format(begin="^", end=urn.path()) remote_resource_names = prune(paths, expression) for local_resource_name in listdir(local_directory): local_path = os.path.join(local_directory, local_resource_name) remote_path = "{remote_directory}{resource_name}".format(remote_directory=urn.path(), resource_name=local_resource_name) if os.path.isdir(local_path): if not self.check(remote_path=remote_path): self.mkdir(remote_path=remote_path) self.push(remote_directory=remote_path, local_directory=local_path) else: if local_resource_name in remote_resource_names: continue self.upload_file(remote_path=remote_path, local_path=local_path)
def download_file(self, remote_path, local_path, progress=None): try: urn = Urn(remote_path) if self.is_dir(urn.path()): raise OptionNotValid(name="remote_path", value=remote_path) if os.path.isdir(local_path): raise OptionNotValid(name="local_path", value=local_path) if not self.check(urn.path()): raise RemoteResourceNotFound(urn.path()) with open(local_path, 'wb') as local_file: url = {'hostname': self.webdav.hostname, 'root': self.webdav.root, 'path': urn.quote()} options = { 'URL': "{hostname}{root}{path}".format(**url), 'HTTPHEADER': self.get_header('download_file'), 'WRITEDATA': local_file, 'NOPROGRESS': 0 if progress else 1, 'NOBODY': 0 } if progress: options["PROGRESSFUNCTION"] = progress request = self.Request(options=options) request.perform() request.close() except pycurl.error: raise NotConnection(self.webdav.hostname)
def download(self, remote_path, local_path, progress=None): urn = Urn(remote_path) if self.is_dir(urn.path()): self.download_directory(local_path=local_path, remote_path=remote_path, progress=progress) else: self.download_file(local_path=local_path, remote_path=remote_path, progress=progress)
def check(self, remote_path=root): try: urn = Urn(remote_path) response = BytesIO() url = { 'hostname': self.webdav.hostname, 'root': self.webdav.root, 'path': urn.quote() } options = { 'URL': "{hostname}{root}{path}".format(**url), 'CUSTOMREQUEST': Client.requests['check'], 'HTTPHEADER': self.get_header('check'), 'WRITEDATA': response, 'NOBODY': 1 } request = self.Request(options=options) request.perform() code = request.getinfo(pycurl.HTTP_CODE) request.close() if int(code) == 200: return True return False except pycurl.error: raise NotConnection(self.webdav.hostname)
def upload_from(self, buff, remote_path): try: urn = Urn(remote_path) if urn.is_dir(): raise OptionNotValid(name="remote_path", value=remote_path) if not self.check(urn.parent()): raise RemoteParentNotFound(urn.path()) url = {'hostname': self.webdav.hostname, 'root': self.webdav.root, 'path': urn.quote()} options = { 'URL': "{hostname}{root}{path}".format(**url), 'HTTPHEADER': self.get_header('upload_from'), 'UPLOAD': 1, 'READFUNCTION': buff.read, } request = self.Request(options=options) request.perform() code = request.getinfo(pycurl.HTTP_CODE) if code == "507": raise NotEnoughSpace() request.close() except pycurl.error: raise NotConnection(self.webdav.hostname)
def push(self, remote_directory, local_directory): def prune(src, exp): return [sub(exp, "", item) for item in src] urn = Urn(remote_directory, directory=True) if not self.is_dir(urn.path()): raise OptionNotValid(name="remote_path", value=remote_directory) if not os.path.isdir(local_directory): raise OptionNotValid(name="local_path", value=local_directory) if not os.path.exists(local_directory): raise LocalResourceNotFound(local_directory) paths = self.list(urn.path()) expression = "{begin}{end}".format(begin="^", end=urn.path()) remote_resource_names = prune(paths, expression) for local_resource_name in listdir(local_directory): local_path = os.path.join(local_directory, local_resource_name) remote_path = "{remote_directory}{resource_name}".format(remote_directory=urn.path(), resource_name=local_resource_name) if os.path.isdir(local_path): if not self.check(remote_path=remote_path): self.mkdir(remote_path=remote_path) self.push(remote_directory=remote_path, local_directory=local_path) else: if local_resource_name in remote_resource_names: continue self.upload_file(remote_path=remote_path, local_path=local_path)
def download_file(self, remote_path, local_path, progress=None): try: urn = Urn(remote_path) if self.is_dir(urn.path()): raise OptionNotValid(name="remote_path", value=remote_path) if os.path.isdir(local_path): raise OptionNotValid(name="local_path", value=local_path) if not self.check(urn.path()): raise RemoteResourceNotFound(urn.path()) with open(local_path, 'wb') as local_file: url = {'hostname': self.webdav.hostname, 'root': self.webdav.root, 'path': urn.quote()} options = { 'URL': "{hostname}{root}{path}".format(**url), 'HTTPHEADER': self.get_header('download_file'), 'WRITEDATA': local_file, 'NOPROGRESS': 0 if progress else 1, 'NOBODY': 0 } if progress: options["PROGRESSFUNCTION"] = progress request = self.Request(options=options) request.perform() request.close() except pycurl.error: raise NotConnection(self.webdav.hostname)
def upload_directory(self, remote_path, local_path, progress=None): """Uploads directory to remote path on WebDAV server. In case directory is exist on remote server it will delete it and then upload directory with nested files and directories. :param remote_path: the path to directory for uploading on WebDAV server. :param local_path: the path to local directory for uploading. :param progress: Progress function. Not supported now. """ urn = Urn(remote_path, directory=True) if not urn.is_dir(): raise OptionNotValid(name='remote_path', value=remote_path) if not os.path.isdir(local_path): raise OptionNotValid(name='local_path', value=local_path) if not os.path.exists(local_path): raise LocalResourceNotFound(local_path) if self.check(urn.path()): self.clean(urn.path()) self.mkdir(remote_path) for resource_name in listdir(local_path): _remote_path = f'{urn.path()}{resource_name}' _local_path = os.path.join(local_path, resource_name) self.upload(local_path=_local_path, remote_path=_remote_path, progress=progress)
def download_to(self, buff, remote_path): try: urn = Urn(remote_path) if self.is_dir(urn.path()): raise OptionNotValid(name="remote_path", value=remote_path) if not self.check(urn.path()): raise RemoteResourceNotFound(urn.path()) url = {'hostname': self.webdav.hostname, 'root': self.webdav.root, 'path': urn.quote()} options = { 'URL': "{hostname}{root}{path}".format(**url), 'WRITEFUNCTION': buff.write, 'HTTPHEADER': self.get_header('download_to'), 'NOBODY': 0 } request = self.Request(options=options) request.perform() request.close() except pycurl.error: raise NotConnection(self.webdav.hostname)
def get_property(self, remote_path, option, matrix_params=None, query_params=None): def parse(response, option): response_str = response.getvalue() tree = etree.fromstring(response_str) xpath = "{xpath_prefix}{xpath_exp}".format( xpath_prefix=".//", xpath_exp=option['name']) return tree.findtext(xpath) def data(option): root = etree.Element("propfind", xmlns="DAV:") prop = etree.SubElement(root, "prop") etree.SubElement(prop, option.get('name', ""), xmlns=option.get('namespace', "")) tree = etree.ElementTree(root) buff = BytesIO() tree.write(buff) return buff.getvalue() try: urn = Urn(remote_path, matrix_params=matrix_params, query_params=query_params) if not self.check(urn.path()): raise RemoteResourceNotFound(urn.path()) response = BytesIO() url = { 'hostname': self.webdav.hostname, 'root': self.webdav.root, 'path': urn.quote() } options = { 'URL': "{hostname}{root}{path}".format(**url), 'CUSTOMREQUEST': Client.requests['get_metadata'], 'HTTPHEADER': self.get_header('get_metadata'), 'POSTFIELDS': data(option), 'WRITEDATA': response, 'NOBODY': 0 } request = self.Request(options=options) request.perform() request.close() return parse(response, option) except pycurl.error: raise NotConnection(self.webdav.hostname)
def clean(self, remote_path, matrix_params=None, query_params=None): try: urn = Urn(remote_path, matrix_params=matrix_params, query_params=query_params) url = { 'hostname': self.webdav.hostname, 'root': self.webdav.root, 'path': urn.quote() } options = { 'URL': "{hostname}{root}{path}".format(**url), 'CUSTOMREQUEST': Client.requests['clean'], 'HTTPHEADER': self.get_header('clean') } request = self.Request(options=options) request.perform() request.close() except pycurl.error: raise NotConnection(self.webdav.hostname)
def download_directory(self, remote_path, local_path, progress=None, recursive=False): urn = Urn(remote_path, directory=True) if not self.is_dir(urn.path()): raise OptionNotValid(name="remote_path", value=remote_path) if os.path.exists(local_path): shutil.rmtree(local_path) os.makedirs(local_path) for resource_name in self.list(urn.path()): _remote_path = "{parent}{name}".format(parent=urn.path(), name=resource_name) _local_path = os.path.join(local_path, resource_name) if self.is_dir(_remote_path): if not os.path.isdir(_local_path): os.mkdir(_local_path) if recursive: self.download_directory(_remote_path) else: self.download(local_path=_local_path, remote_path=_remote_path, progress=progress)
def upload_from(self, buff, remote_path): try: urn = Urn(remote_path) if urn.is_dir(): raise OptionNotValid(name="remote_path", value=remote_path) if not self.check(urn.parent()): raise RemoteParentNotFound(urn.path()) url = {'hostname': self.webdav.hostname, 'root': self.webdav.root, 'path': urn.quote()} options = { 'URL': "{hostname}{root}{path}".format(**url), 'HTTPHEADER': self.get_header('upload_from'), 'UPLOAD': 1, 'READFUNCTION': buff.read, } request = self.Request(options=options) request.perform() code = request.getinfo(pycurl.HTTP_CODE) if code == "507": raise NotEnoughSpace() request.close() except pycurl.error: raise NotConnection(self.webdav.hostname)
def mkdir(self, remote_path): try: directory_urn = Urn(remote_path, directory=True) if not self.check(directory_urn.parent()): raise RemoteParentNotFound(directory_urn.path()) url = { 'hostname': self.webdav.hostname, 'root': self.webdav.root, 'path': directory_urn.quote() } options = { 'URL': "{hostname}{root}{path}".format(**url), 'CUSTOMREQUEST': Client.requests['mkdir'], 'HTTPHEADER': self.get_header('mkdir') } request = self.Request(options=options) request.perform() request.close() except pycurl.error: raise NotConnection(self.webdav.hostname)
def publish(self, remote_path): def parse(response): try: response_str = response.getvalue() tree = etree.fromstring(response_str) result = tree.xpath("//*[local-name() = 'public_url']") public_url = result[0] return public_url.text except IndexError: raise MethodNotSupported(name="publish", server=self.webdav.hostname) except etree.XMLSyntaxError: return "" def data(for_server): root_node = etree.Element("propertyupdate", xmlns="DAV:") set_node = etree.SubElement(root_node, "set") prop_node = etree.SubElement(set_node, "prop") xmlns = Client.meta_xmlns.get(for_server, "") public_url = etree.SubElement(prop_node, "public_url", xmlns=xmlns) public_url.text = "true" tree = etree.ElementTree(root_node) buff = BytesIO() tree.write(buff) return buff.getvalue() try: urn = Urn(remote_path) if not self.check(urn.path()): raise RemoteResourceNotFound(urn.path()) response = BytesIO() url = {'hostname': self.webdav.hostname, 'root': self.webdav.root, 'path': urn.quote()} options = { 'URL': "{hostname}{root}{path}".format(**url), 'CUSTOMREQUEST': Client.requests['publish'], 'HTTPHEADER': self.get_header('publish'), 'POSTFIELDS': data(for_server=self.webdav.hostname), 'WRITEFUNCTION': response.write, 'SSL_VERIFYPEER': 0, 'SSL_VERIFYHOST': 0, 'NOBODY': 0 } request = self.Request(options=options) request.perform() request.close() return parse(response) except pycurl.error: raise NotConnection(self.webdav.hostname)
def rename(self, new_name): old_path = self.urn.path() parent_path = self.urn.parent() new_name = Urn(new_name).filename() new_path = f'{parent_path}{new_name}' self.client.move(remote_path_from=old_path, remote_path_to=new_path) self.urn = Urn(new_path)
def rename(self, new_name): old_path = self.urn.path() parent_path = self.urn.parent() new_name = Urn(new_name).filename() new_path = '{}{}'.format(parent_path, new_name) self.client.move(remote_path_from=old_path, remote_path_to=new_path) self.urn = Urn(new_path)
def clean(self, remote_path): """Cleans (Deletes) a remote resource on WebDAV server. The name of method is not changed for back compatibility with original library. More information you can find by link http://webdav.org/specs/rfc4918.html#METHOD_DELETE :param remote_path: the remote resource whisch will be deleted. """ urn = Urn(remote_path) self.execute_request(action='clean', path=urn.quote())
def publish(self, remote_path): def parse(response): try: response_str = response.getvalue() tree = etree.fromstring(response_str) result = tree.xpath("//*[local-name() = 'public_url']") public_url = result[0] return public_url.text except IndexError: raise MethodNotSupported(name="publish", server=self.webdav.hostname) except etree.XMLSyntaxError: return "" def data(for_server): root_node = etree.Element("propertyupdate", xmlns="DAV:") set_node = etree.SubElement(root_node, "set") prop_node = etree.SubElement(set_node, "prop") xmlns = Client.meta_xmlns.get(for_server, "") public_url = etree.SubElement(prop_node, "public_url", xmlns=xmlns) public_url.text = "true" tree = etree.ElementTree(root_node) buff = BytesIO() tree.write(buff) return buff.getvalue() try: urn = Urn(remote_path) if not self.check(urn.path()): raise RemoteResourceNotFound(urn.path()) response = BytesIO() url = {'hostname': self.webdav.hostname, 'root': self.webdav.root, 'path': urn.quote()} options = { 'URL': "{hostname}{root}{path}".format(**url), 'CUSTOMREQUEST': Client.requests['publish'], 'HTTPHEADER': self.get_header('publish'), 'POSTFIELDS': data(for_server=self.webdav.hostname), 'WRITEDATA': response, 'NOBODY': 0 } request = self.Request(options=options) request.perform() request.close() return parse(response) except pycurl.error: raise NotConnection(self.webdav.hostname)
def rename(self, new_name): old_path = self.urn.path() parent_path = self.urn.parent() new_name = Urn(new_name).filename() new_path = "{directory}{filename}".format(directory=parent_path, filename=new_name) self.client.move(remote_path_from=old_path, remote_path_to=new_path) self.urn = Urn(new_path)
def upload(self, source_stream, device_path, chunk_size=8192): with self._internal_lock: self.is_remote_available.wait() self.logger.debug(self._prefix + "Uploading " + str(device_path)) iter = 0 while 1: self.is_remote_available.wait() already_sent = 0 # всегда начинаем сначала try: source_stream.seek(already_sent) # self._wc.upload_from(source_stream, device_path) try: urn = Urn(device_path) if urn.is_dir(): raise OptionNotValid(name="device_path", value=device_path) if not self._wc.check(urn.parent()): raise RemoteParentNotFound(urn.path()) url = {'hostname': self._wc.webdav.hostname, 'root': self._wc.webdav.root, 'path': urn.quote()} options = { 'URL': "{hostname}{root}{path}".format(**url), 'HTTPHEADER': self._wc.get_header('upload_from'), 'UPLOAD': 1, 'READFUNCTION': source_stream.read, } request = self._wc.Request(options=options) request.perform() code = request.getinfo(pycurl.HTTP_CODE) if str(code) == "507": # raise NotEnoughSpace() raise Exception("Not enough space on the server") request.close() except pycurl.error: raise NotConnection(self._wc.webdav.hostname) break except Exception as ex: iter += 1 if iter == 3: self.logger.error(self._prefix + "Uploading was interrupted " + str(iter) + " times: " + str(ex)) break else: self.logger.error(self._prefix + "Uploading was interrupted " + str(iter) + " times: " + str(ex)) time.sleep(1)
def __init__(self, options): self.options = dict() for key in self.keys: value = options.get(key, '') self.options[key] = value self.__dict__[key] = value self.root = Urn(self.root).quote() if self.root else '' self.root = self.root.rstrip(Urn.separate)
def upload_file(self, remote_path, local_path, progress=None): try: 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) if not os.path.exists(local_path): raise LocalResourceNotFound(local_path) if not self.check(urn.parent()): raise RemoteParentNotFound(urn.path()) with open(local_path, "rb") as local_file: url = {'hostname': self.webdav.hostname, 'root': self.webdav.root, 'path': urn.quote()} options = { 'URL': "{hostname}{root}{path}".format(**url), 'HTTPHEADER': self.get_header('upload_file'), 'UPLOAD': 1, 'READFUNCTION': local_file.read, 'NOPROGRESS': 0 if progress else 1 } if progress: options["PROGRESSFUNCTION"] = progress file_size = os.path.getsize(local_path) if file_size > self.large_size: options['INFILESIZE_LARGE'] = file_size else: options['INFILESIZE'] = file_size request = self.Request(options=options) request.perform() code = request.getinfo(pycurl.HTTP_CODE) if code == "507": raise NotEnoughSpace() request.close() except pycurl.error: raise NotConnection(self.webdav.hostname)
def check(self, remote_path=root): def parse(response, path): try: response_str = response.getvalue() tree = etree.fromstring(response_str) resps = tree.findall("{DAV:}response") for resp in resps: href = resp.findtext("{DAV:}href") urn = unquote(href) if path.endswith(Urn.separate): if path == urn or path[:-1] == urn: return True else: if path == urn: return True return False except etree.XMLSyntaxError: raise MethodNotSupported(name="check", server=self.webdav.hostname) try: urn = Urn(remote_path) parent_urn = Urn(urn.parent()) response = BytesIO() url = {'hostname': self.webdav.hostname, 'root': self.webdav.root, 'path': parent_urn.quote()} options = { 'URL': "{hostname}{root}{path}".format(**url), 'CUSTOMREQUEST': Client.requests['info'], 'HTTPHEADER': self.get_header('info'), 'WRITEDATA': response, 'NOBODY': 0 } request = self.Request(options=options) request.perform() request.close() path = "{root}{path}".format(root=self.webdav.root, path=urn.path()) return parse(response, path) except pycurl.error: raise NotConnection(self.webdav.hostname)
def pull(self, remote_directory, local_directory): def prune(src, exp): return [sub(exp, '', item) for item in src] urn = Urn(remote_directory, directory=True) if not self.is_dir(urn.path()): raise OptionNotValid(name='remote_path', value=remote_directory) if not os.path.exists(local_directory): raise LocalResourceNotFound(local_directory) local_resource_names = listdir(local_directory) paths = self.list(urn.path()) expression = '^{}'.format(remote_directory) remote_resource_names = prune(paths, expression) for remote_resource_name in remote_resource_names: local_path = os.path.join(local_directory, remote_resource_name) remote_path = '{}{}'.format(urn.path(), remote_resource_name) remote_urn = Urn(remote_path) if self.is_dir(remote_urn.path()): if not os.path.exists(local_path): os.mkdir(local_path) self.pull(remote_directory=remote_path, local_directory=local_path) else: if remote_resource_name in local_resource_names: continue self.download_file(remote_path=remote_path, local_path=local_path)
def pull(self, remote_directory, local_directory): def prune(src, exp): return [sub(exp, "", item) for item in src] urn = Urn(remote_directory, directory=True) if not self.is_dir(urn.path()): raise OptionNotValid(name="remote_path", value=remote_directory) if not os.path.exists(local_directory): raise LocalResourceNotFound(local_directory) local_resource_names = listdir(local_directory) paths = self.list(urn.path()) expression = "{begin}{end}".format(begin="^", end=remote_directory) remote_resource_names = prune(paths, expression) for remote_resource_name in remote_resource_names: _local_path = os.path.join(local_directory, remote_resource_name) _remote_path = "{remote_directory}{resource_name}".format( remote_directory=urn.path(), resource_name=remote_resource_name) remote_urn = Urn(_remote_path) if self.is_dir(remote_urn.path()): self.download_directory(remote_path=_remote_path, local_path=_local_path, recursive=True) else: self.download(_remote_path, _local_path)
def set_property(self, remote_path, option, matrix_params=None, query_params=None): def data(option): root_node = etree.Element("propertyupdate", xmlns="DAV:") root_node.set('xmlns:u', option.get('namespace', "")) set_node = etree.SubElement(root_node, "set") prop_node = etree.SubElement(set_node, "prop") opt_node = etree.SubElement( prop_node, "{namespace}:{name}".format(namespace='u', name=option['name'])) opt_node.text = option.get('value', "") tree = etree.ElementTree(root_node) buff = BytesIO() tree.write(buff) return buff.getvalue() try: urn = Urn(remote_path, matrix_params=matrix_params, query_params=query_params) if not self.check(urn.path()): raise RemoteResourceNotFound(urn.path()) url = { 'hostname': self.webdav.hostname, 'root': self.webdav.root, 'path': urn.quote() } options = { 'URL': "{hostname}{root}{path}".format(**url), 'CUSTOMREQUEST': Client.requests['set_metadata'], 'HTTPHEADER': self.get_header('get_metadata'), 'POSTFIELDS': data(option) } request = self.Request(options=options) request.perform() request.close() except pycurl.error: raise NotConnection(self.webdav.hostname)
def upload_file(self, remote_path, local_path, progress=None): try: 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) if not self.check(urn.parent()): raise RemoteParentNotFound(urn.path()) with open(local_path, "rb") as local_file: url = {'hostname': self.webdav.hostname, 'root': self.webdav.root, 'path': urn.quote()} options = { 'URL': "{hostname}{root}{path}".format(**url), 'HTTPHEADER': self.get_header('upload_file'), 'UPLOAD': 1, 'READFUNCTION': local_file.read, 'NOPROGRESS': 0 if progress else 1 } if progress: options["PROGRESSFUNCTION"] = progress file_size = os.path.getsize(local_path) if file_size > self.large_size: options['INFILESIZE_LARGE'] = file_size else: options['INFILESIZE'] = file_size request = self.Request(options=options) request.perform() code = request.getinfo(pycurl.HTTP_CODE) if code == "507": raise NotEnoughSpace() request.close() except pycurl.error: raise NotConnection(self.webdav.hostname)
def open(self, file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None): """Downloads file from WebDAV server and saves it temprorary, then opens it for further manipulations. Has the same interface as built-in open() :param file: the path to remote file for opening. """ urn = Urn(file) urn_path = urn.path() remote_file_exists = self.check(urn_path) if not remote_file_exists: if 'r' in mode: raise RemoteResourceNotFound(urn_path) elif self.is_dir(urn_path): raise OptionNotValid(name='file', value=file) with tempfile.TemporaryDirectory() as temp_dir: local_path = f'{temp_dir}{os.path.sep}{file}' if remote_file_exists: self.download_file(file, local_path) else: if ('w' in mode or 'a' in mode or 'x' in mode) and os.path.sep in local_path: os.makedirs(local_path.rsplit(os.path.sep, 1)[0], exist_ok=True) with open(file=local_path, mode=mode, buffering=buffering, encoding=encoding, errors=errors, newline=newline, closefd=closefd, opener=opener) as f: yield f if 'w' in mode or 'a' in mode or 'x' in mode: self.upload_file(file, local_path)
def get_property(self, remote_path, option): def parse(response, option): response_str = response.getvalue() tree = etree.fromstring(response_str) xpath = "{xpath_prefix}{xpath_exp}".format(xpath_prefix=".//", xpath_exp=option['name']) return tree.findtext(xpath) def data(option): root = etree.Element("propfind", xmlns="DAV:") prop = etree.SubElement(root, "prop") etree.SubElement(prop, option.get('name', ""), xmlns=option.get('namespace', "")) tree = etree.ElementTree(root) buff = BytesIO() tree.write(buff) return buff.getvalue() try: urn = Urn(remote_path) if not self.check(urn.path()): raise RemoteResourceNotFound(urn.path()) response = BytesIO() url = {'hostname': self.webdav.hostname, 'root': self.webdav.root, 'path': urn.quote()} options = { 'URL': "{hostname}{root}{path}".format(**url), 'CUSTOMREQUEST': Client.requests['get_metadata'], 'HTTPHEADER': self.get_header('get_metadata'), 'POSTFIELDS': data(option), 'WRITEDATA': response, 'NOBODY': 0 } request = self.Request(options=options) request.perform() request.close() return parse(response, option) except pycurl.error: raise NotConnection(self.webdav.hostname)
def check(self, remote_path=root): try: urn = Urn(remote_path) response = BytesIO() url = {'hostname': self.webdav.hostname, 'root': self.webdav.root, 'path': urn.quote()} options = { 'URL': "{hostname}{root}{path}".format(**url), 'CUSTOMREQUEST': Client.requests['check'], 'HTTPHEADER': self.get_header('check'), 'WRITEDATA': response, 'NOBODY': 1 } request = self.Request(options=options) request.perform() code = request.getinfo(pycurl.HTTP_CODE) request.close() if int(code) == 200: return True return False except pycurl.error: raise NotConnection(self.webdav.hostname)
def download_directory(self, remote_path, local_path, progress=None): urn = Urn(remote_path, directory=True) if not self.is_dir(urn.path()): raise OptionNotValid(name="remote_path", value=remote_path) if os.path.exists(local_path): shutil.rmtree(local_path) os.makedirs(local_path) for resource_name in self.list(urn.path()): _remote_path = "{parent}{name}".format(parent=urn.path(), name=resource_name) _local_path = os.path.join(local_path, resource_name) self.download(local_path=_local_path, remote_path=_remote_path, progress=progress)
def header(remote_path_to): path = Urn(remote_path_to).path() destination = "{root}{path}".format(root=self.webdav.root, path=path) header_item = "Destination: {destination}".format(destination=destination) header = self.get_header('move') header.append(header_item) return header
def download(self, remote_path, local_path, progress=None): """Downloads remote resource from WebDAV and save it in local path. More information you can find by link http://webdav.org/specs/rfc4918.html#rfc.section.9.4 :param remote_path: the path to remote resource for downloading can be file and directory. :param local_path: the path to save resource locally. :param progress: progress function. Not supported now. """ urn = Urn(remote_path) if self.is_dir(urn.path()): self.download_directory(local_path=local_path, remote_path=remote_path, progress=progress) else: self.download_file(local_path=local_path, remote_path=remote_path, progress=progress)
def check(self, remote_path=root): """Checks an existence of remote resource on WebDAV server by remote path. More information you can find by link http://webdav.org/specs/rfc4918.html#rfc.section.9.4 :param remote_path: (optional) path to resource on WebDAV server. Defaults is root directory of WebDAV. :return: True if resource is exist or False otherwise """ urn = Urn(remote_path) try: response = self.execute_request(action='check', path=urn.quote()) except ResponseErrorCode: return False if int(response.status_code) == 200: return True return False
def rename(self, new_name): old_path = self.urn.path() parent_path = self.urn.parent() new_name = Urn(new_name).filename() new_path = "{directory}{filename}".format(directory=parent_path, filename=new_name) self.client.move(remote_path_from=old_path, remote_path_to=new_path) self.urn = Urn(new_path)
def parse(response): try: response_str = response.getvalue() tree = etree.fromstring(response_str) hrees = [unquote(hree.text) for hree in tree.findall(".//{DAV:}href")] return [Urn(hree) for hree in hrees] except etree.XMLSyntaxError: return list()
def set_property_batch(self, remote_path, option): """Sets batch metadata properties of remote resource on WebDAV server in batch. More information you can find by link http://webdav.org/specs/rfc4918.html#METHOD_PROPPATCH :param remote_path: the path to remote resource. :param option: the property attributes as list of dictionaries with following keys: `namespace`: (optional) the namespace for XML property which will be set, `name`: the name of property which will be set, `value`: (optional) the value of property which will be set. Defaults is empty string. """ urn = Urn(remote_path) if not self.check(urn.path()): raise RemoteResourceNotFound(urn.path()) data = WebDavXmlUtils.create_set_property_batch_request_content(option) self.execute_request(action='set_property', path=urn.quote(), data=data)
def list(self, remote_path=root): def parse(response): try: response_str = response.getvalue() tree = etree.fromstring(response_str) hrees = [unquote(hree.text) for hree in tree.findall(".//{DAV:}href")] return [Urn(hree) for hree in hrees] except etree.XMLSyntaxError: return list() try: directory_urn = Urn(remote_path, directory=True) if directory_urn.path() != Client.root: if not self.check(directory_urn.path()): raise RemoteResourceNotFound(directory_urn.path()) response = BytesIO() url = {'hostname': self.webdav.hostname, 'root': self.webdav.root, 'path': directory_urn.quote()} options = { 'URL': "{hostname}{root}{path}".format(**url), 'CUSTOMREQUEST': Client.requests['list'], 'HTTPHEADER': self.get_header('list'), 'WRITEDATA': response, 'NOBODY': 0 } request = self.Request(options=options) request.perform() request.close() urns = parse(response) path = "{root}{path}".format(root=self.webdav.root, path=directory_urn.path()) return [urn.filename() for urn in urns if urn.path() != path and urn.path() != path[:-1]] except pycurl.error: raise NotConnection(self.webdav.hostname)
def set_property(self, remote_path, option): def data(option): root_node = etree.Element('propertyupdate', xmlns='DAV:', nsmap = {'u' : option.get('namespace', "")}) set_node = etree.SubElement(root_node, 'set') prop_node = etree.SubElement(set_node, 'prop') opt_node = etree.SubElement(prop_node, "{%s}%s"%(option.get('namespace', ""),option.get('name', ""))) opt_node.text = option.get('value', "") tree = etree.ElementTree(root_node) buff = BytesIO() tree.write(buff) return buff.getvalue() try: urn = Urn(remote_path) if not self.check(urn.path()): raise RemoteResourceNotFound(urn.path()) response = BytesIO() url = {'hostname': self.webdav.hostname, 'root': self.webdav.root, 'path': urn.quote()} options = { 'URL': "{hostname}{root}{path}".format(**url), 'CUSTOMREQUEST': Client.requests['set_metadata'], 'HTTPHEADER': self.get_header('get_metadata'), 'POSTFIELDS': data(option), 'WRITEDATA': response, 'NOBODY': 0 } request = self.Request(options=options) request.perform() request.close() except pycurl.error: raise NotConnection(self.webdav.hostname)
def __init__(self, options): self.options = dict() for key in self.keys: value = options.get(key, '') self.options[key] = value self.__dict__[key] = value self.root = Urn(self.root).quote() if self.root else '' self.root = self.root.rstrip(Urn.separate)
def upload_directory(self, remote_path, local_path, progress=None): urn = Urn(remote_path, directory=True) if not urn.is_dir(): raise OptionNotValid(name="remote_path", value=remote_path) if not os.path.isdir(local_path): raise OptionNotValid(name="local_path", value=local_path) if not os.path.exists(local_path): raise LocalResourceNotFound(local_path) if self.check(urn.path()): self.clean(urn.path()) self.mkdir(remote_path) for resource_name in listdir(local_path): _remote_path = "{parent}{name}".format(parent=urn.path(), name=resource_name) _local_path = os.path.join(local_path, resource_name) self.upload(local_path=_local_path, remote_path=_remote_path, progress=progress)
def set_property(self, remote_path, option): def data(option): root_node = etree.Element("propertyupdate", xmlns="DAV:") root_node.set('xmlns:u', option.get('namespace', "")) set_node = etree.SubElement(root_node, "set") prop_node = etree.SubElement(set_node, "prop") opt_node = etree.SubElement(prop_node, "{namespace}:{name}".format(namespace='u', name=option['name'])) opt_node.text = option.get('value', "") tree = etree.ElementTree(root_node) buff = BytesIO() tree.write(buff) return buff.getvalue() try: urn = Urn(remote_path) if not self.check(urn.path()): raise RemoteResourceNotFound(urn.path()) url = {'hostname': self.webdav.hostname, 'root': self.webdav.root, 'path': urn.quote()} options = { 'URL': "{hostname}{root}{path}".format(**url), 'CUSTOMREQUEST': Client.requests['set_metadata'], 'HTTPHEADER': self.get_header('get_metadata'), 'POSTFIELDS': data(option) } request = self.Request(options=options) request.perform() request.close() except pycurl.error: raise NotConnection(self.webdav.hostname)
def mkdir(self, remote_path): try: directory_urn = Urn(remote_path, directory=True) if not self.check(directory_urn.parent()): raise RemoteParentNotFound(directory_urn.path()) url = {'hostname': self.webdav.hostname, 'root': self.webdav.root, 'path': directory_urn.quote()} options = { 'URL': "{hostname}{root}{path}".format(**url), 'CUSTOMREQUEST': Client.requests['mkdir'], 'HTTPHEADER': self.get_header('mkdir') } request = self.Request(options=options) request.perform() request.close() except pycurl.error: raise NotConnection(self.webdav.hostname)
def unpublish(self, remote_path): def data(for_server): root = etree.Element("propertyupdate", xmlns="DAV:") remove = etree.SubElement(root, "remove") prop = etree.SubElement(remove, "prop") xmlns = Client.meta_xmlns.get(for_server, "") etree.SubElement(prop, "public_url", xmlns=xmlns) tree = etree.ElementTree(root) buff = BytesIO() tree.write(buff) return buff.getvalue() try: urn = Urn(remote_path) if not self.check(urn.path()): raise RemoteResourceNotFound(urn.path()) url = {'hostname': self.webdav.hostname, 'root': self.webdav.root, 'path': urn.quote()} options = { 'URL': "{hostname}{root}{path}".format(**url), 'CUSTOMREQUEST': Client.requests['unpublish'], 'HTTPHEADER': self.get_header('unpublish'), 'POSTFIELDS': data(for_server=self.webdav.hostname) } request = self.Request(options=options) request.perform() request.close() except pycurl.error: raise NotConnection(self.webdav.hostname)
def clean(self, remote_path): try: urn = Urn(remote_path) url = {'hostname': self.webdav.hostname, 'root': self.webdav.root, 'path': urn.quote()} options = { 'URL': "{hostname}{root}{path}".format(**url), 'CUSTOMREQUEST': Client.requests['clean'], 'HTTPHEADER': self.get_header('clean') } request = self.Request(options=options) request.perform() request.close() except pycurl.error: raise NotConnection(self.webdav.hostname)
class WebDAVSettings(ConnectionSettings): ns = "webdav:" prefix = "webdav_" keys = {'hostname', 'login', 'password', 'token', 'root', 'cert_path', 'key_path', 'recv_speed', 'send_speed', 'verbose'} def __init__(self, options): self.options = dict() for key in self.keys: value = options.get(key, '') self.options[key] = value self.__dict__[key] = value self.root = Urn(self.root).quote() if self.root else '' self.root = self.root.rstrip(Urn.separate) def is_valid(self): if not self.hostname: raise OptionNotValid(name="hostname", value=self.hostname, ns=self.ns) if self.cert_path and not exists(self.cert_path): raise OptionNotValid(name="cert_path", value=self.cert_path, ns=self.ns) if self.key_path and not exists(self.key_path): raise OptionNotValid(name="key_path", value=self.key_path, ns=self.ns) if self.key_path and not self.cert_path: raise OptionNotValid(name="cert_path", value=self.cert_path, ns=self.ns) if self.password and not self.login: raise OptionNotValid(name="login", value=self.login, ns=self.ns) if not self.token and not self.login: raise OptionNotValid(name="login", value=self.login, ns=self.ns)
def copy(self, remote_path_from, remote_path_to): def header(remote_path_to): path = Urn(remote_path_to).path() destination = "{root}{path}".format(root=self.webdav.root, path=path) header_item = "Destination: {destination}".format(destination=destination) header = self.get_header('copy') header.append(header_item) return header try: urn_from = Urn(remote_path_from) if not self.check(urn_from.path()): raise RemoteResourceNotFound(urn_from.path()) urn_to = Urn(remote_path_to) if not self.check(urn_to.parent()): raise RemoteParentNotFound(urn_to.path()) url = {'hostname': self.webdav.hostname, 'root': self.webdav.root, 'path': urn_from.quote()} options = { 'URL': "{hostname}{root}{path}".format(**url), 'CUSTOMREQUEST': Client.requests['copy'], 'HTTPHEADER': header(remote_path_to) } request = self.Request(options=options) request.perform() request.close() except pycurl.error: raise NotConnection(self.webdav.hostname)
def info(self, remote_path): def parse(response, path): try: response_str = response.getvalue() tree = etree.fromstring(response_str) find_attributes = { 'created': ".//{DAV:}creationdate", 'name': ".//{DAV:}displayname", 'size': ".//{DAV:}getcontentlength", 'modified': ".//{DAV:}getlastmodified" } resps = tree.findall("{DAV:}response") for resp in resps: href = resp.findtext("{DAV:}href") urn = unquote(href) if path[-1] == Urn.separate: if not path == urn: continue else: path_with_sep = "{path}{sep}".format(path=path, sep=Urn.separate) if not path == urn and not path_with_sep == urn: continue info = dict() for (name, value) in find_attributes.items(): info[name] = resp.findtext(value) return info raise RemoteResourceNotFound(path) except etree.XMLSyntaxError: raise MethodNotSupported(name="info", server=self.webdav.hostname) try: urn = Urn(remote_path) response = BytesIO() if not self.check(urn.path()) and not self.check(Urn(remote_path, directory=True).path()): raise RemoteResourceNotFound(remote_path) url = {'hostname': self.webdav.hostname, 'root': self.webdav.root, 'path': urn.quote()} options = { 'URL': "{hostname}{root}{path}".format(**url), 'CUSTOMREQUEST': Client.requests['info'], 'HTTPHEADER': self.get_header('info'), 'WRITEDATA': response, 'NOBODY': 0 } request = self.Request(options=options) request.perform() request.close() path = "{root}{path}".format(root=self.webdav.root, path=urn.path()) return parse(response, path) except pycurl.error: raise NotConnection(self.webdav.hostname)
def is_dir(self, remote_path): def parse(response, path): try: response_str = response.getvalue() tree = etree.fromstring(response_str) resps = tree.findall("{DAV:}response") for resp in resps: href = resp.findtext("{DAV:}href") urn = unquote(href) if path[-1] == Urn.separate: if not path == urn: continue else: path_with_sep = "{path}{sep}".format(path=path, sep=Urn.separate) if not path == urn and not path_with_sep == urn: continue type = resp.find(".//{DAV:}resourcetype") if type is None: raise MethodNotSupported(name="is_dir", server=self.webdav.hostname) dir_type = type.find("{DAV:}collection") return True if dir_type is not None else False raise RemoteResourceNotFound(path) except etree.XMLSyntaxError: raise MethodNotSupported(name="is_dir", server=self.webdav.hostname) try: urn = Urn(remote_path) parent_urn = Urn(urn.parent()) if not self.check(urn.path()) and not self.check(Urn(remote_path, directory=True).path()): raise RemoteResourceNotFound(remote_path) response = BytesIO() url = {'hostname': self.webdav.hostname, 'root': self.webdav.root, 'path': parent_urn.quote()} options = { 'URL': "{hostname}{root}{path}".format(**url), 'CUSTOMREQUEST': Client.requests['info'], 'HTTPHEADER': self.get_header('info'), 'WRITEDATA': response, 'NOBODY': 0 } request = self.Request(options=options) request.perform() request.close() path = "{root}{path}".format(root=self.webdav.root, path=urn.path()) return parse(response, path) except pycurl.error: raise NotConnection(self.webdav.hostname)
def resource(self, remote_path): urn = Urn(remote_path) return Resource(self, urn.path())
def move(self, remote_path): new_urn = Urn(remote_path) self.client.move(remote_path_from=self.urn.path(), remote_path_to=new_urn.path()) self.urn = new_urn
class Resource(object): def __init__(self, client, urn): self.client = client self.urn = urn def __str__(self): return "resource {path}".format(path=self.urn.path()) def is_dir(self): return self.client.is_dir(self.urn.path()) def rename(self, new_name): old_path = self.urn.path() parent_path = self.urn.parent() new_name = Urn(new_name).filename() new_path = "{directory}{filename}".format(directory=parent_path, filename=new_name) self.client.move(remote_path_from=old_path, remote_path_to=new_path) self.urn = Urn(new_path) def move(self, remote_path): new_urn = Urn(remote_path) self.client.move(remote_path_from=self.urn.path(), remote_path_to=new_urn.path()) self.urn = new_urn def copy(self, remote_path): urn = Urn(remote_path) self.client.copy(remote_path_from=self.urn.path(), remote_path_to=remote_path) return Resource(self.client, urn) def info(self, params=None): info = self.client.info(self.urn.path()) if not params: return info return {key: value for (key, value) in info.items() if key in params} def clean(self): return self.client.clean(self.urn.path()) def check(self): return self.client.check(self.urn.path()) def read_from(self, buff): self.client.upload_from(buff=buff, remote_path=self.urn.path()) def read(self, local_path): return self.client.upload_sync(local_path=local_path, remote_path=self.urn.path()) def read_async(self, local_path, callback=None): return self.client.upload_async(local_path=local_path, remote_path=self.urn.path(), callback=callback) def write_to(self, buff): return self.client.download_to(buff=buff, remote_path=self.urn.path()) def write(self, local_path): return self.client.download_sync(local_path=local_path, remote_path=self.urn.path()) def write_async(self, local_path, callback=None): return self.client.download_async(local_path=local_path, remote_path=self.urn.path(), callback=callback) def publish(self): return self.client.publish(self.urn.path()) def unpublish(self): return self.client.unpublish(self.urn.path()) @property def property(self, option): return self.client.get_property(remote_path=self.urn.path(), option=option) @property.setter def property(self, option, value): option['value'] = value.__str__() self.client.set_property(remote_path=self.urn.path(), option=option)