Mercurial > code > home > repos > rdfdb
changeset 93:f22723264489
cleanup
author | drewp@bigasterisk.com |
---|---|
date | Sun, 15 May 2022 15:52:01 -0700 |
parents | 5ebb6129c035 |
children | 8bc63c7b619b |
files | rdfdb/file_vs_uri.py |
diffstat | 1 files changed, 21 insertions(+), 9 deletions(-) [+] |
line wrap: on
line diff
--- a/rdfdb/file_vs_uri.py Sun May 15 15:49:28 2022 -0700 +++ b/rdfdb/file_vs_uri.py Sun May 15 15:52:01 2022 -0700 @@ -1,13 +1,24 @@ """ note that if there are ambiguities in dirUriMap, you'll get undefined results that depend on python's items() order. + +In this example: + http://example.com/a/b/c <-> /my/dir/b/c.n3 + bbbbbbbbbbbbbbbbbbbb mmm ddddddd mmm +The code naming goes like this: + b is baseUri + m is middle + d is dirpath + +DirUriMap is a dict of dirPath -> baseUri. + """ from pathlib import Path -from typing import Dict +from typing import Dict, Union -from rdflib import URIRef +from rdflib import URIRef, Namespace -DirUriMap = Dict[Path, URIRef] +DirUriMap = Dict[Path, Union[URIRef, Namespace]] def _pathStartsWith(fullPath: Path, possiblePrefix: Path) -> bool: @@ -17,19 +28,20 @@ def uriFromFile(dirUriMap: DirUriMap, filename: Path) -> URIRef: if not str(filename).endswith('.n3'): raise ValueError(filename) - for d, prefix in dirUriMap.items(): - if _pathStartsWith(filename, d): - return URIRef(prefix + str(filename)[len(str(d)):-len(b'.n3')]) + for dirPath, baseUri in dirUriMap.items(): + if _pathStartsWith(filename, dirPath): + middle = str(filename)[len(str(dirPath)):-len('.n3')].lstrip('/') + return URIRef(baseUri.rstrip('/') + '/' + middle) raise ValueError("filename %s doesn't start with any of %s" % (filename, list(dirUriMap.keys()))) def fileForUri(dirUriMap: DirUriMap, ctx: URIRef) -> Path: - for d, prefix in dirUriMap.items(): + for dirPath, prefix in dirUriMap.items(): if ctx.startswith(prefix): - middle = ctx[len(prefix):] + middle = ctx[len(prefix):].lstrip('/') if middle.endswith('/'): raise NotImplementedError(f'no file mapped to {ctx=}') - return Path(str(d) + middle + '.n3') + return dirPath / (middle + '.n3') raise ValueError("don't know what filename to use for %s" % ctx)