예제 #1
0
def test_empty_file():

    # Create fake files.
    os.mkdir('tmp')
    with open('tmp/empty.csv', 'w+') as f:
        pass
    Table([['1'], ['a']]).to_csv('tmp/full.csv')

    assert files.has_data('tmp/empty.csv') == False
    assert files.has_data('tmp/full.csv') == True

    # Remove fake files and dir
    shutil.rmtree('tmp')
예제 #2
0
    def from_csv(cls, local_path, **csvargs):
        """
        Create a ``parsons table`` object from a CSV file

        `Args:`
            local_path: obj
                A csv formatted local path, url or ftp. If this is a
                file path that ends in ".gz", the file will be decompressed first.
            \**csvargs: kwargs
                ``csv_reader`` optional arguments
        `Returns:`
            Parsons Table
                See :ref:`parsons-table` for output options.
        """  # noqa: W605

        remote_prefixes = ["http://", "https://", "ftp://", "s3://"]
        if any(map(local_path.startswith, remote_prefixes)):
            is_remote_file = True
        else:
            is_remote_file = False

        if not is_remote_file and not files.has_data(local_path):
            raise ValueError('CSV file is empty')

        return cls(petl.fromcsv(local_path, **csvargs))
예제 #3
0
def test_create_temp_directory():
    temp_directory = files.create_temp_directory()
    test_file1 = f'{temp_directory}/test.txt'
    test_file2 = f'{temp_directory}/test2.txt'
    with open(test_file1, 'w') as fh1, open(test_file2, 'w') as fh2:
        fh1.write('TEST')
        fh2.write('TEST')

    assert files.has_data(test_file1)
    assert files.has_data(test_file2)

    files.cleanup_temp_directory(temp_directory)

    # Verify the temp file no longer exists
    with pytest.raises(FileNotFoundError):
        open(test_file1, 'r')
예제 #4
0
파일: tofrom.py 프로젝트: eliotst/parsons
    def from_csv(cls, local_path, **csvargs):
        """
        Create a ``parsons table`` object from a CSV file

        `Args:`
            local_path: obj
                A csv formatted local path, url or ftp. If this is a
                file path that ends in ".gz", the file will be decompressed first.
            \**csvargs: kwargs
                ``csv_reader`` optional arguments
        `Returns:`
            Parsons Table
                See :ref:`parsons-table` for output options.
        """  # noqa: W605

        if not files.has_data(local_path):
            raise ValueError('CSV file is empty')

        return cls(petl.fromcsv(local_path, **csvargs))