Exemplo n.º 1
0
    def makedir(self, dirname, recursive=False, allow_recreate=False):
        if not dirname and not allow_recreate:
            raise PathError(dirname)
        fullpath = normpath(dirname)
        if fullpath in ('', '/'):
            if allow_recreate:
                return
            raise DestinationExistsError(dirname)
        dirpath, dirname = pathsplit(dirname.rstrip('/'))

        if recursive:
            parent_dir = self._get_dir_entry(dirpath)
            if parent_dir is not None:
                if parent_dir.isfile():
                    raise ResourceInvalidError(dirname, msg="Can not create a directory, because path references a file: %(path)s")
                else:
                    if not allow_recreate:
                        if dirname in parent_dir.contents:
                            raise DestinationExistsError(dirname, msg="Can not create a directory that already exists (try allow_recreate=True): %(path)s")

            current_dir = self.root
            for path_component in iteratepath(dirpath)[:-1]:
                dir_item = current_dir.contents.get(path_component, None)
                if dir_item is None:
                    break
                if not dir_item.isdir():
                    raise ResourceInvalidError(dirname, msg="Can not create a directory, because path references a file: %(path)s")
                current_dir = dir_item

            current_dir = self.root
            for path_component in iteratepath(dirpath):
                dir_item = current_dir.contents.get(path_component, None)
                if dir_item is None:
                    new_dir = self._make_dir_entry("dir", path_component)
                    current_dir.contents[path_component] = new_dir
                    current_dir = new_dir
                else:
                    current_dir = dir_item

            parent_dir = current_dir

        else:
            parent_dir = self._get_dir_entry(dirpath)
            if parent_dir is None:
                raise ParentDirectoryMissingError(dirname, msg="Could not make dir, as parent dir does not exist: %(path)s")

        dir_item = parent_dir.contents.get(dirname, None)
        if dir_item is not None:
            if dir_item.isdir():
                if not allow_recreate:
                    raise DestinationExistsError(dirname)
            else:
                raise ResourceInvalidError(dirname, msg="Can not create a directory, because path references a file: %(path)s")

        if dir_item is None:
            parent_dir.contents[dirname] = self._make_dir_entry("dir", dirname)
Exemplo n.º 2
0
 def _get_dir_entry(self, dirpath):
     current_dir = self.root
     for path_component in iteratepath(dirpath):
         if current_dir.contents is None:
             return None
         dir_entry = current_dir.contents.get(path_component, None)
         if dir_entry is None:
             return None
         current_dir = dir_entry
     return current_dir
Exemplo n.º 3
0
 def _get_dir_entry(self, dir_path):
     """
     Returns a DirEntry for a specified path 'dir_path'
     """
     dir_path = normpath(dir_path)
     current_dir = self.root
     for path_component in iteratepath(dir_path):
         dir_entry = current_dir.contents.get(path_component, None)
         if dir_entry is not None:
             return dir_entry
         current_dir = dir_entry
     return current_dir
Exemplo n.º 4
0
    def _get_item_by_path(self, path):
        """Returns the item at given path.

        :param path:
            The normalized path of the item.
        :type path:
            `str`

        :returns:
            The item as returned by the API call. Example Response:
                {
                    "type":"file",
                    "id":"2305649799",
                    "sequence_id":"1",
                    "name":"testing.html"
                }
        :rtype:
            `dict`
        """
        if path == '/':
            return {
                'type': _ITEM_TYPE_FOLDER,
                'id': self._root_id,
                # TODO(kunal): find correct value for this field.
                'sequence_id': '1',
                'name': path,
            }

        parent_box_id = self._root_id
        parent_path, item_name = pathsplit(path)
        for name in iteratepath(parent_path):
            items = self._get_children_items(parent_box_id)
            item = items.get(name)
            if not item or item['type'] != _ITEM_TYPE_FOLDER:
                raise ParentDirectoryMissingError(path)

            parent_box_id = item['id']

        items = self._get_children_items(parent_box_id)
        return items.get(item_name)
Exemplo n.º 5
0
    def makedir(self, path, recursive=False, allow_recreate=False):
        if not path and not allow_recreate:
            raise PathError(path)

        path = normpath(path)
        if path in ('', '/'):
            if allow_recreate:
                return
            raise DestinationExistsError(path)

        parent_path, dirname = pathsplit(path)
        parent_box_id = self._root_id
        for name in iteratepath(parent_path):
            children_items = self._get_children_items(parent_box_id)
            child_item = children_items.get(name)
            if not child_item:
                if recursive:
                    child_item = self._makedir(parent_box_id, name)
                else:
                    raise ParentDirectoryMissingError(path)

            if child_item['type'] != _ITEM_TYPE_FOLDER:
                raise ResourceInvalidError(path)

            parent_box_id = child_item['id']

        # Check if an item with required name already exists.
        children_items = self._get_children_items(parent_box_id)
        child_item = children_items.get(dirname)
        if child_item:
            if allow_recreate and child_item['type'] == _ITEM_TYPE_FOLDER:
                return
            else:
                raise DestinationExistsError(path)

        self._makedir(parent_box_id, dirname)
Exemplo n.º 6
0
 def _should_hide(self, path):
     return any(any(wildcard.match(part) for wildcard in self._hide_wildcards) for part in iteratepath(path))
Exemplo n.º 7
0
 def _should_hide(self, path):
     return any(
         any(wildcard.match(part) for wildcard in self._hide_wildcards)
         for part in iteratepath(path))
Exemplo n.º 8
0
    def makedir(self, dirname, recursive=False, allow_recreate=False):
        if not dirname and not allow_recreate:
            raise PathError(dirname)
        fullpath = normpath(dirname)
        if fullpath in ('', '/'):
            if allow_recreate:
                return
            raise DestinationExistsError(dirname)
        dirpath, dirname = pathsplit(dirname.rstrip('/'))

        if recursive:
            parent_dir = self._get_dir_entry(dirpath)
            if parent_dir is not None:
                if parent_dir.isfile():
                    raise ResourceInvalidError(
                        dirname,
                        msg=
                        "Can not create a directory, because path references a file: %(path)s"
                    )
                else:
                    if not allow_recreate:
                        if dirname in parent_dir.contents:
                            raise DestinationExistsError(
                                dirname,
                                msg=
                                "Can not create a directory that already exists (try allow_recreate=True): %(path)s"
                            )

            current_dir = self.root
            for path_component in iteratepath(dirpath)[:-1]:
                dir_item = current_dir.contents.get(path_component, None)
                if dir_item is None:
                    break
                if not dir_item.isdir():
                    raise ResourceInvalidError(
                        dirname,
                        msg=
                        "Can not create a directory, because path references a file: %(path)s"
                    )
                current_dir = dir_item

            current_dir = self.root
            for path_component in iteratepath(dirpath):
                dir_item = current_dir.contents.get(path_component, None)
                if dir_item is None:
                    new_dir = self._make_dir_entry("dir", path_component)
                    current_dir.contents[path_component] = new_dir
                    current_dir = new_dir
                else:
                    current_dir = dir_item

            parent_dir = current_dir

        else:
            parent_dir = self._get_dir_entry(dirpath)
            if parent_dir is None:
                raise ParentDirectoryMissingError(
                    dirname,
                    msg=
                    "Could not make dir, as parent dir does not exist: %(path)s"
                )

        dir_item = parent_dir.contents.get(dirname, None)
        if dir_item is not None:
            if dir_item.isdir():
                if not allow_recreate:
                    raise DestinationExistsError(dirname)
            else:
                raise ResourceInvalidError(
                    dirname,
                    msg=
                    "Can not create a directory, because path references a file: %(path)s"
                )

        if dir_item is None:
            parent_dir.contents[dirname] = self._make_dir_entry("dir", dirname)
Exemplo n.º 9
0
 def parts(self) -> Tuple[str]:
     result = list(fp.iteratepath(self.s))
     if self.is_absolute():
         result = ['/'] + result
     return tuple(result)