Skip to content

Commit e699592

Browse files
committed
Add case_sparql_select CSV and TSV output
No effects were observed on Make-managed files. Signed-off-by: Alex Nelson <[email protected]>
1 parent 98d4c6b commit e699592

File tree

7 files changed

+109
-2
lines changed

7 files changed

+109
-2
lines changed

case_utils/case_sparql_select/__init__.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ def graph_and_query_to_data_frame(
123123
def data_frame_to_table_text(
124124
df: pd.DataFrame,
125125
*args: typing.Any,
126+
json_indent: typing.Optional[int] = None,
127+
json_orient: str,
126128
output_mode: str,
127129
**kwargs: typing.Any,
128130
) -> str:
@@ -143,6 +145,12 @@ def data_frame_to_table_text(
143145
# https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_html.html
144146
# Add CSS classes for CASE website Bootstrap support.
145147
table_text = df.to_html(classes=("table", "table-bordered", "table-condensed"))
148+
elif output_mode == "json":
149+
# https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_json.html
150+
151+
table_text = df.to_json(
152+
indent=json_indent, orient=json_orient, date_format="iso"
153+
)
146154
elif output_mode == "md":
147155
# https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_markdown.html
148156
# https://pypi.org/project/tabulate/
@@ -179,14 +187,25 @@ def main() -> None:
179187
action="store_true",
180188
help="Raise error if no results are returned for query.",
181189
)
190+
parser.add_argument(
191+
"--json-indent",
192+
type=int,
193+
help="Number of whitespace characters to use for indentation. Only applicable for JSON output.",
194+
)
195+
parser.add_argument(
196+
"--json-orient",
197+
default="columns",
198+
choices=("columns", "index", "records", "split", "table", "values"),
199+
help="Orientation to use for Pandas DataFrame JSON output. Only applicable for JSON output.",
200+
)
182201
parser.add_argument(
183202
"--use-prefixes",
184203
action="store_true",
185204
help="Abbreviate node IDs according to graph's encoded prefixes. (This will use prefixes in the graph, not the query.)",
186205
)
187206
parser.add_argument(
188207
"out_table",
189-
help="Expected extensions are .html for HTML tables, .md for Markdown tables, .csv for comma-separated values, and .tsv for tab-separated values.",
208+
help="Expected extensions are .html for HTML tables, .json for JSON tables, .md for Markdown tables, .csv for comma-separated values, and .tsv for tab-separated values. Note that JSON is a Pandas output JSON format (chosen by '--json-orient'), and not JSON-LD.",
190209
)
191210
parser.add_argument(
192211
"in_sparql",
@@ -231,6 +250,8 @@ def main() -> None:
231250

232251
table_text = data_frame_to_table_text(
233252
df,
253+
json_indent=args.json_indent,
254+
json_orient=args.json_orient,
234255
output_mode=output_mode,
235256
)
236257
with open(args.out_table, "w") as out_fh:
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"?nFile":{
3+
"0":"kb:file-1",
4+
"1":"kb:file-2"
5+
}
6+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"?nFile":{"0":"kb:file-1","1":"kb:file-2"}}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"?name":{
3+
"0":"Johnny Lee Outlaw",
4+
"1":"Peter Goodguy"
5+
},
6+
"?mbox":{
7+
"0":"mailto:[email protected]",
8+
"1":"mailto:[email protected]"
9+
}
10+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"?name":{"0":"Johnny Lee Outlaw","1":"Peter Goodguy"},"?mbox":{"0":"mailto:[email protected]","1":"mailto:[email protected]"}}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
prefixed*
22
subclass-*.md
3-
w3-output.*
3+
w3-output*

tests/case_utils/case_sparql_select/Makefile

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,16 @@ tests_srcdir := $(top_srcdir)/tests
2020
all: \
2121
prefixed_results.csv \
2222
prefixed_results.html \
23+
prefixed_results.json \
24+
prefixed_results-indented.json \
2325
prefixed_results.md \
2426
prefixed_results.tsv \
2527
subclass-explicit-none.md \
2628
subclass-implicit-any.md \
2729
w3-output.csv \
2830
w3-output.html \
31+
w3-output.json \
32+
w3-output-indented.json \
2933
w3-output.md \
3034
w3-output.tsv
3135

