예제 #1
0
def test_plant(tmp_folder):
    root = Root.plant(tmp_folder, 'my_root')
    assert root.key() == 'my_root'
    file = root._file
    dic = file.read()
    assert dic['_root']['atom'].key == 'my_root'
    assert file.folder().name() == 'my_root'

    root = Root(tmp_folder.append('my_root'))
    assert root.key() == 'my_root'
예제 #2
0
def test_roots(tmp_folder, tmp_files):
    ground = Ground.settle(tmp_folder, 'test_ground')
    r1 = Root.plant(ground, 'root_1')
    f1 = Folder.join(ground, 'folder_1')
    r2 = Root.plant(f1, 'root_2')
    f2 = Folder.join(f1, 'folder_2')
    r3 = Root.plant(f2, 'root_3')
    assert len(ground.list()) == 5
    for file in tmp_files:
        file.copy(f1).copy(f2)
        file.copy(r1.folder()).copy(r2.folder()).copy(r3.folder())
    roots = ground.roots()
    assert len(roots) == 3
예제 #3
0
def test_kwargs(tmp_folder):
    root = Root.plant(tmp_folder,
                      'my_root',
                      one_argument='hey',
                      another_argument=5.5)
    assert root.key() == 'my_root'
    file = root._file
    dic = file.read()
    assert dic['one_argument'] == 'hey'
    assert dic['another_argument'] == 5.5

    root = Root(tmp_folder.append('my_root'))
    assert root.one_argument == 'hey'
    assert root.another_argument == 5.5
예제 #4
0
def test_find_all(tmp_folder):
    root_names = [f'root_{x}' for x in range(10)]
    subfolder1 = tmp_folder.append('sub1')
    subfolder2 = subfolder1.append('sub2')

    for root_name in root_names:
        Root.plant(tmp_folder, root_name)
        Root.plant(subfolder1, root_name)
        Root.plant(subfolder2, root_name)

    roots = Root.find_all(tmp_folder)
    assert 3 * len(root_names) == len(roots)
    roots = Root.find_all((subfolder1))
    assert 2 * len(root_names) == len(roots)
    roots = Root.find_all((subfolder2))
    assert len(root_names) == len(roots)
예제 #5
0
    def roots(self):
        # TODO: Add max depth of search
        # TODO: Add specific folders option (if implemented)
        """
        Find all roots recursively from ground folder

        :return: List of all roots
        :rtype: [Root]
        """
        roots = Root.find_all(Folder(str(self)))
        return [x for x in roots if x.key != self._CAVE]
예제 #6
0
def test_elf(tmp_folder):
    folder = tmp_folder.append('not_root_yet')
    folder.create()
    root = Root.elf(folder)
    assert root.key() == 'not_root_yet'
    root._root.atom.key = 'other_root'
    with pytest.raises(RootError):
        root.write()
    file = root._file
    dic = dict()
    dic['key'] = 'other_root'
    dic['identity'] = root._root.atom.identity
    dic['zulu'] = root._root.atom.zulu
    dic2 = dict()
    dic2['_root'] = dict()
    dic2['_root']['atom'] = dic
    dic2['_root']['species'] = 'root'
    file.write(dic2)
    with pytest.raises(RootError):
        Root.elf(folder)
예제 #7
0
    def settle(cls, folder, key):
        """
        Create new ground in the specified folder path, with the given key.

        :param folder: Folder to settle
        :type folder: Folder
        :param key: Ground key
        :type key: Key
        :return: Settled ground
        :rtype: Ground
        """
        folder = Folder.elf(folder)
        if not folder.exists():
            raise GroundProblem(f'Cannot settle ground in non existent '
                                f'path: {folder}')
        if cls.is_in(folder):
            raise GroundProblem(f'This folder has already been '
                                f'settled: {folder}')

        key = Key.elf(key)
        ground_folder = cls._ground_folder(folder, key)
        ground_folder.create()
        Root.plant(ground_folder, 'cave', ground_key=key)
        return cls(folder)
예제 #8
0
def test_uproot(tmp_folder):
    root = Root.plant(tmp_folder, 'my_root')
    root.uproot()
    root = Root.plant(tmp_folder, 'my_root')
    f = root.folder()
    file = File.join(f, 'one_file.txt')
    file.write('Some contents')
    ff = f.append('one_folder')
    file2 = File.join(ff, 'two_file.txt')
    file2.write('Some other contents')
    with pytest.raises(RootError):
        root.uproot()
    with pytest.raises(RootError):
        root.uproot(with_force=True)
    root.uproot(with_force=True, key=root.key())
    Root.plant(tmp_folder, 'my_root')
    root = Root(tmp_folder.append('my_root'))
    assert root.key() == 'my_root'
예제 #9
0
 def __init__(self, folder):
     super(Ground, self).__init__()
     key = self._find_ground(folder)
     ground_folder = self._ground_folder(self, key)
     cave_folder = Folder.join(ground_folder, self._CAVE)
     cave = Root(cave_folder)
     if not cave.key() == self._CAVE:
         raise NoGround(f'Invalid key in cave: {cave.key()}')
     try:
         ground_key = cave.dic()[self._GROUND_KEY]
         if not ground_key == key:
             raise NoGround(f'Ground key mismatch. '
                            f'Expected from folder name: {key}. '
                            f'Found in cave: {ground_key}')
         self.key = Key.elf(ground_key)
     except KeyError:
         raise NoGround(f'Ground key not found in cave')
     self.zulu = cave.zulu()
     self.identity = cave.identity()
     self.cave = cave.folder()