diff --git a/case_utils/case_sparql_select/__init__.py b/case_utils/case_sparql_select/__init__.py index 424848c..eaa98cb 100644 --- a/case_utils/case_sparql_select/__init__.py +++ b/case_utils/case_sparql_select/__init__.py @@ -33,6 +33,7 @@ import logging import os import sys +import typing import pandas as pd # type: ignore import rdflib.plugins.sparql @@ -77,7 +78,7 @@ def main() -> None: ) parser.add_argument( "out_table", - help="Expected extensions are .html for HTML tables or .md for Markdown tables.", + help="Expected extensions are .html for HTML tables, .md for Markdown tables, .csv for comma-separated values, and .tsv for tab-separated values.", ) parser.add_argument( "in_sparql", @@ -146,8 +147,20 @@ def main() -> None: df = pd.DataFrame(records, columns=variables) - table_text = None - if args.out_table.endswith(".html"): + table_text: typing.Optional[str] = None + if args.out_table.endswith(".csv") or args.out_table.endswith(".tsv"): + # https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_csv.html + sep: str + if args.out_table.endswith(".csv"): + sep = "," + elif args.out_table.endswith(".tsv"): + sep = "\t" + else: + raise NotImplementedError( + "Output extension not implemented in CSV-style output." + ) + table_text = df.to_csv(sep=sep) + elif args.out_table.endswith(".html"): # https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_html.html # Add CSS classes for CASE website Bootstrap support. table_text = df.to_html(classes=("table", "table-bordered", "table-condensed")) diff --git a/tests/case_utils/case_sparql_select/.check-prefixed_results.csv b/tests/case_utils/case_sparql_select/.check-prefixed_results.csv new file mode 100644 index 0000000..60d4b78 --- /dev/null +++ b/tests/case_utils/case_sparql_select/.check-prefixed_results.csv @@ -0,0 +1,3 @@ +,?nFile +0,kb:file-1 +1,kb:file-2 diff --git a/tests/case_utils/case_sparql_select/.check-prefixed_results.tsv b/tests/case_utils/case_sparql_select/.check-prefixed_results.tsv new file mode 100644 index 0000000..9dac11d --- /dev/null +++ b/tests/case_utils/case_sparql_select/.check-prefixed_results.tsv @@ -0,0 +1,3 @@ + ?nFile +0 kb:file-1 +1 kb:file-2 diff --git a/tests/case_utils/case_sparql_select/.check-w3-output.csv b/tests/case_utils/case_sparql_select/.check-w3-output.csv new file mode 100644 index 0000000..063e950 --- /dev/null +++ b/tests/case_utils/case_sparql_select/.check-w3-output.csv @@ -0,0 +1,3 @@ +,?name,?mbox +0,Johnny Lee Outlaw,mailto:jlow@example.com +1,Peter Goodguy,mailto:peter@example.org diff --git a/tests/case_utils/case_sparql_select/.check-w3-output.tsv b/tests/case_utils/case_sparql_select/.check-w3-output.tsv new file mode 100644 index 0000000..a4fdfca --- /dev/null +++ b/tests/case_utils/case_sparql_select/.check-w3-output.tsv @@ -0,0 +1,3 @@ + ?name ?mbox +0 Johnny Lee Outlaw mailto:jlow@example.com +1 Peter Goodguy mailto:peter@example.org diff --git a/tests/case_utils/case_sparql_select/Makefile b/tests/case_utils/case_sparql_select/Makefile index 6431be7..68f11ec 100644 --- a/tests/case_utils/case_sparql_select/Makefile +++ b/tests/case_utils/case_sparql_select/Makefile @@ -18,19 +18,25 @@ top_srcdir := $(shell cd ../../.. ; pwd) tests_srcdir := $(top_srcdir)/tests all: \ + prefixed_results.csv \ prefixed_results.html \ prefixed_results.md \ + prefixed_results.tsv \ subclass-explicit-none.md \ subclass-implicit-any.md \ + w3-output.csv \ w3-output.html \ - w3-output.md + w3-output.md \ + w3-output.tsv .PHONY: \ check-subclass \ check-subclass-explicit-none \ check-subclass-implicit-any \ + check-w3-csv \ check-w3-html \ - check-w3-markdown + check-w3-markdown \ + check-w3-tsv .PRECIOUS: \ prefixed_results.% \ @@ -38,14 +44,23 @@ all: \ w3-output.% check: \ + check-w3-csv \ check-w3-html \ check-w3-markdown \ + check-w3-tsv \ check-prefixed_results \ check-subclass check-prefixed_results: \ + check-prefixed_results-csv \ check-prefixed_results-html \ - check-prefixed_results-md + check-prefixed_results-md \ + check-prefixed_results-tsv + +check-prefixed_results-csv: \ + .check-prefixed_results.csv \ + prefixed_results.csv + diff $^ check-prefixed_results-html: \ .check-prefixed_results.html \ @@ -57,6 +72,11 @@ check-prefixed_results-md: \ prefixed_results.md diff $^ +check-prefixed_results-tsv: \ + .check-prefixed_results.tsv \ + prefixed_results.tsv + diff $^ + check-subclass: \ check-subclass-explicit-none \ check-subclass-implicit-any @@ -71,6 +91,11 @@ check-subclass-implicit-any: \ subclass-implicit-any.md diff $^ +check-w3-csv: \ + .check-w3-output.csv \ + w3-output.csv + diff $^ + check-w3-html: \ .check-w3-output.html \ w3-output.html @@ -81,12 +106,19 @@ check-w3-markdown: \ w3-output.md diff $^ +check-w3-tsv: \ + .check-w3-output.tsv \ + w3-output.tsv + diff $^ + clean: @rm -rf \ __pycache__ @rm -f \ + *.csv \ *.html \ *.md \ + *.tsv \ _* prefixed_results.%: \