ngsildclient.utils.urn#

This module contains helper functions to URI/URN in the NGSI-LD namespace.

References

2

ETSI, 2021. “NGSI-LD namespace” in Context Information Management (CIM); NGSI-LD API ETSI GS CIM 009 V1.4.2, Annex A.3, p.183, 2021-04.

Module Attributes

NID_PATTERN

Regex pattern that matches a valid namespace identifier (regex.Pattern).

URN_PATTERN

Regex pattern that extracts NID and NSS from a full valid urn string(regex.Pattern).

ENTITY_TYPE_PATTERN

Regex pattern that extracts type from a NGSI-LD NSS following naming convention.(regex.Pattern).

Classes

Urn()

Helper class to handle NGSI-LD urn/uri and allow to work with unprefixed strings.

Exceptions

UrnError

Exception raised when parsing invalid urn/uri.

ngsildclient.utils.urn.ENTITY_TYPE_PATTERN = re.compile('^([^:]+):.*')#

Regex pattern that extracts type from a NGSI-LD NSS following naming convention.(regex.Pattern).

ngsildclient.utils.urn.NID_PATTERN = re.compile('^[0-9a-zA-Z\\-]+$')#

Regex pattern that matches a valid namespace identifier (regex.Pattern).

ngsildclient.utils.urn.URN_PATTERN = re.compile('^urn:(?P<nid>[0-9a-zA-Z\\-]+):(?P<nss>.+)$')#

Regex pattern that extracts NID and NSS from a full valid urn string(regex.Pattern).

class ngsildclient.utils.urn.Urn(fqn: str)[source]#
class ngsildclient.utils.urn.Urn(*, nss: str, nid: str = DEFAULT_NID)

Bases: object

Helper class to handle NGSI-LD urn/uri and allow to work with unprefixed strings.

property fqn: str#

Returns the fully-qualified name

Returns

the fully qualified name

Return type

str

Example

>>> from ngsildclient.utils.urn import Urn
>>> urn = Urn(nss="AirQualityObserved:RZ:Obsv4567")
>>> print(urn.fqn)
urn:ngsi-ld:AirQualityObserved:RZ:Obsv4567
infertype()[source]#

Infer type.

Work only when following the naming convention urn:ngsi-ld:<type>:…

Returns

the inferred NGSI LD type if found

Return type

Optional[str]

static is_prefixed(value)[source]#

Check whether the given string is prefixed (URN scheme+NID)

Parameters

value (str) – the string value to be checked

Returns

True if the string value starts with the NGSI-LD u`urn:ngsi-ld:`

Return type

bool

Example

>>> from ngsildclient.utils.urn import Urn
>>> print(Urn.is_prefixed("urn:ngsi-ld:AirQualityObserved:RZ:Obsv4567"))
True
static is_valid_nid(nid)[source]#

Check whether the given nid is a valid one

Parameters

nid (str) – the nid string to be checked

Returns

True if if the nid is valid

Return type

bool

Example

>>> from ngsildclient.utils.urn import Urn
>>> print(Urn.is_valid_nid("ngsi-ld"))
True
>>> print(Urn.is_valid_nid("ngsi+ld"))
False
static prefix(value)[source]#

Prefix a string with URN scheme+NID

Parameters

value (str) – the string to be prefixed

Returns

the string prefixed (if not already prefixed)

Return type

str

Example

>>> from ngsildclient.utils.urn import Urn
>>> print(Urn.prefix("AirQualityObserved:RZ:Obsv4567"))
urn:ngsi-ld:AirQualityObserved:RZ:Obsv4567
static unprefix(value)[source]#

Remove the prefix (URN scheme+NID)

Parameters

value (str) – the string to be unprefixed

Returns

the string without the prefix

Return type

str

Example

>>> from ngsildclient.utils.urn import Urn
>>> print(Urn.unprefix("urn:ngsi-ld:AirQualityObserved:RZ:Obsv4567"))
AirQualityObserved:RZ:Obsv4567
exception ngsildclient.utils.urn.UrnError[source]#

Bases: Exception

Exception raised when parsing invalid urn/uri.

with_traceback()#

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.