@@ -35,6 +39,8 @@ all: \
3539
check-subclass-implicit-any \
3640
check-w3-csv \
3741
check-w3-html \
42+
check-w3-json \
43+
check-w3-json-indented \
3844
check-w3-markdown \
3945
check-w3-tsv
4046

@@ -46,6 +52,7 @@ all: \
4652
check: \
4753
check-w3-csv \
4854
check-w3-html \
55+
check-w3-json \
4956
check-w3-markdown \
5057
check-w3-tsv \
5158
check-prefixed_results \
@@ -54,6 +61,7 @@ check: \
5461
check-prefixed_results: \
5562
check-prefixed_results-csv \
5663
check-prefixed_results-html \
64+
check-prefixed_results-json \
5765
check-prefixed_results-md \
5866
check-prefixed_results-tsv
5967

@@ -67,6 +75,19 @@ check-prefixed_results-html: \
6775
prefixed_results.html
6876
diff $^
6977

78+
check-prefixed_results-json: \
79+
check-prefixed_results-json-indented \
80+
.check-prefixed_results.json \
81+
prefixed_results.json
82+
diff \
83+
.check-prefixed_results.json \
84+
prefixed_results.json
85+
86+
check-prefixed_results-json-indented: \
87+
.check-prefixed_results-indented.json \
88+
prefixed_results-indented.json
89+
diff $^
90+
7091
check-prefixed_results-md: \
7192
.check-prefixed_results.md \
7293
prefixed_results.md
@@ -101,6 +122,19 @@ check-w3-html: \
101122
w3-output.html
102123
diff $^
103124

125+
check-w3-json: \
126+
.check-w3-output.json \
127+
check-w3-json-indented \
128+
w3-output.json
129+
diff \
130+
.check-w3-output.json \
131+
w3-output.json
132+
133+
check-w3-json-indented: \
134+
.check-w3-output-indented.json \
135+
w3-output-indented.json
136+
diff $^
137+
104138
check-w3-markdown: \
105139
.check-w3-output.md \
106140
w3-output.md
@@ -119,6 +153,7 @@ clean:
119153
*.html \
120154
*.md \
121155
*.tsv \
156+
*output*.json \
122157
_*
123158

124159
prefixed_results.%: \
@@ -136,6 +171,22 @@ prefixed_results.%: \
136171
subclass.json
137172
mv _$@ $@
138173

174+
prefixed_results-indented.json: \
175+
$(tests_srcdir)/.venv.done.log \
176+
$(top_srcdir)/case_utils/case_sparql_select/__init__.py \
177+
$(top_srcdir)/case_utils/ontology/__init__.py \
178+
$(top_srcdir)/case_utils/ontology/version_info.py \
179+
subclass.json \
180+
subclass.sparql
181+
source $(tests_srcdir)/venv/bin/activate \
182+
&& case_sparql_select \
183+
--json-indent 4 \
184+
--use-prefixes \
185+
_$@ \
186+
subclass.sparql \
187+
subclass.json
188+
mv _$@ $@
189+
139190
subclass-explicit-none.md: \
140191
$(tests_srcdir)/.venv.done.log \
141192
$(top_srcdir)/case_utils/case_sparql_select/__init__.py \
@@ -180,3 +231,20 @@ w3-output.%: \
180231
w3-input-2.ttl \
181232
w3-input-3.json
182233
mv _$@ $@
234+
235+
w3-output-indented.json: \
236+
$(tests_srcdir)/.venv.done.log \
237+
$(top_srcdir)/case_utils/case_sparql_select/__init__.py \
238+
$(top_srcdir)/case_utils/ontology/__init__.py \
239+
$(top_srcdir)/case_utils/ontology/version_info.py \
240+
w3-input-1.sparql \
241+
w3-input-2.ttl \
242+
w3-input-3.json
243+
source $(tests_srcdir)/venv/bin/activate \
244+
&& case_sparql_select \
245+
--json-indent 4 \
246+
_$@ \
247+
w3-input-1.sparql \
248+
w3-input-2.ttl \
249+
w3-input-3.json
250+
mv _$@ $@

0 commit comments

Comments
 (0)