Exemple #1
0
    def leaf(self, atom):
        """
        Get the leaf defined by the given atom

        :param atom: Atom defining the leaf contents
        :type atom: Atom
        :return: Leaf defined by the atom
        :rtype: Leaf
        """
        atom = Atom.elf(atom)
        folder = self._branch(atom)
        file_name = File.make_file_name(str(atom),
                                        self.extension,
                                        is_compressed=self.compress_all,
                                        is_encrypted=self.encrypt_all)
        file = File.join(folder, file_name)
        return Leaf(file)
Exemple #2
0
    def grow(self,
             native,
             atom=None,
             force_extension=False,
             delete_native=False):
        """
        Add a file to the tree. A file outside of the tree is called ``native``
        while a file in the tree is called ``leaf``

        :param native: File to add to the tree
        :type native: File
        :param atom: Atom defining the contents of the file, if the file name
            is not a valid atom string
        :type atom: Atom
        :param force_extension: Flags forcing file extension to be the
            extension inherent in tree
        :type force_extension: bool
        :param delete_native: Flags whether to delete the native file after
            file has been copied to the tree structure
        :type delete_native: bool
        :return: New leaf in tree
        :rtype: Leaf
        """
        native = File.elf(native)
        if not native.exists():
            raise TreeError(f'Cannot grow Leaf from non existent '
                            f'path: {native}')
        if not native.is_file():
            raise TreeError(f'Native is not a file: {native}')
        if not force_extension and len(native.extensions()) != 1:
            raise TreeError(f'Leaves must have one extension, this native '
                            f'has none or multiple: {native.extensions()}. '
                            f'Use force_extension to set it to '
                            f'tree inherent: \'{self.extension}\'')

        if not atom:
            try:
                atom = Atom(native.stub())
            except AtomError as se:
                raise TreeError(f'File name is not a valid '
                                f'atom: {native.name()}. '
                                f'Add atom as parameter to override '
                                f'file name.') from se

        if force_extension:
            extension = self.extension
        else:
            extension = native.extension()

        new_name = File.make_file_name(str(atom),
                                       extension,
                                       is_compressed=native.is_compressed(),
                                       is_encrypted=native.is_encrypted())

        if native.name() == new_name:
            new_name = None

        folder = self._branch(atom)
        if delete_native:
            file = native.move(folder, new_name)
        else:
            file = native.copy(folder, new_name)
        leaf = Leaf(file)
        logger.debug(f'Added leaf: {leaf}')
        leaf = self._shape(leaf)
        return leaf