prefixmaps package

Subpackages

Module contents

class prefixmaps.Context(name, description=None, prefix_expansions=<factory>, comments=None, location=None, format=None, merged_from=None, upper=None, lower=None, _prefixes=<factory>, _prefixes_lower=<factory>, _namespaces=<factory>, _namespaces_lower=<factory>)

Bases: object

A context is a localized collection of prefix expansions.

A context should be internally consistent:

  • the set of canonical PrefixExpansions should be bijective

However, there is no guarantee that a context is consistent with other contexts.

add_prefix(prefix, namespace, status=StatusType.canonical, preferred=False, expansion_source=None, force=False)

Adds a prefix expansion to this context.

The current context stays canonical. Additional prefixes added may be classified as non-canonical.

If upper or lower is set for this context, the prefix will be auto-case normalized, UNLESS preferred=True

Parameters:
  • prefix (str) – prefix to be added

  • namespace (str) – namespace to be added

  • status (StatusType) – the status of the prefix being added

  • preferred (bool) –

  • expansion_source (Optional[str]) – An optional annotation to be used when merging contexts together. The source will keep track of the original context that a given prefix expansion came from. This is used in Context.combine().

  • force (bool) – if True, recompute namespaces and prefixes. default False.

Returns:

as_converter()

Get a converter from this prefix map.

Return type:

Converter

Returns:

as_dict()

Returns a mapping between canonical prefixes and expansions.

This only includes canonical expansions. The results can be safely used in the header of RDF syntax documents.

Return type:

Mapping[str, str]

Returns:

Mappings between prefixes and namespaces

as_extended_prefix_map()

Return an extended prefix, appropriate for generating a curies.Converter.

An extended prefix map is a collection of dictionaries, each of which has the following fields: :rtype: List[Record]

  • prefix - the canonical prefix

  • uri_prefix - the canonical URI prefix (i.e. namespace)

  • prefix_synonyms - optional extra prefixes such as capitialization variants. No prefix synonyms are allowed to be duplicate across any canonical prefixes or synonyms in other records in the extended prefix

  • uri_prefix_synonyms - optional extra URI prefixes such as variants of Identifiers.org URLs, PURLs, etc. No URI prefix synyonms are allowed to be duplicates of either canonical or other URI prefix synonyms.

Extended prefix maps have the benefit over regular prefix maps in that they keep extra information. An extended prefix map can be readily collapsed into a normal prefix map by getting the prefix and uri_prefix fields.

as_inverted_dict()

Returns a mapping between canonical expansions and prefixes.

Return type:

Mapping[str, str]

Returns:

Mapping between namespaces and prefixes

combine(context)

Merge a context into this one.

If there are conflicts, the current context takes precedence, and the merged expansions are marked as non-canonical

Parameters:

context (Context) –

Returns:

comments: List[str] = None

Optional comments on the context.

description: Optional[str] = None

A human readable concise description of the context.

filter(prefix=None, namespace=None)

Returns namespaces matching query.

Parameters:
  • prefix (Optional[str]) –

  • namespace (Optional[str]) –

Returns:

format: Optional[str] = None
location: Optional[str] = None
lower: bool = None
merged_from: Optional[List[str]] = None
name: str

A unique stable handle for the context.

namespaces(lower=False, force=True, as_list=True)

All unique namespaces in all prefix expansions

Parameters:
  • lower – if True, the namespace is normalized to lowercase.

  • force (bool) – if True, recompute. if False, return cached

  • as_list (bool) – if True (default), return as a list. Otherwise a set

Return type:

Union[List[str], Set[str]]

Returns:

prefix_expansions: List[PrefixExpansion]

All prefix expansions within that context. Corresponds to http://www.w3.org/ns/shacl#prefixes

prefixes(lower=False, force=True, as_list=True)

All unique prefixes in all prefix expansions.

Parameters:
  • lower – if True, the prefix is normalized to lowercase.

  • force (bool) – if True, recompute. if False, return cached

  • as_list (bool) – if True (default), return as a list. Otherwise a set

Return type:

Union[List[str], Set[str]]

Returns:

upper: bool = None
validate(canonical_only=True)

Validates each prefix expansion in the context.

Parameters:

canonical_only

Return type:

List[str]

Returns:

class prefixmaps.PrefixExpansion(context, prefix, namespace, status, expansion_source=None)

Bases: object

An individual mapping between a prefix and a namespace.

A PrefixExpansion corresponds to a SHACL PrefixDeclaration (https://www.w3.org/TR/shacl/#dfn-prefix-declarations)

canonical()

True if this is the canonical mapping in both directions.

Note that canonicality is always relative to a context:

Return type:

bool

Returns:

True if the status is canonical

context: str

Each PrefixExpansion is grouped into a context.

expansion_source: Optional[str] = None

Indicates the source of the prefix expansion.

namespace: str

Corresponds to http://www.w3.org/ns/shacl#namespace

prefix: str

Corresponds to http://www.w3.org/ns/shacl#prefix

status: StatusType

Indicates whether the expansion is canonical, a prefix alias, a namespace alias, or both.

validate()

Validate the prefix expansion.

  • Ensures that prefixes conform to W3C CURIE syntax

  • Ensures that namespaces conform to a restricted subset of W3C URI syntax

Note that we use a highly restricted syntax in order to filter out pseudo-semantic URIs. These include URLs for websites intended for humans that have http parameters with `?`s, `=`s, etc.

These URLs are almost NEVER intended to be used as semantic URIs, i.e as subjects of RDF triples. It is almost always bad practice to use them as such.

In future, if we discover exceptions to this rule, we will add them here.

Return type:

List[str]

Returns:

list of validation errors

class prefixmaps.StatusType(value)

Bases: Enum

Classification of prefix expansions.

Note that only canonical mappings are exposed to the users of the library. However, it can be useful for prefixmap ETL pipelines to include non-canonical mappings for purposes of debugging.

canonical = 'canonical'

The canonical prefix expansion for a prefix. The set of all canonical mappings must be bijective.

multi_alias = 'multi_alias'

Both the prefix and the namespace are aliases for existing canonical namespaces.

namespace_alias = 'namespace_alias'

The prefix is an alias for an existing canonical namespace.

prefix_alias = 'prefix_alias'

The prefix is an alias for an existing canonical prefix.

prefixmaps.load_context(name, refresh=False)

Loads a context by name from standard location

Parameters:
  • name (str) –

  • refresh – if True, fetch from upstream

Return type:

Context

Returns:

prefixmaps.load_converter(names, refresh=False)

Get a converter.

Return type:

Converter

prefixmaps.load_multi_context(names, refresh=False)

Merges multiple contexts

Parameters:
  • names (List[str]) –

  • refresh – if True, fetch from upstream

Return type:

Context

Returns: