Exemple #1
0
 def __init__(self, folder):
     self._root = None
     self._folder = Folder.elf(folder)
     if not self._folder.exists():
         raise NotRootException(f'Folder does not exists: {self._folder}')
     self._file = File.join(self._folder, self._file_name(self._folder))
     if not self._file.exists():
         raise NotRootException(f'Description file does '
                                f'not exist: {self._file}')
     self.read()
     if self.key() != self._folder.name():
         raise NotRootException(f'Key/folder mismatch. '
                                f'key={self._root.key}, '
                                f'folder={self._folder}, '
                                f'file={self._file}')
Exemple #2
0
    def elf(cls, folder, **kwargs):
        """
        Converts an existing folder to root, tolerating missing file but not
        invalid key. If a folder already is a valid root, it will be returned
        as root. The folder name must be a valid :class:`.Key`

        :raise RootError: If file already exists, but does not have the right
            key, or if it has an invalid format
        :param folder: Folder to convert to root
        :type folder: Folder
        :param kwargs: Attributes to add to root file
        :return: New or existing root
        :rtype: Root
        """
        if cls.is_root(folder):
            return cls(folder)

        folder = Folder.elf(folder)
        file = File.join(folder, cls._file_name(folder))

        if file.exists():
            try:
                dic = file.read()
                if isinstance(dic['_root'], Atom):
                    key = dic['_root'].key
                    if key == folder.name():
                        raise RootError(f'This seems to be a valid root, but '
                                        f'is_root said otherwise. Contact '
                                        f'the rather incompetent developer of '
                                        f'this module.')
                    else:
                        raise RootError(f'Root file already exists, but has '
                                        f'the wrong key. '
                                        f'Expected: key={folder.name()}, '
                                        f'Found: key={key}')
                else:
                    raise RootError(f'Root file already exists, but has '
                                    f'invalid format. Should be atom, but '
                                    f'contents is instead: {dic}')
            except FileError as fe:
                raise RootError(f'Root file exists, does not seem to be a '
                                f'valid JSON file. '
                                f'Original error from read: {fe}')
        else:
            folder.touch()
            file.write(cls._dic(folder, **kwargs))

        return cls(folder)
Exemple #3
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 #4
0
 def _get_file(cls, folder):
     return File.join(folder, cls._file_name(folder))