Beispiel #1
0
    def create_subdirectory(self,
                            namex,
                            initial_children={},
                            overwrite=True,
                            mutable=True,
                            mutable_version=None,
                            metadata=None):
        name = normalize(namex)
        if self.is_readonly():
            return defer.fail(NotWriteableError())
        if mutable:
            if mutable_version:
                d = self._nodemaker.create_new_mutable_directory(
                    initial_children, version=mutable_version)
            else:
                d = self._nodemaker.create_new_mutable_directory(
                    initial_children)
        else:
            # mutable version doesn't make sense for immmutable directories.
            assert mutable_version is None
            d = self._nodemaker.create_immutable_directory(initial_children)

        def _created(child):
            entries = {name: (child, metadata)}
            a = Adder(self,
                      entries,
                      overwrite=overwrite,
                      create_readonly_node=self._create_readonly_node)
            d = self._node.modify(a.modify)
            d.addCallback(lambda res: child)
            return d

        d.addCallback(_created)
        return d
Beispiel #2
0
    def move_child_to(self, current_child_namex, new_parent,
                      new_child_namex=None, overwrite=True):
        """
        I take one of my child links and move it to a new parent. The child
        link is referenced by name. In the new parent, the child link will live
        at 'new_child_namex', which defaults to 'current_child_namex'. I return
        a Deferred that fires when the operation finishes.
        'new_child_namex' and 'current_child_namex' need not be normalized.

        The overwrite parameter may be True (overwrite any existing child),
        False (error if the new child link already exists), or "only-files"
        (error if the new child link exists and points to a directory).
        """
        if self.is_readonly() or new_parent.is_readonly():
            return defer.fail(NotWriteableError())

        current_child_name = normalize(current_child_namex)
        if new_child_namex is None:
            new_child_name = current_child_name
        else:
            new_child_name = normalize(new_child_namex)

        from_uri = self.get_write_uri()
        if new_parent.get_write_uri() == from_uri and new_child_name == current_child_name:
            # needed for correctness, otherwise we would delete the child
            return defer.succeed("redundant rename/relink")

        d = self.get_child_and_metadata(current_child_name)
        def _got_child(child_and_metadata):
            (child, metadata) = child_and_metadata
            return new_parent.set_node(new_child_name, child, metadata,
                                       overwrite=overwrite)
        d.addCallback(_got_child)
        d.addCallback(lambda child: self.delete(current_child_name))
        return d
Beispiel #3
0
    def add_file(self,
                 namex,
                 uploadable,
                 metadata=None,
                 overwrite=True,
                 progress=None):
        """I upload a file (using the given IUploadable), then attach the
        resulting FileNode to the directory at the given name. I return a
        Deferred that fires (with the IFileNode of the uploaded file) when
        the operation completes."""
        with ADD_FILE(name=namex, metadata=metadata,
                      overwrite=overwrite).context():
            name = normalize(namex)
            if self.is_readonly():
                d = DeferredContext(defer.fail(NotWriteableError()))
            else:
                # XXX should pass reactor arg
                d = DeferredContext(
                    self._uploader.upload(uploadable, progress=progress))
                d.addCallback(lambda results: self._create_and_validate_node(
                    results.get_uri(), None, name))
                d.addCallback(lambda node: self.set_node(
                    name, node, metadata, overwrite))

        return d.addActionFinish()
Beispiel #4
0
    def move_child_to(self,
                      current_child_namex,
                      new_parent,
                      new_child_namex=None,
                      overwrite=True):
        """I take one of my children and move them to a new parent. The child
        is referenced by name. On the new parent, the child will live under
        'new_child_name', which defaults to 'current_child_name'. I return a
        Deferred that fires when the operation finishes."""

        if self.is_readonly() or new_parent.is_readonly():
            return defer.fail(NotWriteableError())

        current_child_name = normalize(current_child_namex)
        if new_child_namex is None:
            new_child_namex = current_child_name
        d = self.get(current_child_name)

        def sn(child):
            return new_parent.set_node(new_child_namex,
                                       child,
                                       overwrite=overwrite)

        d.addCallback(sn)
        d.addCallback(lambda child: self.delete(current_child_name))
        return d
Beispiel #5
0
 def set_nodes(self, entries, overwrite=True):
     precondition(isinstance(entries, dict), entries)
     if self.is_readonly():
         return defer.fail(NotWriteableError())
     a = Adder(self, entries, overwrite=overwrite,
               create_readonly_node=self._create_readonly_node)
     d = self._node.modify(a.modify)
     d.addCallback(lambda res: self)
     return d
Beispiel #6
0
 def delete(self, namex, must_exist=True, must_be_directory=False, must_be_file=False):
     """I remove the child at the specific name. I return a Deferred that
     fires (with the node just removed) when the operation finishes."""
     if self.is_readonly():
         return defer.fail(NotWriteableError())
     deleter = Deleter(self, namex, must_exist=must_exist,
                       must_be_directory=must_be_directory, must_be_file=must_be_file)
     d = self._node.modify(deleter.modify)
     d.addCallback(lambda res: deleter.old_child)
     return d
Beispiel #7
0
 def set_metadata_for(self, namex, metadata):
     name = normalize(namex)
     if self.is_readonly():
         return defer.fail(NotWriteableError())
     assert isinstance(metadata, dict)
     s = MetadataSetter(self, name, metadata,
                        create_readonly_node=self._create_readonly_node)
     d = self._node.modify(s.modify)
     d.addCallback(lambda res: self)
     return d
Beispiel #8
0
 def add_file(self, namex, uploadable, metadata=None, overwrite=True):
     """I upload a file (using the given IUploadable), then attach the
     resulting FileNode to the directory at the given name. I return a
     Deferred that fires (with the IFileNode of the uploaded file) when
     the operation completes."""
     name = normalize(namex)
     if self.is_readonly():
         return defer.fail(NotWriteableError())
     d = self._uploader.upload(uploadable)
     d.addCallback(lambda results: self._create_and_validate_node(
         results.get_uri(), None, name))
     d.addCallback(
         lambda node: self.set_node(name, node, metadata, overwrite))
     return d
Beispiel #9
0
    def set_node(self, namex, child, metadata=None, overwrite=True):
        """I add a child at the specific name. I return a Deferred that fires
        when the operation finishes. This Deferred will fire with the child
        node that was just added. I will replace any existing child of the
        same name.

        If this directory node is read-only, the Deferred will errback with a
        NotWriteableError."""

        precondition(IFilesystemNode.providedBy(child), child)

        if self.is_readonly():
            return defer.fail(NotWriteableError())
        assert IFilesystemNode.providedBy(child), child
        a = Adder(self, overwrite=overwrite,
                  create_readonly_node=self._create_readonly_node)
        a.set_node(namex, child, metadata)
        d = self._node.modify(a.modify)
        d.addCallback(lambda res: child)
        return d