Skip to content

Commit 83ec9ac

Browse files
Merge pull request #88 from casework/case_sparql_select_add_csv_and_tsv_output
case_sparql_select: Add CSV and TSV output modes
2 parents 4b9bbd5 + e9ab560 commit 83ec9ac

File tree

6 files changed

+63
-6
lines changed

6 files changed

+63
-6
lines changed

case_utils/case_sparql_select/__init__.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import logging
3434
import os
3535
import sys
36+
import typing
3637

3738
import pandas as pd # type: ignore
3839
import rdflib.plugins.sparql
@@ -77,7 +78,7 @@ def main() -> None:
7778
)
7879
parser.add_argument(
7980
"out_table",
80-
help="Expected extensions are .html for HTML tables or .md for Markdown tables.",
81+
help="Expected extensions are .html for HTML tables, .md for Markdown tables, .csv for comma-separated values, and .tsv for tab-separated values.",
8182
)
8283
parser.add_argument(
8384
"in_sparql",
@@ -146,8 +147,20 @@ def main() -> None:
146147

147148
df = pd.DataFrame(records, columns=variables)
148149

149-
table_text = None
150-
if args.out_table.endswith(".html"):
150+
table_text: typing.Optional[str] = None
151+
if args.out_table.endswith(".csv") or args.out_table.endswith(".tsv"):
152+
# https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_csv.html
153+
sep: str
154+
if args.out_table.endswith(".csv"):
155+
sep = ","
156+
elif args.out_table.endswith(".tsv"):
157+
sep = "\t"
158+
else:
159+
raise NotImplementedError(
160+
"Output extension not implemented in CSV-style output."
161+
)
162+
table_text = df.to_csv(sep=sep)
163+
elif args.out_table.endswith(".html"):
151164
# https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_html.html
152165
# Add CSS classes for CASE website Bootstrap support.
153166
table_text = df.to_html(classes=("table", "table-bordered", "table-condensed"))
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
,?nFile
2+
0,kb:file-1
3+
1,kb:file-2
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
?nFile
2+
0 kb:file-1
3+
1 kb:file-2
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
,?name,?mbox
2+
0,Johnny Lee Outlaw,mailto:[email protected]
3+
1,Peter Goodguy,mailto:[email protected]
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
?name ?mbox
2+
0 Johnny Lee Outlaw mailto:[email protected]
3+
1 Peter Goodguy mailto:[email protected]

tests/case_utils/case_sparql_select/Makefile

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,34 +18,49 @@ top_srcdir := $(shell cd ../../.. ; pwd)
1818
tests_srcdir := $(top_srcdir)/tests
1919

2020
all: \
21+
prefixed_results.csv \
2122
prefixed_results.html \
2223
prefixed_results.md \
24+
prefixed_results.tsv \
2325
subclass-explicit-none.md \
2426
subclass-implicit-any.md \
27+
w3-output.csv \
2528
w3-output.html \
26-
w3-output.md
29+
w3-output.md \
30+
w3-output.tsv
2731

2832
.PHONY: \
2933
check-subclass \
3034
check-subclass-explicit-none \
3135
check-subclass-implicit-any \
36+
check-w3-csv \
3237
check-w3-html \
33-
check-w3-markdown
38+
check-w3-markdown \
39+
check-w3-tsv
3440

3541
.PRECIOUS: \
3642
prefixed_results.% \
3743
subclass-% \
3844
w3-output.%
3945

4046
check: \
47+
check-w3-csv \
4148
check-w3-html \
4249
check-w3-markdown \
50+
check-w3-tsv \
4351
check-prefixed_results \
4452
check-subclass
4553

4654
check-prefixed_results: \
55+
check-prefixed_results-csv \
4756
check-prefixed_results-html \
48-
check-prefixed_results-md
57+
check-prefixed_results-md \
58+
check-prefixed_results-tsv
59+
60+
check-prefixed_results-csv: \
61+
.check-prefixed_results.csv \
62+
prefixed_results.csv
63+
diff $^
4964

5065
check-prefixed_results-html: \
5166
.check-prefixed_results.html \
@@ -57,6 +72,11 @@ check-prefixed_results-md: \
5772
prefixed_results.md
5873
diff $^
5974

75+
check-prefixed_results-tsv: \
76+
.check-prefixed_results.tsv \
77+
prefixed_results.tsv
78+
diff $^
79+
6080
check-subclass: \
6181
check-subclass-explicit-none \
6282
check-subclass-implicit-any
@@ -71,6 +91,11 @@ check-subclass-implicit-any: \
7191
subclass-implicit-any.md
7292
diff $^
7393

94+
check-w3-csv: \
95+
.check-w3-output.csv \
96+
w3-output.csv
97+
diff $^
98+
7499
check-w3-html: \
75100
.check-w3-output.html \
76101
w3-output.html
@@ -81,12 +106,19 @@ check-w3-markdown: \
81106
w3-output.md
82107
diff $^
83108

109+
check-w3-tsv: \
110+
.check-w3-output.tsv \
111+
w3-output.tsv
112+
diff $^
113+
84114
clean:
85115
@rm -rf \
86116
__pycache__
87117
@rm -f \
118+
*.csv \
88119
*.html \
89120
*.md \
121+
*.tsv \
90122
_*
91123

92124
prefixed_results.%: \

0 commit comments

Comments
 (0)