Skip to content

Commit 10589e7

Browse files
Merge pull request #86 from casework/case_sparql_select_add_use_prefixes
case_sparql_select: Add --use-prefixes flag
2 parents 609f762 + 9a2eb22 commit 10589e7

File tree

6 files changed

+87
-0
lines changed

6 files changed

+87
-0
lines changed

case_utils/case_sparql_select/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ def main() -> None:
7070
action="store_true",
7171
help="Raise error if no results are returned for query.",
7272
)
73+
parser.add_argument(
74+
"--use-prefixes",
75+
action="store_true",
76+
help="Abbreviate node IDs according to graph's encoded prefixes. (This will use prefixes in the graph, not the query.)",
77+
)
7378
parser.add_argument(
7479
"out_table",
7580
help="Expected extensions are .html for HTML tables or .md for Markdown tables.",
@@ -124,6 +129,11 @@ def main() -> None:
124129
# The render to ASCII is in support of this script rendering results for website viewing.
125130
# .decode() is because hexlify returns bytes.
126131
column_value = binascii.hexlify(column.toPython()).decode()
132+
elif isinstance(column, rdflib.URIRef):
133+
if args.use_prefixes:
134+
column_value = graph.namespace_manager.qname(column.toPython())
135+
else:
136+
column_value = column.toPython()
127137
else:
128138
column_value = column.toPython()
129139
if row_no == 0:
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<table border="1" class="dataframe table table-bordered table-condensed">
2+
<thead>
3+
<tr style="text-align: right;">
4+
<th></th>
5+
<th>?nFile</th>
6+
</tr>
7+
</thead>
8+
<tbody>
9+
<tr>
10+
<th>0</th>
11+
<td>kb:file-1</td>
12+
</tr>
13+
<tr>
14+
<th>1</th>
15+
<td>kb:file-2</td>
16+
</tr>
17+
</tbody>
18+
</table>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
| | ?nFile |
2+
|----|-----------|
3+
| 0 | kb:file-1 |
4+
| 1 | kb:file-2 |

tests/case_utils/case_sparql_select/Makefile

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ top_srcdir := $(shell cd ../../.. ; pwd)
1818
tests_srcdir := $(top_srcdir)/tests
1919

2020
all: \
21+
prefixed_results.html \
22+
prefixed_results.md \
2123
subclass-explicit-none.md \
2224
subclass-implicit-any.md \
2325
w3-output.html \
@@ -31,14 +33,30 @@ all: \
3133
check-w3-markdown
3234

3335
.PRECIOUS: \
36+
prefixed_results.% \
3437
subclass-% \
3538
w3-output.%
3639

3740
check: \
3841
check-w3-html \
3942
check-w3-markdown \
43+
check-prefixed_results \
4044
check-subclass
4145

46+
check-prefixed_results: \
47+
check-prefixed_results-html \
48+
check-prefixed_results-md
49+
50+
check-prefixed_results-html: \
51+
.check-prefixed_results.html \
52+
prefixed_results.html
53+
diff $^
54+
55+
check-prefixed_results-md: \
56+
.check-prefixed_results.md \
57+
prefixed_results.md
58+
diff $^
59+
4260
check-subclass: \
4361
check-subclass-explicit-none \
4462
check-subclass-implicit-any
@@ -71,6 +89,21 @@ clean:
7189
*.md \
7290
_*
7391

92+
prefixed_results.%: \
93+
$(tests_srcdir)/.venv.done.log \
94+
$(top_srcdir)/case_utils/case_sparql_select/__init__.py \
95+
$(top_srcdir)/case_utils/ontology/__init__.py \
96+
$(top_srcdir)/case_utils/ontology/version_info.py \
97+
subclass.json \
98+
subclass.sparql
99+
source $(tests_srcdir)/venv/bin/activate \
100+
&& case_sparql_select \
101+
--use-prefixes \
102+
_$@ \
103+
subclass.sparql \
104+
subclass.json
105+
mv _$@ $@
106+
74107
subclass-explicit-none.md: \
75108
$(tests_srcdir)/.venv.done.log \
76109
$(top_srcdir)/case_utils/case_sparql_select/__init__.py \
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<table border="1" class="dataframe table table-bordered table-condensed">
2+
<thead>
3+
<tr style="text-align: right;">
4+
<th></th>
5+
<th>?nFile</th>
6+
</tr>
7+
</thead>
8+
<tbody>
9+
<tr>
10+
<th>0</th>
11+
<td>kb:file-1</td>
12+
</tr>
13+
<tr>
14+
<th>1</th>
15+
<td>kb:file-2</td>
16+
</tr>
17+
</tbody>
18+
</table>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
| | ?nFile |
2+
|----|-----------|
3+
| 0 | kb:file-1 |
4+
| 1 | kb:file-2 |

0 commit comments

Comments
 (0)