Exemplo n.º 1
0
    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)
Exemplo n.º 2
0
    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)
Exemplo n.º 3
0
    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)
Exemplo n.º 4
0
    def get_property(self, remote_path, option):
        def parse(response, option):

            response_str = response.content
            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()

        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['get_metadata'],
            'HTTPHEADER': self.get_header('get_metadata'),
            'POSTFIELDS': data(option),
            'NOBODY': 0
        }

        response = requests.request(options["CUSTOMREQUEST"],
                                    options["URL"],
                                    auth=(self.webdav.login,
                                          self.webdav.password),
                                    headers=self.get_header('get_metadata'),
                                    data=data(option))
        return parse(response, option)
Exemplo n.º 5
0
        def header(remote_path_to):

            path = Urn(remote_path_to).path()
            destination = "{root}{path}".format(root=self.webdav.root,
                                                path=path)
            header = self.get_header('copy')
            header["Destination"] = destination

            return header
Exemplo n.º 6
0
        def header(remote_path_to):

            path = Urn(remote_path_to).path()
            destination = "{root}{path}".format(root=self.webdav.root,
                                                path=path)
            header = self.get_header('move')
            header["Destination"] = destination
            header["Overwrite"] = "T" if overwrite else "F"
            return header
Exemplo n.º 7
0
    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)
Exemplo n.º 8
0
    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()

        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)
        }

        response = requests.request(options["CUSTOMREQUEST"],
                                    options["URL"],
                                    auth=(self.webdav.login,
                                          self.webdav.password),
                                    headers=self.get_header('get_metadata'),
                                    data=data(option))
Exemplo n.º 9
0
        def parse(response):

            try:
                response_str = response.content
                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()
Exemplo n.º 10
0
    def clean(self, remote_path):

        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')
        }

        response = requests.request(
            options["CUSTOMREQUEST"],
            options["URL"],
            auth=(self.webdav.login, self.webdav.password),
            headers=self.get_header('clean'),
        )
Exemplo n.º 11
0
    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)
Exemplo n.º 12
0
    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)
Exemplo n.º 13
0
    def upload_from(self, buff, remote_path):

        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),
        }
        # import ipdb; ipdb.set_trace();
        #FIXME
        if buff.tell() == 0:
            data = buff.read()
        else:
            data = buff
        response = requests.request('PUT',
                                    options["URL"],
                                    auth=(self.webdav.login,
                                          self.webdav.password),
                                    headers=self.get_header('upload_from'),
                                    data=data)
        if response.status_code == 507:
            raise NotEnoughSpace()
        log.debug("Response: %s", response)
Exemplo n.º 14
0
    def download_file(self, remote_path, local_path):

        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,
                'NOBODY': 0
            }

            response = requests.request(
                "GET",
                options["URL"],
                auth=(self.webdav.login, self.webdav.password),
                headers=self.get_header('download_file'))
            for block in response.iter_content(1024):
                local_file.write(block)
Exemplo n.º 15
0
    def mkdir(self, remote_path):

        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'],
        }

        response = requests.request(options["CUSTOMREQUEST"],
                                    options["URL"],
                                    auth=(self.webdav.login,
                                          self.webdav.password),
                                    headers=self.get_header('mkdir'))
Exemplo n.º 16
0
    def check(self, remote_path=root):
        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'],
        }

        response = requests.request(options["CUSTOMREQUEST"],
                                    options["URL"],
                                    auth=(self.webdav.login,
                                          self.webdav.password),
                                    headers=self.get_header('check'))

        if int(response.status_code) == 200:
            return True
        return False
Exemplo n.º 17
0
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)
Exemplo n.º 18
0
    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 = self.get_header('copy')
            header["Destination"] = destination

            return header

        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)
        }

        h = header(remote_path_to)
        response = requests.request(
            options["CUSTOMREQUEST"],
            options["URL"],
            auth=(self.webdav.login, self.webdav.password),
            headers=h,
        )
Exemplo n.º 19
0
    def list(self, remote_path=root):
        def parse(response):

            try:
                response_str = response.content
                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()

        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())

        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'),
            'NOBODY': 0
        }

        response = requests.request(options["CUSTOMREQUEST"],
                                    options["URL"],
                                    auth=(self.webdav.login,
                                          self.webdav.password),
                                    headers=self.get_header('list'))

        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]
        ]
Exemplo n.º 20
0
    def upload_file(self, remote_path, local_path, progress=None):

        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

            response = requests.request('PUT',
                                        options["URL"],
                                        auth=(self.webdav.login,
                                              self.webdav.password),
                                        headers=self.get_header('upload_file'),
                                        data=buff)
            if request.status_code == 507:
                raise NotEnoughSpace()
            if response.status_code == 409:
                raise Conflict()
Exemplo n.º 21
0
    def download_to(self, buff, remote_path):
        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),
        }

        response = requests.request("GET",
                                    options["URL"],
                                    auth=(self.webdav.login,
                                          self.webdav.password),
                                    headers=self.get_header('download_to'))
        buff.write(response.content)
Exemplo n.º 22
0
    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)
Exemplo n.º 23
0
    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
Exemplo n.º 24
0
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)
Exemplo n.º 25
0
    def info(self, remote_path):
        def parse(response, path):

            try:
                response_str = response.content
                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)

        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'],
        }

        response = requests.request(options["CUSTOMREQUEST"],
                                    options["URL"],
                                    auth=(self.webdav.login,
                                          self.webdav.password),
                                    headers=self.get_header('info'))
        path = "{root}{path}".format(root=self.webdav.root, path=urn.path())
        return parse(response, path)
Exemplo n.º 26
0
    def is_dir(self, remote_path):
        def parse(response, path):

            try:
                response_str = response.content
                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)

        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'],
        }

        response = requests.request(options["CUSTOMREQUEST"],
                                    options["URL"],
                                    auth=(self.webdav.login,
                                          self.webdav.password),
                                    headers=self.get_header('info'))

        path = "{root}{path}".format(root=self.webdav.root, path=urn.path())

        return parse(response, path)
Exemplo n.º 27
0
 def resource(self, remote_path):
     urn = Urn(remote_path)
     return Resource(self, urn.path())