Exemple #1
0
    def find_all(cls, folder):
        """
        Find all roots in folder

        :param folder: Folder to search
        :type folder: Folder
        :return: List of roots found in folder
        :rtype: [Root]
        """
        path = Path.elf(folder)
        paths = path.glob(cls._FILE_WILDCARD, recursive=True)
        roots = []
        for path in paths:
            if path.is_file():
                file = File.elf(path)
                folder = file.folder()
                if file.stub() == folder.name():
                    if cls.is_root(folder):
                        cls._append_if_root(roots, folder)

        return roots
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