"""DIDComm prefix management."""
import re
from enum import Enum
QUALIFIED = re.compile(r"^[a-zA-Z\-\+]+:.+")
[docs]
def qualify(msg_type: str, prefix: str):
"""Qualify a message type with a prefix, if unqualified."""
return msg_type if QUALIFIED.match(msg_type or "") else f"{prefix}/{msg_type}"
[docs]
class DIDCommPrefix(Enum):
"""Enum for DIDComm Prefix, old or new style, per Aries RFC 384."""
NEW = "https://didcomm.org"
OLD = "did:sov:BzCbsNYhMrjHiqZDTUASHg;spec"
[docs]
def qualify(self, msg_type: str) -> str:
"""Qualify input message type with prefix and separator."""
return qualify(msg_type, self.value)
[docs]
@classmethod
def qualify_all(cls, messages: dict) -> dict:
"""Apply all known prefixes to a dictionary of message types."""
return {qualify(k, pfx.value): v for pfx in cls for k, v in messages.items()}
[docs]
@staticmethod
def qualify_current(slug: str) -> str:
"""Qualify input slug with prefix currently in effect and separator.
This method now will always use the new prefix.
"""
return qualify(slug, DIDCommPrefix.NEW.value)
[docs]
@staticmethod
def unqualify(qual: str) -> str:
"""Strip prefix and separator from input, if present, and return result."""
for pfx in DIDCommPrefix:
if (qual or "").startswith(f"{pfx.value}/"):
return qual.split(f"{pfx.value}/")[1]
return qual