Skip to content

Commit 712ea3a

Browse files
committed
Require namespace argument
No effects were observed on Make-managed files. Signed-off-by: Alex Nelson <[email protected]>
1 parent 48511fe commit 712ea3a

File tree

2 files changed

+15
-62
lines changed

2 files changed

+15
-62
lines changed

case_utils/case_file/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def create_file_node(
104104
n_file_facet: rdflib.URIRef
105105
if use_deterministic_uuids:
106106
n_file_facet = case_utils.inherent_uuid.get_facet_uriref(
107-
n_file, NS_UCO_OBSERVABLE.FileFacet
107+
n_file, NS_UCO_OBSERVABLE.FileFacet, namespace=node_namespace
108108
)
109109
else:
110110
n_file_facet = node_namespace["FileFacet-" + case_utils.local_uuid.local_uuid()]
@@ -138,7 +138,7 @@ def create_file_node(
138138
n_contentdata_facet: rdflib.URIRef
139139
if use_deterministic_uuids:
140140
n_contentdata_facet = case_utils.inherent_uuid.get_facet_uriref(
141-
n_file, NS_UCO_OBSERVABLE.ContentDataFacet
141+
n_file, NS_UCO_OBSERVABLE.ContentDataFacet, namespace=node_namespace
142142
)
143143
else:
144144
n_contentdata_facet = node_namespace[

case_utils/inherent_uuid.py

Lines changed: 13 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,18 @@
3434
A knowledge base ontology currently uses a prefix 'kb:', expanding to 'http://example.org/kb/'. This knowledge base has a node kb:File-ac6b44cf-dc6b-4f2c-a09d-c9beb0a345a9. What is the IRI of its FileFacet?
3535
3636
>>> from case_utils.namespace import NS_UCO_OBSERVABLE
37-
>>> file_iri: str = "http://example.org/kb/File-ac6b44cf-dc6b-4f2c-a09d-c9beb0a345a9"
38-
>>> n_file = URIRef(file_iri)
39-
>>> n_file_facet = get_facet_uriref(n_file, NS_UCO_OBSERVABLE.FileFacet)
37+
>>> ns_kb = Namespace("http://example.org/kb/")
38+
>>> n_file = ns_kb["File-ac6b44cf-dc6b-4f2c-a09d-c9beb0a345a9"]
39+
>>> n_file_facet = get_facet_uriref(n_file, NS_UCO_OBSERVABLE.FileFacet, namespace=ns_kb)
4040
>>> n_file_facet
4141
rdflib.term.URIRef('http://example.org/kb/FileFacet-01d292e3-0f38-5974-868d-006ef07f5186')
4242
4343
A documentation policy change has been enacted, and now all knowledge base individuals need to use the URN example form. What is the FileFacet IRI now?
4444
45+
>>> ns_kb_2 = Namespace("urn:example:kb:")
4546
>>> file_iri_2: str = "urn:example:kb:File-ac6b44cf-dc6b-4f2c-a09d-c9beb0a345a9"
4647
>>> n_file_2 = URIRef(file_iri_2)
47-
>>> n_file_facet_2 = get_facet_uriref(n_file_2, NS_UCO_OBSERVABLE.FileFacet)
48+
>>> n_file_facet_2 = get_facet_uriref(n_file_2, NS_UCO_OBSERVABLE.FileFacet, namespace=ns_kb_2)
4849
>>> n_file_facet_2
4950
rdflib.term.URIRef('urn:example:kb:FileFacet-01d292e3-0f38-5974-868d-006ef07f5186')
5051
@@ -53,7 +54,7 @@
5354
>>> assert str(n_file_facet)[-36:] == str(n_file_facet_2)[-36:]
5455
"""
5556

56-
__version__ = "0.0.2"
57+
__version__ = "0.0.3"
5758

5859
import binascii
5960
import re
@@ -148,85 +149,37 @@ def facet_inherence_uuid(
148149
return uuid.uuid5(uco_object_inherence_uuid, str(n_facet_class))
149150

150151

151-
def guess_namespace(n_node: URIRef, *args: Any, **kwargs: Any) -> Namespace:
152-
"""
153-
This function attempts simple heuristics to extract from a URIRef its namespace IRI. The heuristics, being simple, are not necessarily going to provide desirable answers, and might be semantically incorrect.
154-
155-
:rtype rdflib.Namespace:
156-
157-
Examples
158-
========
159-
160-
>>> guess_namespace(URIRef("http://example.org/kb/Foo"))
161-
Namespace('http://example.org/kb/')
162-
>>> guess_namespace(URIRef("http://example.org/kb#Foo"))
163-
Namespace('http://example.org/kb#')
164-
>>> guess_namespace(URIRef("urn:example:kb#Foo"))
165-
Namespace('urn:example:kb#')
166-
167-
Note this function might not always give desirable answers.
168-
169-
>>> guess_namespace(URIRef("urn:example:kb#Foo/Bar"))
170-
Namespace('urn:example:kb#')
171-
>>> guess_namespace(URIRef("urn:example:kb/Foo#Bar"))
172-
Namespace('urn:example:kb/Foo#')
173-
174-
Some patterns, such as Simple Storage Service (S3) URLs being treated as RDF IRIs, should avoid using this function. This object that houses a PCAP blob can function as an IRI, but the guessed namespace value does not serve as an RDF namespace.
175-
176-
>>> guess_namespace(URIRef("s3://digitalcorpora/corpora/scenarios/2008-nitroba/nitroba.pcap"))
177-
Namespace('s3://digitalcorpora/corpora/scenarios/2008-nitroba/')
178-
"""
179-
node_iri = str(n_node)
180-
if "#" in node_iri:
181-
namespace_iri = node_iri[: 1 + node_iri.rindex("#")]
182-
elif "/" in node_iri:
183-
namespace_iri = node_iri[: 1 + node_iri.rindex("/")]
184-
else:
185-
namespace_iri = node_iri[: 1 + node_iri.rindex(":")]
186-
return Namespace(namespace_iri)
187-
188-
189152
def get_facet_uriref(
190153
n_uco_object: URIRef,
191154
n_facet_class: URIRef,
192155
*args: Any,
193-
namespace: Optional[Namespace] = None,
156+
namespace: Namespace,
194157
**kwargs: Any
195158
) -> URIRef:
196159
"""
197-
:param namespace: An optional RDFLib Namespace object to use for prefixing the Facet IRI with a knowledge base prefix IRI. If not provided, will be guessed from a right-truncation of n_uco_object under some namespace forms (hash-, then slash-, then colon-terminated). This is a potentially fragile guessing mechanism, so users should feel encouraged to provide this optional parameter.
198-
:type namespace rdflib.Namespace or None:
160+
:param namespace: An RDFLib Namespace object to use for prefixing the Facet IRI with a knowledge base prefix IRI.
161+
:type namespace rdflib.Namespace:
199162
200163
Examples
201164
========
202165
203-
What is the URLFacet pertaining to the Nitroba University Scenario's PCAP file, when being interpreted as a Simple Storage Service (S3) object? Note that this example will show that in some cases a (RDFLib) Namespace will be desired.
166+
What is the URLFacet pertaining to the Nitroba University Scenario's PCAP file, when being interpreted as a Simple Storage Service (S3) object?
204167
205168
>>> from case_utils.namespace import NS_UCO_OBSERVABLE
206169
>>> pcap_url: str = "s3://digitalcorpora/corpora/scenarios/2008-nitroba/nitroba.pcap"
207170
>>> n_pcap = URIRef(pcap_url)
208-
>>> n_pcap_url_facet_try1 = get_facet_uriref(n_pcap, NS_UCO_OBSERVABLE.URLFacet)
209-
>>> n_pcap_url_facet_try1
210-
rdflib.term.URIRef('s3://digitalcorpora/corpora/scenarios/2008-nitroba/URLFacet-4b6023da-dbc4-5e1e-9a2f-aca2a6f6405c')
211-
>>> # Looks like a (RDFLib) Namespace object should be provided.
212171
>>> ns_kb = Namespace("http://example.org/kb/")
213-
>>> n_pcap_url_facet_try2 = get_facet_uriref(n_pcap, NS_UCO_OBSERVABLE.URLFacet, namespace=ns_kb)
214-
>>> n_pcap_url_facet_try2
172+
>>> n_pcap_url_facet = get_facet_uriref(n_pcap, NS_UCO_OBSERVABLE.URLFacet, namespace=ns_kb)
173+
>>> n_pcap_url_facet
215174
rdflib.term.URIRef('http://example.org/kb/URLFacet-4b6023da-dbc4-5e1e-9a2f-aca2a6f6405c')
216175
"""
217176
uco_object_uuid_namespace: uuid.UUID = inherence_uuid(n_uco_object)
218177
facet_uuid = facet_inherence_uuid(uco_object_uuid_namespace, n_facet_class)
219178

220-
_namespace: Namespace
221-
if namespace is None:
222-
_namespace = guess_namespace(n_uco_object)
223-
else:
224-
_namespace = namespace
225-
226179
# NOTE: This encodes an assumption that Facets (including extension Facets) use the "Slash" IRI style.
227180
facet_class_local_name = str(n_facet_class).rsplit("/")[-1]
228181

229-
return _namespace[facet_class_local_name + "-" + str(facet_uuid)]
182+
return namespace[facet_class_local_name + "-" + str(facet_uuid)]
230183

231184

232185
def hash_method_value_uuid(l_hash_method: Literal, l_hash_value: Literal) -> uuid.UUID:

0 commit comments

Comments
 (0)