コード例 #1
0
    def test_listdir(self, mock_labbook, sample_src_file):
        def write_test_file(base, name):
            with open(os.path.join(base, name), 'wt') as f:
                f.write("Blah blah")

        lb = mock_labbook[2]
        dirs = ["code/new_dir", ".hidden_dir"]
        for d in dirs:
            FO.makedir(lb, d)
        write_test_file(lb.root_dir, 'test1.txt')
        write_test_file(lb.root_dir, 'test2.txt')
        write_test_file(lb.root_dir, '.hidden.txt')
        write_test_file(lb.root_dir, 'code/test_subdir1.txt')
        write_test_file(lb.root_dir, 'code/test_subdir2.txt')
        write_test_file(lb.root_dir, 'code/new_dir/tester.txt')

        # List just the code dir
        data = FO.listdir(lb, "code", base_path='')
        assert len(data) == 3
        assert data[0]['key'] == 'new_dir/'
        assert data[1]['key'] == 'test_subdir1.txt'
        assert data[2]['key'] == 'test_subdir2.txt'

        data = FO.listdir(lb, "input", base_path='')
        assert len(data) == 0

        # List just the code/subdir dir
        data = FO.listdir(lb, "code", base_path='new_dir')
        assert len(data) == 1
        assert data[0]['key'] == 'new_dir/tester.txt'
コード例 #2
0
    def helper_resolve_files(self, labbook, kwargs):
        """Helper method to populate the LabbookFileConnection"""
        base_dir = None
        if 'root_dir' in kwargs:
            if kwargs['root_dir']:
                base_dir = kwargs['root_dir'] + os.path.sep
                base_dir = base_dir.replace(os.path.sep + os.path.sep,
                                            os.path.sep)

        # Get all files and directories, with the exception of anything in .git or .gigantum
        edges = FileOperations.listdir(labbook,
                                       self.section,
                                       base_path=base_dir,
                                       show_hidden=False)

        # Generate naive cursors
        cursors = [
            base64.b64encode("{}".format(cnt).encode("UTF-8")).decode("UTF-8")
            for cnt, x in enumerate(edges)
        ]

        # Process slicing and cursor args
        lbc = ListBasedConnection(edges, cursors, kwargs)
        lbc.apply()

        edge_objs = []
        for edge, cursor in zip(lbc.edges, lbc.cursors):
            create_data = {
                "owner": self.owner,
                "section": self.section,
                "name": self.name,
                "key": edge['key'],
                "_file_info": edge
            }
            edge_objs.append(
                LabbookFileConnection.Edge(node=LabbookFile(**create_data),
                                           cursor=cursor))

        return LabbookFileConnection(edges=edge_objs, page_info=lbc.page_info)
コード例 #3
0
 def test_listdir_expect_error(self, mock_labbook, sample_src_file):
     lb = mock_labbook[2]
     with pytest.raises(ValueError):
         FO.listdir(lb, "code", base_path='blah')