Skip to content

case_sparql_select: Add CSV and TSV output modes #88

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions case_utils/case_sparql_select/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import logging
import os
import sys
import typing

import pandas as pd # type: ignore
import rdflib.plugins.sparql
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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"))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
,?nFile
0,kb:file-1
1,kb:file-2
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
?nFile
0 kb:file-1
1 kb:file-2
3 changes: 3 additions & 0 deletions tests/case_utils/case_sparql_select/.check-w3-output.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
,?name,?mbox
0,Johnny Lee Outlaw,mailto:jlow@example.com
1,Peter Goodguy,mailto:peter@example.org
3 changes: 3 additions & 0 deletions tests/case_utils/case_sparql_select/.check-w3-output.tsv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
?name ?mbox
0 Johnny Lee Outlaw mailto:jlow@example.com
1 Peter Goodguy mailto:peter@example.org
38 changes: 35 additions & 3 deletions tests/case_utils/case_sparql_select/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,49 @@ 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.% \
subclass-% \
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 \
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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.%: \
Expand Down