lakesuperior.util package

Submodules

lakesuperior.util.benchmark module

lakesuperior.util.generators module

lakesuperior.util.ingest_random_image module

lakesuperior.util.locustfile module

lakesuperior.util.toolbox module

Utility to translate and generate strings and other objects.

class lakesuperior.util.toolbox.RequestUtils[source]

Bases: object

Utilities that require access to an HTTP request context.

Initialize this within a Flask request context.

globalize_graph(gr)[source]

Globalize a graph.

globalize_imr(imr)[source]

Globalize an Imr.

Return type:rdflib.Graph
globalize_rsrc(rsrc)[source]

Globalize a resource.

globalize_string(s)[source]

Convert URNs into URIs in a string using the application base URI.

Parameters:s (string) – Input string.
Return type:string
globalize_term(urn)[source]

Convert an URN into an URI using the application base URI.

Parameters:urn (rdflib.URIRef) – Input URN.
Return type:rdflib.URIRef
globalize_triple(trp)[source]

Globalize terms in a triple.

Parameters:trp (tuple(rdflib.URIRef)) – The triple to be converted
Return type:tuple(rdflib.URIRef)
localize_ext_str(s, urn)[source]

Convert global URIs to local in a SPARQL or RDF string.

Also replace empty URIs (<>) with a fixed local URN and take care of fragments and relative URIs.

This is a 3-pass replacement. First, global URIs whose webroot matches the application ones are replaced with internal URIs. Then, relative URIs are converted to absolute using the internal URI as the base; finally, the root node is appropriately addressed.

localize_graph(gr)[source]

Localize a graph.

localize_payload(data)[source]

Localize an RDF stream with domain-specific URIs.

Parameters:data (bytes) – Binary RDF data.
Return type:bytes
localize_term(uri)[source]

Localize an individual term.

Parameters:rdflib.URIRef – urn Input URI.
Return type:rdflib.URIRef
localize_triple(trp)[source]

Localize terms in a triple.

Parameters:trp (tuple(rdflib.URIRef)) – The triple to be converted
Return type:tuple(rdflib.URIRef)
localize_uri_string(s)[source]

Convert URIs into URNs in a string using the application base URI.

Parameters:str – s Input string.
Return type:str
uid_to_uri(uid)[source]

Convert a UID to a URI.

Return type:rdflib.URIRef
uri_to_uid(uri)[source]

Convert an absolute URI (internal or external) to a UID.

Return type:str
lakesuperior.util.toolbox.fsize_fmt(num, suffix='b')[source]

Format an integer into 1024-block file size format.

Adapted from Python 2 code on https://stackoverflow.com/a/1094933/3758232

Parameters:
  • num (int) – Size value in bytes.
  • suffix (str) – Suffix label (defaults to b).
Return type:

str

Returns:

Formatted size to largest fitting unit.

lakesuperior.util.toolbox.get_tree_size(path, follow_symlinks=True)[source]

Return total size of files in given path and subdirs.

Ripped from https://www.python.org/dev/peps/pep-0471/

lakesuperior.util.toolbox.parse_rfc7240(h_str)[source]

Parse Prefer header as per https://tools.ietf.org/html/rfc7240

The cgi.parse_header standard method does not work with all possible use cases for this header.

Parameters:h_str (str) – The header(s) as a comma-separated list of Prefer statements, excluding the Prefer: token.
lakesuperior.util.toolbox.rel_uri_to_urn(uri, uid)[source]

Convert a URIRef with a relative location (e.g. <>) to an URN.

Parameters:
  • uri (URIRef) – The URI to convert.
  • uid (str) – Resource UID that the URI should be relative to.
Returns:

Converted URN if the input is relative, otherwise the unchanged URI.

Return type:

URIRef

lakesuperior.util.toolbox.rel_uri_to_urn_string(string, uid)[source]

Convert relative URIs in a SPARQL or RDF string to internal URNs.

Parameters:string (str) – Input string.

:param str uid Resource UID to build the base URN from.

Return type:str
Returns:Modified string.
lakesuperior.util.toolbox.replace_term_domain(term, search, replace)[source]

Replace the domain of a term.

Parameters:
  • term (rdflib.URIRef) – The term (URI) to change.
  • search (str) – Domain string to replace.
  • replace (str) – Domain string to use for replacement.
Return type:

rdflib.URIRef

lakesuperior.util.toolbox.split_uuid(uuid)[source]

Split a UID into pairtree segments. This mimics FCREPO4 behavior.

Parameters:uuid (str) – UUID to split.
Return type:str