예제 #1
0
def test_all_formats(mock_find_executable):
    mock_find_executable.return_value = True
    csvw = CSVW(csv_path="./tests/books.csv",
                metadata_path="./tests/books.csv-metadata.json")
    assert mock_find_executable.call_count == 0
    # Create file like objects
    input_val = []
    for f in TEST_PARAMS:
        input_val.append((tempfile.TemporaryFile(), f[0]))
    csvw.to_rdf_files(input_val)
    # Check each output
    for fmt in zip(input_val, TEST_PARAMS):
        file_obj = fmt[0][0]
        validate_func = fmt[1][1]
        rdflib_input = fmt[1][2]
        # Rewind and read
        file_obj.seek(0)
        contents = file_obj.read().decode('utf-8')
        # Validate
        validate_func(contents)
        verify_rdf_contents(contents, rdflib_input)

    assert mock_find_executable.call_count == 1
예제 #2
0
def process_csv(csv_file, metadata_file, riotpath, g = None):
    # Generate RDF from a CSV file and metadata file pair, 
    # csv_file is a path string
    # metadata-file is a path string path optionally merging it into a Graph

    if g is None:
        g = Graph()

    csvw = CSVW(csv_path = csv_file, metadata_path = metadata_file, riot_path = riotpath)

    with NamedTemporaryFile() as f:
        rdf_output = csvw.to_rdf_files([ (f,'turtle') ])
        f.seek(0)
        g.parse(f, format = 'ttl')

    csvw.close()

    return g