Skip to content

Commit ac90fb3

Browse files
committed
closes #82
1 parent 5bc712f commit ac90fb3

5 files changed

Lines changed: 35 additions & 15 deletions

File tree

CHANGES

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
Changelog
22
=========
33

4+
unreleased
5+
----------
6+
7+
- fixed expanding of URLs to account for QNames used as URI template.
8+
9+
410

511
Version 3.6.0
612
-------------

src/csvw/jsonld.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,7 @@ def from_col(cls, table, col, row, prop, val, rownum):
7979
is_type = prop == 'rdf:type'
8080
valueUrl = col.valueUrl if col else table.inherit('valueUrl')
8181
if valueUrl:
82-
val = table.expand(
83-
valueUrl, row, _row=rownum, _name=_name, qname=is_type, uri=not is_type)
82+
val = table.expand(valueUrl, row, _row=rownum, _name=_name, qname=is_type)
8483
val = format_value(val, col)
8584
s = None
8685
aboutUrl = col.aboutUrl if col else None

src/csvw/metadata.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,7 +1098,7 @@ def base(self) -> typing.Union[str, pathlib.Path]:
10981098
return self._parent._fname.parent if (self._parent and self._parent._fname) else \
10991099
(self._fname.parent if self._fname else None)
11001100

1101-
def expand(self, tmpl: URITemplate, row: dict, _row, _name=None, qname=False, uri=False) -> str:
1101+
def expand(self, tmpl: URITemplate, row: dict, _row, _name=None, qname=False) -> str:
11021102
"""
11031103
Expand a `URITemplate` using `row`, `_row` and `_name` as context and resolving the result
11041104
against `TableLike.url`.
@@ -1113,25 +1113,28 @@ def expand(self, tmpl: URITemplate, row: dict, _row, _name=None, qname=False, ur
11131113
'https://raw.githubusercontent.com/path?1#2'
11141114
11151115
"""
1116-
assert not (qname and uri)
11171116
if tmpl is INVALID:
11181117
return self.url.resolve(self.base)
1119-
res = Link(
1120-
tmpl.expand(
1121-
_row=_row, _name=_name, **{_k: _v for _k, _v in row.items() if isinstance(_k, str)}
1122-
)).resolve(self.url.resolve(self.base) if self.url else self.base)
1118+
1119+
for prefix, url in NAMESPACES.items():
1120+
if tmpl.uri.startswith(prefix + ':'):
1121+
# If the URI Template is a QName, we expand it to a URL to prevent `Link.resolve`
1122+
# from turning it into a local path.
1123+
res = '{}{}'.format(url, tmpl.uri.split(':')[1])
1124+
break
1125+
else:
1126+
res = Link(
1127+
tmpl.expand(
1128+
_row=_row,
1129+
_name=_name,
1130+
**{_k: _v for _k, _v in row.items() if isinstance(_k, str)}
1131+
)).resolve(self.url.resolve(self.base) if self.url else self.base)
11231132
if not isinstance(res, pathlib.Path):
11241133
if qname:
11251134
for prefix, url in NAMESPACES.items():
11261135
if res.startswith(url):
11271136
res = res.replace(url, prefix + ':')
11281137
break
1129-
if uri:
1130-
if res != 'rdf:type':
1131-
for prefix, url in NAMESPACES.items():
1132-
if res.startswith(prefix + ':'):
1133-
res = res.replace(prefix + ':', url)
1134-
break
11351138
return res
11361139

11371140

tests/fixtures/csv.txt-table-metadata.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"url": "csv.txt",
1313
"tableSchema": {
1414
"columns": [
15-
{"name": "ID", "datatype": {"base": "string", "minLength": 3}},
15+
{"name": "ID", "propertyUrl": "dc:identifier","datatype": {"base": "string", "minLength": 3}},
1616
{"datatype": "string"}
1717
]
1818
}

tests/test_conformance.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
import pathlib
2+
13
import pytest
24

5+
FIXTURES = pathlib.Path(__file__).parent / 'fixtures'
6+
37

48
@pytest.mark.conformance
59
def test_csvw_json(csvwjsontest):
@@ -14,3 +18,11 @@ def test_csvw_nonnorm(csvwnonnormtest):
1418
@pytest.mark.conformance
1519
def test_csvw_validation(csvwvalidationtest):
1620
csvwvalidationtest.run()
21+
22+
23+
def test_prefix_in_property_url():
24+
from csvw import CSVW
25+
26+
obj = CSVW(str(FIXTURES / 'csv.txt'), md_url=str(FIXTURES / 'csv.txt-table-metadata.json'))
27+
row = obj.to_json()['tables'][0]['row'][0]['describes'][0]
28+
assert 'dc:identifier' in row

0 commit comments

Comments
 (0)