Skip to content
This repository has been archived by the owner on Sep 17, 2024. It is now read-only.

Commit

Permalink
Merge pull request #75 from dbluhm/refactor/module-router-context
Browse files Browse the repository at this point in the history
Further simplify module routing
  • Loading branch information
dbluhm authored Oct 11, 2021
2 parents 86774e4 + 1de36bb commit 2125c46
Show file tree
Hide file tree
Showing 6 changed files with 268 additions and 220 deletions.
2 changes: 1 addition & 1 deletion aries_staticagent/dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class NoRegisteredHandlerException(Exception):
class Handler: # pylint: disable=too-few-public-methods
"""A Message Handler."""

__slots__ = ("type", "handler", "context")
__slots__ = ("type", "handler")

def __init__(self, msg_type: MsgType, handler: Callable):
if not isinstance(msg_type, MsgType):
Expand Down
43 changes: 38 additions & 5 deletions aries_staticagent/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
from .mtc import MessageTrustContext


MTURI_RE = re.compile(r"(.*?)([a-z0-9._-]+)/(\d[^/]*)/([a-z0-9._-]+)$")


class MsgVersion(VersionInfo): # pylint: disable=too-few-public-methods
"""Wrapper around the more complete VersionInfo class from semver package.
Expand Down Expand Up @@ -46,17 +43,53 @@ def from_str(cls, version_str):
)


class InvalidProtocolIdentifier(ValueError):
"""Raised when protocol identifier is unparsable or invalid."""


class ProtocolIdentifier(str):
"""Protocol identifier."""

PIURI_RE = re.compile(r"^(.*?)([a-z0-9._-]+)/(\d[^/]*)/?$")

def __init__(self, ident: str):
"""Parse Protocol Identifier string."""
super().__init__()
matches = self.PIURI_RE.match(ident)
if not matches:
raise InvalidProtocolIdentifier(f"Invalid protocol identifier: {ident}")
doc_uri, protocol, version = matches.groups()
try:
self.version_info = MsgVersion.from_str(version)
except ValueError as err:
raise InvalidProtocolIdentifier(
f"Invalid protocol version {version}"
) from err

self.version = version
self.doc_uri = doc_uri
self.protocol = protocol
self.normalized = f"{self.doc_uri}{self.protocol}/{self.version_info}"
self.normalized_version = str(self.version_info)

@classmethod
def unparse(cls, doc_uri: str, protocol: str, version: str):
return cls(f"{doc_uri}{protocol}/{version}")


class InvalidType(ValueError):
"""When type is unparsable or invalid."""


class MsgType(str):
"""Message type."""

MTURI_RE = re.compile(r"^(.*?)([a-z0-9._-]+)/(\d[^/]*)/([a-z0-9._-]+)$")

def __init__(self, msg_type: str):
"""Parse Message Type string."""
super().__init__()
matches = MTURI_RE.match(msg_type)
matches = self.MTURI_RE.match(msg_type)
if not matches:
raise InvalidType(f"Invalid message type: {msg_type}")

Expand All @@ -73,7 +106,7 @@ def __init__(self, msg_type: str):
self.normalized = (
f"{self.doc_uri}{self.protocol}/{self.version_info}/{self.name}"
)
self.normalized_version = f"{self.version_info}"
self.normalized_version = str(self.version_info)

@classmethod
def __get_validators__(cls):
Expand Down
Loading

0 comments on commit 2125c46

Please sign in to comment.