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 1 commit
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
18 changes: 15 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 @@ -72,7 +73,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 @@ -136,8 +137,19 @@ 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"):
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
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
22 changes: 20 additions & 2 deletions tests/case_utils/case_sparql_select/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,29 @@ tests_srcdir := $(top_srcdir)/tests
all: \
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: \
subclass-% \
w3-output.%

check: \
check-w3-csv \
check-w3-html \
check-w3-markdown \
check-w3-tsv \
check-subclass

check-subclass: \
Expand All @@ -53,6 +59,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 @@ -63,12 +74,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 \
_*

subclass-explicit-none.md: \
Expand Down