Utilities¶
kurra.utils ¶
Utilities used by the other modules.
GspType ¶
Bases: str, Enum
Graph Store Protocol operation types: get, put, post, or delete.
RenderFormat ¶
Bases: str, Enum
Output formats for render_sparql_result: original, json or markdown.
guess_format_from_data ¶
Guess an RDF media type from a string of RDF data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rdf
|
str
|
The RDF data to inspect. |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
The guessed media type, or None if |
Source code in kurra/utils.py
load_graph ¶
load_graph(source: Union[GraphInput, list[GraphInput], tuple[GraphInput, ...]], *additional_graph_paths_or_str: GraphInput, recursive: bool = False) -> Graph
Return an RDFLib Graph from one or more existing Graphs, pickle-cached RDF files, RDF files or directories, remote RDF URLs, or RDF data strings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
Union[GraphInput, list[GraphInput], tuple[GraphInput, ...]]
|
A single input, or a list/tuple of inputs, to combine into one graph. |
required |
*additional_graph_paths_or_str
|
GraphInput
|
Further inputs, combined with |
()
|
recursive
|
bool
|
If True, recurse into subdirectories when |
False
|
Returns:
| Type | Description |
|---|---|
Graph
|
The combined Graph. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If a given filesystem path does not exist. |
Source code in kurra/utils.py
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 | |
render_sparql_result ¶
Render a SPARQL result as plain JSON, Markdown, or its original form.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
r
|
dict | str | Graph
|
The SPARQL result to render as a dict, a JSON string, or a Graph (for CONSTRUCT/DESCRIBE). |
required |
rf
|
RenderFormat
|
The format to render as. |
markdown
|
Returns:
| Type | Description |
|---|---|
str
|
The rendered result. |
Source code in kurra/utils.py
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 | |
make_httpx_client ¶
make_httpx_client(sparql_username: str | None = None, sparql_password: str | None = None, timeout: int = 60) -> httpx.Client
Create an HTTPX client, with Basic Auth if a username and password are given.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sparql_username
|
str | None
|
The username for Basic Auth. |
None
|
sparql_password
|
str | None
|
The password for Basic Auth. |
None
|
timeout
|
int
|
The client's request timeout, in seconds. |
60
|
Returns:
| Type | Description |
|---|---|
Client
|
A configured HTTPX Client. |
Source code in kurra/utils.py
convert_sparql_json_to_python ¶
convert_sparql_json_to_python(j: Union[str, bytes, Response], return_bindings_only: bool = False) -> dict
Convert a SPARQL JSON results response into native Python types.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
j
|
Union[str, bytes, Response]
|
The SPARQL JSON results, as a string, bytes, or an HTTPX Response. |
required |
return_bindings_only
|
bool
|
If True, return just the result bindings (for SELECT) or a bool (for ASK), rather than the full SPARQL results structure. |
False
|
Returns:
| Type | Description |
|---|---|
dict
|
The converted result. |
Source code in kurra/utils.py
sparql_statement_return_type ¶
Get the media type a SPARQL endpoint should return for a query.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
The SPARQL query. |
required |
statement
|
SparqlStatementType | None
|
The query's parsed statement type, if already known. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
|
Source code in kurra/utils.py
statement_type_for_query ¶
Get the statement type of a SPARQL query/update string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
The SPARQL query or update. |
required |
Returns:
| Type | Description |
|---|---|
SparqlStatementType
|
The statement type. |
Source code in kurra/utils.py
is_construct_query ¶
Check whether a given query is a SPARQL CONSTRUCT query.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
The SPARQL query. |
required |
statement
|
SparqlStatementType | None
|
The query's parsed statement type, if already known. |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
True if |
Source code in kurra/utils.py
is_describe_query ¶
Check whether a given query is a SPARQL DESCRIBE query.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
The SPARQL query. |
required |
statement
|
SparqlStatementType | None
|
The query's parsed statement type, if already known. |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
True if |
Source code in kurra/utils.py
is_select_query ¶
Check whether a given query is a SPARQL SELECT query.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
The SPARQL query. |
required |
statement
|
SparqlStatementType | None
|
The query's parsed statement type, if already known. |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
True if |
Source code in kurra/utils.py
is_ask_query ¶
Check whether a given query is a SPARQL ASK query.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
The SPARQL query. |
required |
statement
|
SparqlStatementType | None
|
The query's parsed statement type, if already known. |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
True if |
Source code in kurra/utils.py
is_construct_or_describe_query ¶
Check whether a given query is a SPARQL CONSTRUCT or DESCRIBE query.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
The SPARQL query. |
required |
statement
|
SparqlStatementType | None
|
The query's parsed statement type, if already known. |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
True if |
Source code in kurra/utils.py
is_select_or_ask_query ¶
Check whether a given query is a SPARQL SELECT or ASK query.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
The SPARQL query. |
required |
statement
|
SparqlStatementType | None
|
The query's parsed statement type, if already known. |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
True if |
Source code in kurra/utils.py
is_update_query ¶
Check whether a given query is a SPARQL update.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
The SPARQL query. |
required |
statement
|
SparqlStatementType | None
|
The query's parsed statement type, if already known. |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
True if |
Source code in kurra/utils.py
is_drop_update ¶
Check whether a given query is a SPARQL DROP update.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
The SPARQL query. |
required |
statement
|
SparqlStatementType | None
|
The query's parsed statement type, if already known. |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
True if |
Source code in kurra/utils.py
make_sparql_dataframe ¶
Convert a parsed SPARQL SELECT or ASK result into a pandas DataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sparql_result
|
dict
|
The parsed SPARQL JSON result. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
A DataFrame of the result's bindings (SELECT) or its boolean value (ASK). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If pandas is not installed. |
Source code in kurra/utils.py
add_namespaces_to_query_or_data ¶
Prepend PREFIX declarations to a SPARQL query or RDF data string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
q
|
str
|
The SPARQL query or RDF data to prepend to. |
required |
namespaces
|
dict
|
Namespace prefixes to declare, keyed by prefix. |
required |
Returns:
| Type | Description |
|---|---|
str
|
|
Source code in kurra/utils.py
get_system_graph ¶
get_system_graph(system_graph_source: str | Path | Dataset | Graph = None, http_client: Client | None = None) -> Graph | int
Load Olis's System Graph from a file, Graph, Dataset, or SPARQL endpoint.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
system_graph_source
|
str | Path | Dataset | Graph
|
The source to load from as an RDF file path, Graph, Dataset, SPARQL endpoint URL, or None for an empty System Graph. |
None
|
http_client
|
Client | None
|
An optional HTTPX client to contain credentials if needed to access a SPARQL endpoint. A new one is created if not given. |
None
|
Returns:
| Type | Description |
|---|---|
Graph | int
|
The System Graph, or the HTTP status code if fetching from a remote endpoint failed. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in kurra/utils.py
put_system_graph ¶
put_system_graph(system_graph: Graph, system_graph_source: str | Path | Dataset | Graph | None = None, http_client: Client | None = None) -> Graph | int | None
Write a System Graph back to its file, Dataset, or SPARQL endpoint source.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
system_graph
|
Graph
|
The System Graph to write. |
required |
system_graph_source
|
str | Path | Dataset | Graph | None
|
An RDF file path, a Dataset, a SPARQL endpoint URL, or None to return |
None
|
http_client
|
Client | None
|
An optional HTTPX client to contain credentials if needed to access a SPARQL endpoint. A new one is created if not given. |
None
|
Returns:
| Type | Description |
|---|---|
Graph | int | None
|
The HTTP status code if writing to a remote endpoint failed, |
Source code in kurra/utils.py
make_system_specific_sparql_endpoint ¶
make_system_specific_sparql_endpoint(sparql_endpoint: str, q: str | None = None, statement: SparqlStatementType | None = None, gsp_query_type: GspType | None = None) -> str
Adjust a SPARQL endpoint URL to match a specific triplestore's conventions.
For example, GraphDB requires /statements appended to its base SPARQL endpoint for updates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sparql_endpoint
|
str
|
The base SPARQL endpoint URL. |
required |
q
|
str | None
|
The SPARQL query or update, used to detect GraphDB update endpoints. |
None
|
statement
|
SparqlStatementType | None
|
The query's parsed statement type, if already known. |
None
|
gsp_query_type
|
GspType | None
|
The Graph Store Protocol operation type, if this is a GSP request. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
The adjusted endpoint URL, or |
Source code in kurra/utils.py
iter_iris ¶
Iterate over every IRI referenced in a graph's triples.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
Graph
|
The graph to scan. |
required |
Returns:
| Type | Description |
|---|---|
Iterable[URIRef]
|
Each IRI found. |
Source code in kurra/utils.py
build_values_clause ¶
Build a SPARQL VALUES clause for the given values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
dict[str, Iterable[Node]]
|
A dictionary where keys are variable names and values are iterables of RDF nodes. |
required |
Returns:
| Type | Description |
|---|---|
str
|
A string containing the SPARQL VALUES clause. |
Source code in kurra/utils.py
is_class ¶
Check whether the given IRI is a class in the provided RDF graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
Graph
|
An RDFLib Graph object. |
required |
iri
|
URIRef
|
The IRI to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the IRI is a class, False otherwise. |